Hi,
I don’t know if this is a stupid question, but I can’t seem to find an answer.
Is there a way to convert a variable name to a string? i.e. can we print the variable name and not just it’s value?
For example:
@MattthewLXXIII Just to give you a quick answer, I would say no. When you pass a variable to a function, you’re not passing the variable name, you’re passing the memory address of that variable. Codea looks in it’s table of variables and finds the address associated with that variable. It then passes that address to the function and the function uses that address to get the value stored there. At this point I don’t know if there’s a way to search Codea’s table using the address to get the variable name. Does that sound like what you’re after because I’m not completely sure what you’re trying to do.
@MattthewLXXIII Can you give a better example of what you’re trying to do. Where are you getting or how are you getting the variable names that you’re passing to a function. It doesn’t sound like you’re hard coding them because if you are, you could just make the variable a string to begin with.
@dave1707 Yeah, I could, I was just being lazy. I will have a table of maps = {CLASSIC, PLAIN} et cetera. Then I’ll these variables to call functions to make the walls, like Map:maps[mapnumber]() where Map() is a class. In my settings I wanted to change mapnumber but showing the actual name of the map e.g. text(mapnames[mapnumber]), where mapnames = {"CLASSIC", "PLAIN"}, I just didn’t want to have to change both maps and mapnames every time I added a new map.
I guess I spent more time typing this out than I would have if I just did both XD.
EDIT: I just realised what I wanted should not be possible, sorry for wasting your time.
@MattthewLXXIII I’m not sure if this is what you’re trying to do. You would add new names to self.maps. Since you have to code those functions anyways, just add a print/text statement with their names.
function setup()
m=Map()
m:map(1)
m:map(2)
end
Map=class()
function Map:init()
self.maps={Map.CLASSIC,Map.PLAIN}
end
function Map:map(mapnumber)
print(self.maps[mapnumber]())
end
function Map:CLASSIC()
print("CLASSIC")
print("the function classic was executed")
end
function Map:PLAIN()
print("PLAIN")
print("the function plain as executed")
end
It was somewhat similar, I just didn’t want to have to write out "CLASSIC" for the print, the other stuff is pretty much what I’m going to have. Thanks for your help anyway.
maps = {CLASSIC=map[1],PLAIN=map[2]) --ie a named table
--now
CurrentMap=maps.CLASSIC --sets map[1] as current map
--if you create a property in the maps class containing the map name, then
--you can write the map name with
CurrentMap.name
@MattthewLXXIII I could understand your wanting to find a shortcut if you were adding hundreds of mapnames on a regular basis, but 10 mapnames isn’t a problem for 2 tables. Trying to find a solution for your question was interesting anyways. Thanks for asking.
@MattthewLXXIII I don’t know if you need this anymore, but this code prints the function name and executes that function using 1 table like you requested. I print the function name and then I use a print statement to show that the function is being executed.
EDIT: Changed original program to create nameTab in setup().
function setup()
tab={CLASSIC,PLAIN,NORMAL,SPECIAL}
nameTab={}
local str=readProjectTab("main")
for a in string.gmatch(str,"tab={(%g+)}") do
for b,_ in string.gmatch(a,"(%a+)([,]*)") do
table.insert(nameTab,b)
end
end
exec(1)
exec(4)
exec(2)
exec(3)
end
function exec(v)
print(nameTab[v])
tab[v]()
end
function CLASSIC()
print("classic function was called\
\
")
end
function PLAIN()
print("plain function was called\
\
")
end
function NORMAL()
print("normal function was called\
\
")
end
function SPECIAL()
print("special function was called\
\
")
end
@MattthewLXXIII I figured you wouldn’t. I just wanted to show that it could be done. Your question was a good challenge and I didn’t want to give up until I got it to work or was convinced it couldn’t be done.