executing a function that is a value in a table

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

executing a function that is a value in a table

Post 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?

davidk
Posts: 13
Joined: Wed Dec 09, 2009 7:01 pm
Location: avalon.mud.de

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

Post 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.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

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

Post 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.

davidk
Posts: 13
Joined: Wed Dec 09, 2009 7:01 pm
Location: avalon.mud.de

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

Post by davidk »

There is also a third syntax:

Code: Select all

attackTable = { }

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

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

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

Post 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.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

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

Post 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.

Post Reply