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

Calling Functions from Arrays As References?

Started by mckerrnel, 17 May 2014 - 04:20 AM
mckerrnel #1
Posted 17 May 2014 - 06:20 AM
By no means am I good at LUA so am trying to fudge something to let me continue where my turtle left off before chunk unloading, etc.

I have a 40-step loop that I want my turtle to run, doing various tasks. I was thinking one way to do it was to make a 40-element array with references to the function I would need to call. I know you can do something like this in Perl but wasn't sure if it was possible in LUA.

Like:

task[1] = &goForward()
task[2] = &turnRight

So then I could go into a loop and do something like (this is not necessarily code…):

for step=1 to 2 do
result = task[step](3)
end

Where the (3) is a value I am passing to the function.

I am going to assume this is not possible, so I would basically have to do some if/then's for keywords to call the correct function…am probably trying to get too cute with this?
flaghacker #2
Posted 17 May 2014 - 06:28 AM
This is possible:

data = {turtle.forward, turtle.left, turtle.select}
data[1]()       --turtle.forward()
data[3](5)     --turtle.select(5)