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 .
Otoris
Prole
Posts: 5 Joined: Thu Mar 15, 2012 6:38 am
Post
by Otoris » Fri Mar 16, 2012 8:47 pm
I have a table.
It has some child tables.
I have a function that sets and prints the table
Code: Select all
function tblscan()
x = b
print(tbl.x)
end
This currently returns nil. Any idea how to make this work without setting x = tbl.b?
trubblegum
Party member
Posts: 192 Joined: Wed Feb 22, 2012 10:40 pm
Post
by trubblegum » Fri Mar 16, 2012 9:05 pm
What you're doing there does nothing.
I assume you're trying to do something like :
Code: Select all
t = {}
t.a, t.b, t.c = 1, 2, 3
function fetchvalue(x) return t[x] end
print fetchvalue('a')
It's usually called dynamic referencing.
You should probably read over this :
http://lua-users.org/wiki/TablesTutorial
And look up function arguments too.
Otoris
Prole
Posts: 5 Joined: Thu Mar 15, 2012 6:38 am
Post
by Otoris » Fri Mar 16, 2012 9:25 pm
Thanks for the helpful answer trubblegum!
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Fri Mar 16, 2012 10:48 pm
That seems a bit unnecessary. Try this:
Code: Select all
function tblscan()
x = "b"
print(tbl[x])
end
meoiswa
Prole
Posts: 48 Joined: Wed Mar 14, 2012 8:13 pm
Location: Venezuela
Post
by meoiswa » Mon Mar 19, 2012 7:14 pm
Are you tying to do something like this?
Code: Select all
table = {}
table.a = "The first"
table.b = "The third"
table.c = "Da fudge?"
function printThis(str)
if table[str] then
print(table[str])
else
print("There is no value at intex "..str.." in the table")
end
end
printThis("a") -- > "The first"
printThis("c") -- > "Da fudge?"
Write the ö of Löve with Alt+numPad(148) or Alt+numPad(153)
Users browsing this forum: Amazon [Bot] , Google [Bot] and 4 guests