Question for tables and variables :3

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.
User avatar
Volgoza
Prole
Posts: 26
Joined: Fri Jul 13, 2012 12:20 pm

Question for tables and variables :3

Post by Volgoza »

Hello.

Someone help me.
Can i use variable for function table.insert?

Example:

Code: Select all

Table = {}

SomeTable = {}
SomeTable.var = "Table"

function love.mousepressed(x, y, button)
if button == "l" then
table.insert (SomeTable.var, 1)
end
end
That is to substitute the variable as the table name?

Sorry for my english :3 And ty for answer.
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Question for tables and variables :3

Post by Snuux »

In your example, SomeTable.var has String type.
table.insert (SomeTable.var, 1) - first parameter must be Table - not String...
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question for tables and variables :3

Post by Robin »

You need to change one line:

Code: Select all

SomeTable.var = Table
Help us help you: attach a .love.
User avatar
Volgoza
Prole
Posts: 26
Joined: Fri Jul 13, 2012 12:20 pm

Re: Question for tables and variables :3

Post by Volgoza »

Then it will be and if the variable assignment table does not exist, then do not get anything. I already tried.
Or I did not understand you.

But i did:

https://love2d.org/imgmirrur/ykNXPAr.jpg

where vv.name its - BigCrystal or SmallCrystal1 or SmallCrystal2.

Understand or no?

If you know, how i can get it easy, i would appreciate :3
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Question for tables and variables :3

Post by Robin »

Volgoza wrote:If you know, how i can get it easy, i would appreciate :3
I'm afraid I don't understand what you're saying.
Help us help you: attach a .love.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Question for tables and variables :3

Post by Ranguna259 »

What's your native language ?
Last edited by Ranguna259 on Mon Apr 14, 2014 4:29 pm, edited 1 time in total.
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
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: Question for tables and variables :3

Post by Snuux »

His native language - is Russian. I told him, that it's code is little strange...
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31
(Sorry for my english. I learn it myself, and I don't have enough experience)
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Question for tables and variables :3

Post by zorg »

i could be wrong, but maybe Volgoza wants this:

Code: Select all

table.insert(someTable[var], 1)
Basically, if you do the dot syntax like you did before, it will interpret that as the string "var", or more visually, as:

Code: Select all

someTable['var'] -- is the same as: someTable.var
Note that this is how it works everywhere, not just with table.insert.

Edit: I still think my version is what he wants, though he needs to do

Code: Select all

SomeTable.var = Table
instead, without the quotes around Table.
Last edited by zorg on Tue Apr 15, 2014 3:47 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Question for tables and variables :3

Post by Ranguna259 »

OOH now I see what he wants :crazy: (maybe)
I think he wants to add something to the "Table" table throught the "SomeTable" table, like so:

Code: Select all

table1 = {}
table2={table1}

table2[1].a = 1
--and so:
table1.a == 1
A pointer of some sort ?
Well I think that that is impossible, correct me if I'm wrong but when you create a table using another table you'll just be creating another table that is completly isolated from any other table. (table, table, table, table, table,)
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
moikmellah
Prole
Posts: 12
Joined: Fri Jan 31, 2014 8:31 pm
Location: USA
Contact:

Re: Question for tables and variables :3

Post by moikmellah »

Perhaps something like this is what the OP is looking for?

Code: Select all

Table = {}

SomeTable = {}
SomeTable.var = "Table"

function love.mousepressed(x, y, button)
  if button == "l" then
    local myTable = _G[SomeTable.var]
    table.insert (myTable, 1)
  end
end
That allows you to reference a variable by name through the global table _G (scope allowing, of course). Whether it's a wise idea is up for debate, but I think that's what the poster is going for.

(source: PIL 14.1)
Post Reply

Who is online

Users browsing this forum: No registered users and 194 guests