Page 1 of 1

executing a function that is a value in a table

Posted: Mon Dec 14, 2009 3:37 pm
by Caled

Code: Select all

attackTable={
 axehere=axeHere,
 axeback=axeBack
}

function tabAttack( att )
    local attack = attackTable[ att ]
    attack()
end


function axeHere()
     --blah blah some commands
end
A few months back, the above code worked to execute axeHere()
I haven't mudded much in the mean time, and certainly haven't used that script, but tonight I attempted to do both and discovered that this script no longer works.

Potentially, I may have broken the script a few months back and forgot about it - perhaps it never worked that way. Regardless, I HAVE executed functions in a manner something like this, but have forgotten how to do so (or something has changed).

Please help?

Re: executing a function that is a value in a table

Posted: Mon Dec 14, 2009 5:33 pm
by davidk
You would have to define the function axeHere before assigning it to another variable. Else the variable "axeHere" would be undefined (the nil value).

I guess it only worked previously when you executed the given code twice so during the second run "axeHere" was the correct function.

Re: executing a function that is a value in a table

Posted: Wed Dec 16, 2009 8:00 am
by Thylacine
It's been a while since I done any lua, but I think you can use anonymous (lamda) functions to do the same thing if you don't want to define axeHere.

I think in you'd replace axehere = axeHere with axehere = function() echo("do stuff") end. Otherwise just define axeHere first like davidk suggested.

Re: executing a function that is a value in a table

Posted: Thu Dec 17, 2009 8:58 am
by davidk
There is also a third syntax:

Code: Select all

attackTable = { }

function attackTable.axehere ()
  -- ...
end

Re: executing a function that is a value in a table

Posted: Thu Dec 17, 2009 11:59 am
by Caled
Thanks for the replies. The problem was just that the script object which defined that function was moved (by me) and in its new location it was executed -after- rather than before. So now it is in the correct order again, it works.

It does seem a bit of a design problem. I will not say design fault, just a problem, because it is easy to rearrange script objects to keep things organised nicely, but more difficult to remember when things must be in a specific order if the script was written six months ago (or written by someone else).

You might suggest that stuff like this ought to all be written into the same script object, but that would be a little awkward here. My attack table and the other table and scripts that with it form my entire combat offense, is pretty long, at least for a mudding script. I'm going to make a guess and say 500 lines. The beauty of not having that all in 1-2 script objects (aside from making it easier to fund and edit stuff) is also making it easier to separate common scripts from character specific scripts.

Re: executing a function that is a value in a table

Posted: Thu Dec 17, 2009 10:37 pm
by Thylacine
You could always save the .lua file in a relevant location and load it into others with dofile('path/to/file.lua').

You are right, though. It is a little awkward trying to keep them in order at the moment.