Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can you put functions into a table?

edited August 2013 in Questions Posts: 22

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.
I have tried it and it hasn't worked. Is possible?



  • dave1707dave1707 Mod
    Posts: 7,809

    Quick answer yes. If someone doesn't show you, I will soon.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    It is very possible. Post your code, you pby have a syntaxt error.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    God damnit Dave! You shot first again!

  • dave1707dave1707 Mod
    edited August 2013 Posts: 7,809

    @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
  • dave1707dave1707 Mod
    Posts: 7,809

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

  • IgnatzIgnatz Mod
    edited August 2013 Posts: 5,396

    @Circussmith - I have a couple of posts about this. It is a really cool feature.

    (See reference in there to an earlier post as well).

  • Jmv38Jmv38 Mod
    Posts: 3,295

    @Dave1707 i know! Just find this quite funny!

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

    function Elf.Fight(char1,char2) --char2 is opponent
    --code here
    return hitPoints

    Thanks again

  • Jmv38Jmv38 Mod
    Posts: 3,295

    dont feel ashamed. Everyone has to start somewhere.
    You are correct. The following are equivalent:
    MyTable["a"] = b
    MyTable.a = b

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

  • Jmv38Jmv38 Mod
    Posts: 3,295

    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.

Sign In or Register to comment.