String inside a variable.. inside a variable (variable^2)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

String inside a variable.. inside a variable (variable^2)

Post by Ranguna259 »

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 :3 )

Here's an exemple code:

Code: Select all

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 :D

Check out my twitter.
User avatar
riidom
Citizen
Posts: 74
Joined: Wed Jun 19, 2013 4:28 pm
Location: irgendwo an der Elbe
Contact:

Re: String inside a variable.. inside a variable (variable^2

Post by riidom »

Code: Select all

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.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: String inside a variable.. inside a variable (variable^2

Post by Ranguna259 »

riidom wrote:

Code: Select all

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 :awesome:
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
riidom
Citizen
Posts: 74
Joined: Wed Jun 19, 2013 4:28 pm
Location: irgendwo an der Elbe
Contact:

Re: String inside a variable.. inside a variable (variable^2

Post by riidom »

let me still add a comment .. or two, if I think about it:
a) you can iterate over a table that has strings as keys with

Code: Select all

for k, v in pairs(tbl) --just without "i"
b) you can quickly check if a playerID is already in use, if you check

Code: Select all

if player.thatID == nil -- or player["thatID"] is same
or not

But that just as a sidenote, dont let me confuse you :)
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: String inside a variable.. inside a variable (variable^2

Post by Ranguna259 »

riidom wrote:let me still add a comment .. or two, if I think about it:
a) you can iterate over a table that has strings as keys with

Code: Select all

for k, v in pairs(tbl) --just without "i"
b) you can quickly check if a playerID is already in use, if you check

Code: Select all

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 :ultrahappy:

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:

Code: Select all

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 :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], MrFariator and 67 guests