Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Lua Help - tables - Gideros Forum

Lua Help - tables

I'm try to make an array of objects to hold my box2d objects, but I'm new to lua and not sure how to do this.

Here is an example of what I'm wanting to do.


local my_objects = {}
for i = 0, 9 do

my_objects[i].body = world:createBody(
my_objects[i].shape = b2.CircleShape.new(0,0, radius)

-- etc.

end

But I get the error: "attempt to index field '?' (a nil value)"

I assume it's something to do with my .body and .shape not being setup correctly but I'm not sure how to do it and also not even familar enough with Lua to know how to search google for an answer because I don't know what these things are... is my_objects a Table ... what is the .body part of the table, a property or a field?

Thanks in advanced!
Tagged:

Comments

  • edited December 2018
    You can try something like that
    Coming soon
  • local my_objects = {}
    for i = 0, 9 do
     
    my_objects[i] = {
    body = world:createBody()
    , shape = b2.CircleShape.new(0,0, radius)
    }
     
    -- etc.
    -- or init and assign --
    --my_objects[i] = {}
    --my_objects[i].body = world:createBody()
    --my_objects[i].shape = b2.CircleShape.new(0,0, radius)
    end
    Sorry i do not want to spam but edit reply make code unreadable
    Coming soon
  • Also, lua tables generally start from index of 1, not 0.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+2 / -0 ) Share on Facebook
  • Ah yes like @SinisterSoft notice, for loop should change to
    for i = 1, 10 do

    or if you like to keep for i = 0, 9 do , you can using my_objects[#my_objects + 1] as the index inside the loop. But "for i = 1,10 do" is more natural and readable so i comment you using that way.
    @hgy29 each time I edit code snippets, it generates so much span and destroys the format. I insert the code by using the symbol from the editor and then choose "code" from the drop-down.

    Likes: SinisterSoft

    Coming soon
    +1 -1 (+1 / -0 ) Share on Facebook
  • @vitalitymobile when adding/editing code to the forum it seems to mess up on the initial preview - but once it refreshes it looks ok - at least it does for me.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.