Page 2 of 2
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 12:50 am
by nichg
I
think I have a sane way to pass
certain things to script_entities from hooks. I've had decent luck using '.' instead of ':'. For instance, the stuff I was talking about earlier in this thread works consistently for me if I do:
script_library
Code: Select all
function checkChampionEquip(champ_id, itemname)
champ = party:getChampion(champ_id)
for i=1,10 do
if champ:getItem(i):name == itemname then
return true
end
end
end
OnDamage hook
Code: Select all
onDamage = function(champ, dmg, dmgtype)
if script_library.checkChampionEquip(champ:getOrdinal(), "senvelor_shield") then
-- do stuff!
end
end
Whereas it crashes and gives me weird references if I did script_library:checkChampionEquip(...). I'm not sure why, but you might find this fixes your bug.
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 1:21 am
by zalewapl
Oh silly me, I've figured out why the game was crashing. I completely forgot I had an onDie hook that looked like this:
Code: Select all
onDie = function(champion)
local CHAMPION_DEATH_PRICE = 1000
scoreLua.score = scoreLua.score - CHAMPION_DEATH_PRICE
print ("Score " .. scoreLua.score)
end,
Still, that's not a real reason for a nasty CTD so I still believe that there is a bug somewhere. The game shouldn't crash here. Instead it should point out what I did wrong.
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 4:01 am
by nichg
Try doing something like this instead:
scoreLua
Code: Select all
function decrementScore(ammt)
score = score - ammt
end
function getScore()
return score
end
onDie
Code: Select all
onDie = function(champion)
-- ...
scoreLua.decrementScore(ammt)
end
Use scoreLua.getScore() when you want to access it. I've found I had to do that with a timer on a featherfall potion I have that has to tie into onMove to correctly count down for puzzles/etc.
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 10:58 am
by Xanathar
- Whereas it crashes and gives me weird references if I did script_library:checkChampionEquip(...).
You have to call it with a "." not a ":".
If you call a method with a ":" it receives what's before the ":" as the first argument and thus you'll end up having all the function arguments shifted by one (thus the error).
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 8:11 pm
by nichg
Aha, good to know why it happens. I guess then it could also affect other bits of code where one script calls another directly. And all of Grimrock's functions for objects always expect a self to be passed to them. Okay, that makes sense.
Re: Weird glitch between hooks and script_entity objects
Posted: Mon Oct 29, 2012 8:26 pm
by zalewapl
Yeah, that explains a lot. I feel really ashamed now for making such basic mistakes.