Yes, that would work just fine. In the script I posted, there are actually two completely distinct functions referenced by the name "onDie" and two distinct functions referenced by the name "onDamage". Lua can tell apart the two "onDie" functions. The first "onDie" function (in monsters.lua) is a value in an anonymous table, corresponding to the key "onDie". That table is then used as the argument to the Grimrock API function cloneObject. The second "onDie" function (in bossScript) is stored in bossScript's non-local variable onDie. I prefer to reuse the names like this when the purpose of the two functions is so closely related (the function in monsters.lua does nothing except call the function in bossScript), but if you prefer to use distinct names for distinct functions, there's nothing wrong with that approach, either.Ryeath_Greystalk wrote:I see you have onDie and onDamage in both the monsters.lua and bossScript. Are they calling the same functions or are they separate? For example could I use
monsters.lua
onDie = function( monster )
return bossScript.isItDeadYet( monster )
end,
with bossScript
function isItDeadYet(monster)
That's correct.Ryeath_Greystalk wrote:Another unclear this is alreadyEscaped variable. I don't see it assigned to anything until after the monster has escaped where you give it alreadyEscaped = true.
Everything you said here is true, except for bringing up an error. Nil is a legitimate value in lua. The value of any variable which has not yet been initialized is nil. The same is true for values within a table which have not yet been initialized. So when lua checks alreadyEscaped before a value has been assigned to this variable, its value is nil. When nil is used as a condition or within complex logical operations:Ryeath_Greystalk wrote: Wouldn't that bring up an error for a nil value? I tend to think very linear. So I would think it would happen like this, I hit monster, monster takes damage, call bossScript.onDamage, function tries to compare not alreadyEscaped..lua say "whoa, what is this alreadyEscaped thing I haven't seen that yet, guess I will give it a nil value."
Code: Select all
if nil then
--lua will never reach this line
end
if (not nil) then
--lua will always reach this line
end
if ( nil or x ) then
--lua will reach this line if and only if the variable x is a "positive" value
--anything except for nil or false is a "positive" value
end
if ( nil and y ) then
--lua will never reach this line
--in fact, lua will never even check the value of y because ( nil and whatever ) is always a "negative" result
end
Code: Select all
alreadyEscaped = false
You have probably seen errors occur from attempting to index a nil value. This happens when you try to treat a nil value as if it was a table. For example:
Code: Select all
local entity = findEntity("NoSuchEntity")
entity:destroy()
Code: Select all
local entity = findEntity("NoSuchEntity")
if entity then
entity:destroy()
end
Code: Select all
local entity = findEntity("NoSuchEntity")
if ( type(entity) == "table" ) and ( type(entity.destroy) == "function" ) then
entity:destroy()
end
It seems I've rambled quite a bit already. I hope some of this has helped