Converting variable name to a string

Hello -
What would you recommend be the best way to do the following:

I have a requirement of using an existing variables’ name as a string in the Lua code.

Currently, I am forced to have two tables with as such:

[lua]table1 = { variable1, variable2, variable3 }
table2 = { “variable1”, “variable2”, “variable3” }

Is there a way to combine the two so I don’t have the above duplication? I realize that the second table contains a list of strings whereas the first contains a list of variables.

However, both have the same name, as you can see.

Is there some way in Lua to just have one table (In this case, table1) and then convert the variable name to a string as necessary?

I have tried the “tostring” function but wasn’t successful with it.

Thank you. [import]uid: 32833 topic_id: 6960 reply_id: 306960[/import]

Updated entry (Tried modifying the original post but unable to do so. Here it is again with the correct Lua code formatting):

Hello -
What would you recommend be the best way to do the following:

I have a requirement of using an existing variables’ name as a string in the Lua code.

Currently, I am forced to have two tables with as such:

[lua]table1 = { variable1, variable2, variable3 }
table2 = { “variable1”, “variable2”, “variable3” }[/lua]

Is there a way to combine the two so I don’t have the above duplication? I realize that the second table contains a list of strings whereas the first contains a list of variables.

However, both have the same name, as you can see.

Is there some way in Lua to just have one table (In this case, table1) and then convert the variable name to a string as necessary?

I have tried the “tostring” function but wasn’t successful with it.

Thank you. [import]uid: 32833 topic_id: 6960 reply_id: 24364[/import]

this might help

[lua]table1 = {}

table1[“myVar”]=999
print(table1.myVar) – 999

table1.myVar=123
print(table1[“myVar”]) – 123

local varString=“myVar”
table1[varString]=456
print(table1.myVar) – 456

table1.myVar = 789
print(table1[“my”…“Var”]) – 789[/lua]

it doesnt answer your question about being able to convert a variable name to string but it will probably solve your problem [import]uid: 6645 topic_id: 6960 reply_id: 24367[/import]

Thank you very much, jmp909! [import]uid: 32833 topic_id: 6960 reply_id: 24370[/import]

Hi jmp909 -

Here is some more additional info as to what I am trying to do:

[lua]text1 = “Some Variable”
text2 = “Some Other Variable”
text3 = “Yet Another Variable”
d = {}

local textTable1 = { text1, text2, text3 } – Table of variables
local textTable2 = { “text1”, “text2”, “text3” } – Table of labels for above variables

– This is the listener for the display Text object
local function textTouch(event)
local id = event.target.id
if(event.phase == “began”) then
transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0})
d = text[id] – As a test, this should save the variable (Not text label)
elseif(event.phase == “moved”) then
transition.to( text[id]:setTextColor(255,255,55), { time=50, alpha=1.0 })
elseif(event.phase == “ended”) then
transition.to( text[id]:setTextColor(255,255,255), { time=100, alpha=1.0 })
end
end

– Create text display objects that have listeners for touch events
text = {}
for k, v in ipairs(textTable2) do
local myText = display.newText( v, m, n, “Helvetica”, 16 )
myText:setReferencePoint(display.TopLeftReferencePoint)
myText.id = k
myText:setTextColor(255,255,255)
myText:addEventListener(“touch”, textTouch)
text[k] = myText
end[/lua]

Right now I have two tables:
textTable1 has a list of the actual variables.
textTable2 has a list of the labels for these variables.

I then use ipairs to iterate through textTable2 and create 3 text display Objects with listeners that respond to touch events.

Finally, under function textTouch, I have a test table that should save the variable name and not the text name as a test condition (table d). However, this is not occurring.

So as a result, I have to have 2 tables - textTable1 and textTable2. This isn’t really a big deal, but I was hoping for a more “elegant” solution with one table since the names are identical (The only thing being different is that one is a set of variables and the other is a set of strings).

Thanks! [import]uid: 32833 topic_id: 6960 reply_id: 24372[/import]

but you never use textTable1 in your code after defining it [import]uid: 6645 topic_id: 6960 reply_id: 24464[/import]

Sorry - You’re right.

Please replace this line:
[lua]d = text[id][/lua]

…with:

[lua]d(textTable1[id]) – Where d is a function[/lua]

I think I am making the code more complicated than it needs to be :slight_smile:

I’ll go back and review everything. [import]uid: 32833 topic_id: 6960 reply_id: 24468[/import]

yes you are :slight_smile: what are you actually trying to do? [import]uid: 6645 topic_id: 6960 reply_id: 24488[/import]

What I am trying to do is:

  • Create 3 text display objects.
  • Assign touch events to these 3 text display objects.
  • When a text object receives a touch event, it sends its name which is also the name of a variable to a function for processing some action.

For example:
I have these 3 text objects.

  • Bird
  • Cat
  • Dog

Bird, Cat, and Dog are also variables.

[lua]bird = “Say Hello”; cat = “Meow”; dog = “Woof”[/lua]

So when the text display object bird receives a touch event, it uses that text display object’s label to access the variable of the same name.

i.e. If I press “Bird”, it sends the word bird to a function and the function does something with the variable of the same name (e.g. print(bird))

Thanks for your help and insights!
[import]uid: 32833 topic_id: 6960 reply_id: 24495[/import]

at the risk of confusing you :wink:

main.lua
[lua]local Word = require(“Word”)

words = {}

words[1] = Word.new(“Dog”, “Woof”, 100,100)
words[2] = Word.new(“Cat”, “Meow”, 150,100)
words[3] = Word.new(“Bird”, “Tweet”, 200,100)

words[1]:speak()

print(words[1].hasSpoken) – true
print(words[2].hasSpoken) – false[/lua]

Word.lua
[lua]module(…, package.seeall)

Word = {}
Word_mt = { __index = Word }

– functions
local textTouch
function new(name, speech, x, y)

local self = {}

local myText = display.newText( name, x, y, “Helvetica”, 16 )
myText:setReferencePoint(display.TopLeftReferencePoint)
myText:setTextColor(255,255,255)
myText:addEventListener(“touch”, textTouch)

– set reference back to “word” object (self) so events can reference it
myText._parent = self;
self._name = name
self._speech = speech
self._text = myText
self.hasSpoken=false

setmetatable(self, Word_mt)
return self

end

function Word:speak()

print(self._speech…" “…self._speech…”! My name is "…self._name)
self._text.alpha=0.5
self.hasSpoken=true
end
function textTouch(event)

local word = event.target._parent

if(event.phase==“began”) then

print("touched "…tostring(word))
print("name is "…word._name)
print("speech is "…word._speech)

word:speak()
end
end[/lua] [import]uid: 6645 topic_id: 6960 reply_id: 24527[/import]

Wow - Awesome.
Thank you!

The Force is strong with you, Master Jedi. [import]uid: 32833 topic_id: 6960 reply_id: 24542[/import]

And just as a final comment - Any recommendations on how to someday get to where you are? :slight_smile:

i.e. Studying the code you provided is one way - Thanks!

Do you have any other recommendations? Just trial and error? I’ve been reading the programming Lua book, or attempting to.

I see this will take many years to get good - But a very nice journey! [import]uid: 32833 topic_id: 6960 reply_id: 24544[/import]

program flash for 7 years. spend a few weeks learning Lua/Corona. read the docs & forums. trial and error… even making that example for you i got it wrong 3 times. there’s modules and there’s metatables, then there’s mixing the two like i’ve done, and i’ve still not got the difference totally nailed yet. I do know that metatables save memory because you’re not adding the functions to every instance of your object. just the master that they are derived from.

i still haven’t standardized my coding practices yet. i just make something that works then improve it later. but largely based on what other people are doing too. download all the the code samples like Beebe games’ stuff. and open up all the code in the Corona samples and see how they do it.

j [import]uid: 6645 topic_id: 6960 reply_id: 24548[/import]

That’s a pretty clear and simple example of how to use metatables, thanks. If I ever need to need to save memory this is a good example to refer to; whether or not to use metatables appears to be a tradeoff between memory and execution speed:
http://developer.anscamobile.com/forum/2011/01/21/oop-game-classes-objects-inheritance-methods-properties [import]uid: 12108 topic_id: 6960 reply_id: 24969[/import]

This will help me alot…Thanks jmp909 [import]uid: 41322 topic_id: 6960 reply_id: 42245[/import]