This is a question about lua, I've searched around the web but I didn't find any answer, so here's the question:
I would like to know how I could store a variable(1) inside a variable(2) so when I say variable(2)='foo' then variable(1) would equal 'foo' and variable(2) would equal variable(1) which equals 'foo'. (and all this should be inside a function )
player={}
function newPlayer(ppname, pname)
local n=#player+1
player[n]={}
player[n].name=pname
ppname=player[n]
end
newPlayer(p,'player 1')
print(p.name)
And that shoud've printed player 1 because:
1. ppname=p
2. ppname(which is p)=player[n]
3. p=player[n]
This doesn't seem to work but surely there is a way right ?
Thanks for reading
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping
player={}
function newPlayer(pname)
local n=#player+1
player[n]={}
player[n].name=pname
return player[n]
end
p = newPlayer('player 1')
print(p.name)
I would do like this.. I assume ppname was meant as kind of unique ID? Eventually it is all a bit weird.. do you know what kind of access you need to the player table? Maybe it would be better to skip the numeric indices and use the ppname as key? But then I dont know what you are planning, maybe this is not what you need.
player={}
function newPlayer(pname)
local n=#player+1
player[n]={}
player[n].name=pname
return player[n]
end
p = newPlayer('player 1')
print(p.name)
I would do like this.. I assume ppname was meant as kind of unique ID? Eventually it is all a bit weird.. do you know what kind of access you need to the player table? Maybe it would be better to skip the numeric indices and use the ppname as key? But then I dont know what you are planning, maybe this is not what you need.
Ohh, that did it, yep ppname is supposed to be a unique ID so whenever I need to call player[1] I would just write p
Using ppname as key isn't an option because I don't think 'for i,v in ipair(player)' would work without numbers.
All works know and for the time being this is what I need
Thanks for the help riidom
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping
if player.thatID == nil -- or player["thatID"] is same
or not
But that just as a sidenote, dont let me confuse you
Still there are a number of reasons why I am not going to use the UniqueID as the table key, but that b) point is actualy realy helpful, reeeeaaaly helpful thanks
riidom if you have more tips please say them, you are being a real help
I think I'm gonna store the UniqueIDs in a separeted table:
player={}
IDs={}
function newPlayer(pname, ID)
for i,v in ipairs(IDs) do
if v==ID then
alreadyinsuse=true
end
end
if alreadyinuse then
break
end
local n=#player+1
player[n]={}
player[n].name=pname
return player[n]
end
alreadyinuse=false
p = newPlayer('player 1', 'p')
if alreadyinuse then
print('ID already in use)
else
table.insert(IDs,'p')
print(p.name)
end
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping