Getting value from table problem
Getting value from table problem
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 ?
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 ?
-
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Getting value from table problem
it should be enemystuff.enemy.y
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Re: Getting value from table problem
For me it looks like it should be
Code: Select all
enemystuff[1].y
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Getting value from table problem
That also works, because in actuality it translates toCode: Select all
enemystuff[1].y
Code: Select all
enemystuff[enemy].y
Code: Select all
table.insert(
--table to insert to,
--value to inser,
)
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Getting value from table problem
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.
Me and my stuff True 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.
Re: Getting value from table problem
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.
But you asking if a > enemy.y i.e. if 10 > 100 which is obviously is never true.
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: Getting value from table problem
No.yetneverdone wrote: ↑Mon Apr 24, 2017 3:09 pmThat also works, because in actuality it translates toCode: Select all
enemystuff[1].y
Code: Select all
enemystuff[enemy].y
- yetneverdone
- Party member
- Posts: 448
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Getting value from table problem
Sir_Silver wrote: ↑Mon Apr 24, 2017 5:24 pmNo.yetneverdone wrote: ↑Mon Apr 24, 2017 3:09 pmThat also works, because in actuality it translates toCode: Select all
enemystuff[1].y
Code: Select all
enemystuff[enemy].y
Okay i understand. Sorry for my mistakes.
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: Getting value from table problem
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:
is functionally the same as this code:
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.
tl:dr
This is not the same as this:
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
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
Code: Select all
print(enemystuff[enemy])
output->
nil
This
Code: Select all
enemystuff[1].y
Code: Select all
enemystuff[enemy].y
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: Getting value from table problem
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:
is functionally the same as this code:
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.
tl:dr
This does not translate to this:
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
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
Code: Select all
print(enemystuff[enemy])
output->
nil
This
Code: Select all
enemystuff[1].y
Code: Select all
enemystuff[enemy].y
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 8 guests