Can I Make Dynamic Properties for Display Objects?

Let’s say I create a display object like this:

[lua]local alien = display.newImage(“alien.png”)[/lua]

…and then later I read in the contents of a text file to a local variable:

[lua]local txt = readFile(“customVars.txt”)[/lua]

For the sake of this example, let’s say the entire contents of that text file are: name = "Bob"

Question…

Is there a way for me to “attach” that content to the existing display object so it now has a property called name with a value of Bob?

So if I do this:

[lua]print(alien.name)[/lua]

…I’d see Bob in the console.

I’ve poked at this for a while and either I’m missing something or it can’t be done. I’d prefer the former, even if it makes me go, “D’oh!” and be embarrassed at how easy it is.

Anyone?

 Jay

You’ll want to read in the contents of the text file into a Lua table.  Then you can assign alien.name to the index for name in the table. 

Hmmm…but I won’t know the name of the property. While it may very well be “name” I can’t do this:

[lua]alien.name = txtTable[name][/lua]

…because the property could be foo, or bar, or anything else. It’s like I need to be able to do:

[lua]alien.eval(txtTable[1].key) = txtTable[1].val[/lua]

…or something like that. Know what I mean? (Or maybe I’m still making this harder than I need to…)

What I’m trying to do is to allow using a Gumbo source file directly in a Corona game and Gumbo allows you to add “extra code” that is used right after the display object is created. I don’t actually have to add it as a property/function to the object itself, I just need to be able to execute it, and some of the time it will be properties that should be added to the object.

Maybe I’ll go browse through the Code Exchange and see if I can find someone who’s done something similar.

 Jay

You can still read the values into a table and then use a loop to assign the properties like this:

for k,v in pairs txtTable do   aliens[k] = v end 

Just be sure not to create properties with names that overlap Corona property/field names.

In fact, I would actual suggest this instead:

  1. Read in json encoded table (or other format if you perfer).

  2. json decode the table

  3. Assign the table to your ‘aliens’ object under the filed ‘_params’ or some other name that works for you.

Tip: If you are assigning the same table to multiple aliens, then assign a copy of the table to ‘_params’.  You can make a copy with ‘shallowcopy’ from here:  http://lua-users.org/wiki/CopyTable

-Ed

On a side note, you can’t execute any code dynamically.  All the dynamic Lua evaluation functions are removed from the Lua that comes bundled with Corona.  This is because iOS doesn’t allow scripting in their apps (although anyone who wants to do it can get around this limitation by making their own state machine or pseudo-interpreter.

-Ed

You’ll want to read in the contents of the text file into a Lua table.  Then you can assign alien.name to the index for name in the table. 

Hmmm…but I won’t know the name of the property. While it may very well be “name” I can’t do this:

[lua]alien.name = txtTable[name][/lua]

…because the property could be foo, or bar, or anything else. It’s like I need to be able to do:

[lua]alien.eval(txtTable[1].key) = txtTable[1].val[/lua]

…or something like that. Know what I mean? (Or maybe I’m still making this harder than I need to…)

What I’m trying to do is to allow using a Gumbo source file directly in a Corona game and Gumbo allows you to add “extra code” that is used right after the display object is created. I don’t actually have to add it as a property/function to the object itself, I just need to be able to execute it, and some of the time it will be properties that should be added to the object.

Maybe I’ll go browse through the Code Exchange and see if I can find someone who’s done something similar.

 Jay

You can still read the values into a table and then use a loop to assign the properties like this:

for k,v in pairs txtTable do   aliens[k] = v end 

Just be sure not to create properties with names that overlap Corona property/field names.

In fact, I would actual suggest this instead:

  1. Read in json encoded table (or other format if you perfer).

  2. json decode the table

  3. Assign the table to your ‘aliens’ object under the filed ‘_params’ or some other name that works for you.

Tip: If you are assigning the same table to multiple aliens, then assign a copy of the table to ‘_params’.  You can make a copy with ‘shallowcopy’ from here:  http://lua-users.org/wiki/CopyTable

-Ed

On a side note, you can’t execute any code dynamically.  All the dynamic Lua evaluation functions are removed from the Lua that comes bundled with Corona.  This is because iOS doesn’t allow scripting in their apps (although anyone who wants to do it can get around this limitation by making their own state machine or pseudo-interpreter.

-Ed