Can I put a few functions into a table and then call them individually?
For instance tableA{} has 4 functions in it. My variable n changes depending on circumstances. I would then want to call a particular function from that table.
tableA[n]
I have tried it and it hasn’t worked. Is possible?
Quick answer yes. If someone doesn’t show you, I will soon.
It is very possible. Post your code, you pby have a syntaxt error.
God damnit Dave! You shot first again!
Lol!
@Circussmith Thy this.
function setup()
tab={a1,a2,a3}
for z=1,#tab do
tab[z]()
end
end
function a1()
print("function a1 executed")
end
function a2()
print("function a2 executed")
end
function a3()
print("function a3 executed")
end
@Jmv38 Honest, I’m not trying to beat you. I just see a question that needs to be answered, so I respond. Alot of times someone else answers while I’m putting together an example so I’ll skip the response or post the example if it’s different from what’s there.
@Circussmith - I have a couple of posts about this. It is a really cool feature.
http://coolcodea.wordpress.com/2013/06/14/84-a-practical-example-showing-the-value-of-classes/
(See reference in there to an earlier post as well).
Thank you guys. Dave1707 that worked and I think will help me in what I was trying to do. Ignatz, I really liked that post and will definitely read more them.
I am a complete beginner to coding, having never done it before. I have been teaching myself to use codea for just under two months now. So please excuse this if it is a stupid question.
Ignatz in your example on tables;
When you put in things like, Elf.walkSpeed=10 is that inserting that info into the table Elf?
Elf={} --define table
–store basic info about elves
Elf.walkSpeed=10
Elf.runSpeed=15
Elf.hitPoints=8 --damage
Elf.Eyesight=25 --distance seen in pixels
function Elf.Move(char)
–code follows to calculate new position
char.x=char.x+moveX --see explanation below
char.y=char.y+moveY
end
function Elf.Fight(char1,char2) --char2 is opponent
–code here
return hitPoints
end
Thanks again
dont feel ashamed. Everyone has to start somewhere.
You are correct. The following are equivalent:
MyTable[“a”] = b
MyTable.a = b
table.insert(MyTable,b,“a”)
Thanks Jmv, I appreciate that. I do have a follow up question. In Ignatz example he creates a table called Elf. Then he writes Elf.walkSpeed = 10. Now in that example is he putting walkSpeed into the table at position 10 or is the string walkSpeed = 10 being put in at the next open position in the table?
You should read the lua langage link in the wiki: it explains all that.
But i’ll do it anyway:
Tables have 2 series of indexes:
- first numbers 1,2, etc with no interruption. They are numbers.
- then any tag: a, toto, xxxx they are strings.
You access consecutive integer index with : for i,v in ipairs(mytable) do … End.
You access all indexes (integers and tags) with: for i,v in pairs(mytable) do … End
Notice the i is gone?
You must know: - indexes 1,2,3,7,8,9 stop at 3 with ipairs() because interruption.
- Indexes are in random order with pairs().
Thank you, I will read that. Your explanation was great. Thanks again.