It looks like you're new here. If you want to get involved, click one of these buttons!
local ggameResourceTypes= {
"GAMERESOURCE_COAL",
"GAMERESOURCE_OIL",
"GAMERESOURCE_GARBAGE",
"GAMERESOURCE_NUCLEAR"
}
self.ggameResources= {}
for indx,gameResourceType in ipairs(ggameResourceTypes) do
self.ggameResources[gameResourceType]= "joe"
end
print(#self.ggameResources)
Why does this print 0?
Thanks
Comments
Because you are creating a new table indexed by strings. The
#
operator only works on the numerical part of a table. So:has
#a
equal to 3 as the indices default to integers, buthas
#a
equal to 0 as the indices are strings.(Incidentally, to format a code block, put
~~~
either side of it, see the FAQ post for more details.)Thank you! Wasted a whole hour train ride staring at it. Explains the failure of ipairs() as well.
Thanks