Page 1 of 1
Getting value from table problem
Posted: Mon Apr 24, 2017 1:57 pm
by PGUp
so, i want to get a value from a table, its hard to explain, this is the code :
function love.load ()
a = 10
enemystuff = {}
enemy = {}
enemy.y = 100
table.insert(enemystuff, enemy)
end
function love.update(dt)
if a > enemy.y then
(print something here)
end
end
so, i dont know how to get the enemy.y, using for loop make no sense, and it wont work.. so i have no idea at all how to do this, help ?
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 2:03 pm
by yetneverdone
it should be enemystuff.enemy.y
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 3:03 pm
by MasterLee
yetneverdone wrote: ↑Mon Apr 24, 2017 2:03 pm
it should be enemystuff.enemy.y
For me it looks like it should be
but there changes are high that the enemystuff is supposed to be inside enemy and not the other way round as it is at the moment
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 3:09 pm
by yetneverdone
That also works, because in actuality it translates to
well, the "enemy" table is inside the "enemystuff" because you inserted that way.
Code: Select all
table.insert(
--table to insert to,
--value to inser,
)
So you should try table.insert(enemy,enemystuff)
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 4:07 pm
by zorg
table.insert inserts things into the array part of a table, or in other words, enemystuff.enemy is nil, because enemy is not the key, it's the value, and enemystuff[1].y is the only correct way it will work.
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 5:19 pm
by arampl
I think this issue is not about tables. Your "enemy" table is global AND is also child of table "enemystuff". So enemy.y is correct expression.
But you asking if a > enemy.y i.e. if 10 > 100 which is obviously is never true.
Re: Getting value from table problem
Posted: Mon Apr 24, 2017 5:24 pm
by Sir_Silver
yetneverdone wrote: ↑Mon Apr 24, 2017 3:09 pm
That also works, because in actuality it translates to
No.
Re: Getting value from table problem
Posted: Tue Apr 25, 2017 4:37 am
by yetneverdone
Sir_Silver wrote: ↑Mon Apr 24, 2017 5:24 pm
yetneverdone wrote: ↑Mon Apr 24, 2017 3:09 pm
That also works, because in actuality it translates to
No.
Okay i understand. Sorry for my mistakes.
Re: Getting value from table problem
Posted: Tue Apr 25, 2017 5:03 am
by Sir_Silver
To elaborate, table.insert does not work the way you were mentioning before.
table.insert can take either two arguments- a table and a value to insert - or three arguments- a table, the numerical index to insert into, and the value to insert. When calling table.insert with only two arguments, it is functionally the same as calling table.insert with three arguments where the second argument is automatically the same as the length of the first table argument plus one.
This code:
Code: Select all
local enemystuff = {1, 2, 3, 4}
local enemy = {}
table.insert(enemystuff, enemy)
for k, v in pairs(enemystuff) do
print(k, v)
end
output ->
1 1
2 2
3 3
4 4
5 table: 0x2838b00
is functionally the same as this code:
Code: Select all
local enemystuff = {1, 2, 3, 4}
local enemy = {}
table.insert(enemystuff, #enemystuff + 1, enemy)
for k, v in pairs(enemystuff) do
print(k, v)
end
output->
1 1
2 2
3 3
4 4
5 table: 0x2838eb0
You cannot use table.insert to place a value into a table at any index that isn't a number, therefore after calling table.insert and inserting the enemy table into the enemystuff table, you will find that indexing the enemystuff table with the enemy will return nil.
Code: Select all
print(enemystuff[enemy])
output->
nil
tl:dr
This
is not the same as this:
Re: Getting value from table problem
Posted: Tue Apr 25, 2017 5:09 am
by Sir_Silver
To the OP, I don't really understand your question, do you mind clarifying?
To yetnevedone, table.insert does not work the way you were mentioning before.
table.insert can take either two arguments- a table and a value to insert - or three arguments- a table, the numerical index to insert into, and the value to insert. When calling table.insert with only two arguments, it is functionally the same as calling table.insert with three arguments where the second argument is automatically the same as the length of the first table argument plus one.
This code:
Code: Select all
local enemystuff = {1, 2, 3, 4}
local enemy = {}
table.insert(enemystuff, enemy)
for k, v in pairs(enemystuff) do
print(k, v)
end
output ->
1 1
2 2
3 3
4 4
5 table: 0x2838b00
is functionally the same as this code:
Code: Select all
local enemystuff = {1, 2, 3, 4}
local enemy = {}
table.insert(enemystuff, #enemystuff + 1, enemy)
for k, v in pairs(enemystuff) do
print(k, v)
end
output->
1 1
2 2
3 3
4 4
5 table: 0x2838eb0
You cannot use table.insert to place a value into a table at any index that isn't a number, therefore after calling table.insert and inserting the enemy table into the enemystuff table, you will find that indexing the enemystuff table with the enemy will return nil.
Code: Select all
print(enemystuff[enemy])
output->
nil
tl:dr
This
does not translate to this: