Tables using strings vs numbers

So I initially had a table as follows

scenes = {

 [1] = {

  preDraw = function(self)

   --stuff that references self

  end

 };

 [2] = {

   postDraw = function(self)

   --stuff that references self

   end

 };

}

and it worked fine, but I decided to use strings instead of numbers, so imagine it as

scenes = {

 [“A”] = {

 };

 [“B”] = {

 }

}

with the same type of references. Now, I have a variable “activeScene” which in the first block was a number, and in the second is a string. When I call a function using the number form ( scenes[activeScene]:preDraw() ) it works fine, but when I do the same with the string form, it breaks. Does anybody have any idea why this is the case, and any suggestions? I would prefer to go with strings if possible, but if there is no solution I can work with numbers. Thanks!

Hi @thepixelatedplayer,

Just to be clear, in the first version, you were using a simple indexed table, but in the second version, you opted for a “dictionary” style table? You can definitely use the latter version, it’s just a matter of how you refer to the sub-table that you want to access.

What does an example code call to access one of your table entries look like?

Brent

if (scenes[activeScene].preDraw ~= nil) then scenes[activeScene]:preDraw() end

if (scenes[activeScene].text ~= nil) then textWrite(prevText…scenes[activeScene].text) end

if (scenes[activeScene].postDraw ~= nil) then scenes[activeScene]:postDraw() end

there’s an example. this is where it breaks as well when I use the dictionary style. the preDraw and postDraw are both as follows, and within the table. Also, activeScene is a string variable of the scene I am attempting to reference

preDraw = function(self)

 --stuff that uses self.variables

end;

Okay I figured it out. I rather simply just changed the variable for activeScene from a string to the scene itself (activeScene = scenes.Scene). Sorry for the trouble and thanks for the help! You definitely put me on the right track!

Hi @thepixelatedplayer,

Just to be clear, in the first version, you were using a simple indexed table, but in the second version, you opted for a “dictionary” style table? You can definitely use the latter version, it’s just a matter of how you refer to the sub-table that you want to access.

What does an example code call to access one of your table entries look like?

Brent

if (scenes[activeScene].preDraw ~= nil) then scenes[activeScene]:preDraw() end

if (scenes[activeScene].text ~= nil) then textWrite(prevText…scenes[activeScene].text) end

if (scenes[activeScene].postDraw ~= nil) then scenes[activeScene]:postDraw() end

there’s an example. this is where it breaks as well when I use the dictionary style. the preDraw and postDraw are both as follows, and within the table. Also, activeScene is a string variable of the scene I am attempting to reference

preDraw = function(self)

 --stuff that uses self.variables

end;

Okay I figured it out. I rather simply just changed the variable for activeScene from a string to the scene itself (activeScene = scenes.Scene). Sorry for the trouble and thanks for the help! You definitely put me on the right track!