Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
table question please - Gideros Forum

table question please

MoKaLuxMoKaLux Member
edited February 14 in General questions
I can't insert k, v in a table, could you please help me?
I have this code:
local world = r3d.World.new(0, gravityy, 0) -- 0, -9.8, 0
world.missiles = {}
 
stage:addEventListener(Event.ENTER_FRAME, function(e)
	if isshoot then shoot() end
end)
 
function shoot()
	local missile, missileobj = MISSILES(world, "3d_models/missiles", "missile03.obj", MISSILE_BIT, ENEMY_BIT)
	world.missiles[missile] = missileobj
	print(#world.missiles) -- TABLE ALWAYS EMPTY!
end
MISSILES returns missile and missileobj.
The table is always empty! Do you see anything wrong?
Thank you in advance for your help :)

Tagged:
zip
zip
yt_06_gideros_r3d_shmup.zip
55K
table.png
601 x 411 - 21K

Comments

  • hgy29hgy29 Maintainer
    edited February 14 Accepted Answer
    Nothing is wrong, but the '#' operator is not what you think, it only gives the length of an array (with keys being sequential numbers starting from 1). It doesn't work for hash maps.

    To check for a table emptyness use next(table) instead.
  • MoKaLuxMoKaLux Member
    edited February 14
    thank you hgy29. Still learning lua :) . I have just found out how ipairs work thanks to your sample code and now I can't live without it :)
  • hgy29hgy29 Maintainer
    Accepted Answer
  • rrraptorrrraptor Member
    Accepted Answer
    MoKaLux said:

    thank you hgy29. Still learning lua :) . I have just found out how ipairs work thanks to your sample code and now I can't live without it :)

    Also, ipairs(table) - iterate over number keys, pairs(table) - for any key.
    local t = {
    	a = 123, 
    	b = 321, 
    	[1] = "asd",
    	[2] = "LOL",
    	[7] = 45,
    	[{}] = "TABLE KEY"
    }
     
    for i,v in ipairs(t) do print(i,v) end
    print(string.rep("-", 8))
    for i,v in pairs(t) do print(i,v) end

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • MoKaLuxMoKaLux Member
    edited February 15
    MoKaLux said:

    thank you hgy29. Still learning lua :) . I have just found out how ipairs work thanks to your sample code and now I can't live without it :)

    I found my error: I wanted to use pairs instead of ipairs >:) debugging fun :)
  • MoKaLuxMoKaLux Member
    edited February 15
    cadeau :)

    a yt series should start shortly (God's willing).
    PS: I did all the 3d graphics this time (blender)
    PS2: having the best time of my life thanks to gideros <3
    r3d_shmup.png
    802 x 532 - 93K
    zip
    zip
    yt_06_gideros_r3d_shmup_x.zip
    246K
    +1 -1 (+2 / -0 ) Share on Facebook
Sign In or Register to comment.