This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
civilwargeeky's profile picture

[Lua] ToFunction?

Started by civilwargeeky, 19 December 2012 - 01:50 PM
civilwargeeky #1
Posted 19 December 2012 - 02:50 PM
So I was getting ready to make a text based game (sort of like the one included) where a user would type an input, like hit or mine something, and the program would react accordingly. So what I was wondering if there was some way to call a string (I guess you would say). So:

function punch()
steve.punchTree()
TreePunch = true
end

action = read()
[value of action]()
I know that you could do a lot of if statements, but I was curious if there is a simple way to do this in this manner.
Lyqyd #2
Posted 19 December 2012 - 03:05 PM
Easiest way is going to be to stick all of the functions in a table.


actionTable = {}
actionTable.punch = function()
 --blah
end

act = read()
actionTable[act]()