Getting value from table problem

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Getting value from table problem

Post by PGUp » Mon Apr 24, 2017 1:57 pm

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 ?
-

User avatar
yetneverdone
Party member
Posts: 358
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Getting value from table problem

Post by yetneverdone » Mon Apr 24, 2017 2:03 pm

it should be enemystuff.enemy.y

MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Getting value from table problem

Post by MasterLee » Mon Apr 24, 2017 3:03 pm

yetneverdone wrote:
Mon Apr 24, 2017 2:03 pm
it should be enemystuff.enemy.y
For me it looks like it should be

Code: Select all

enemystuff[1].y
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

User avatar
yetneverdone
Party member
Posts: 358
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Getting value from table problem

Post by yetneverdone » Mon Apr 24, 2017 3:09 pm

Code: Select all

enemystuff[1].y
That also works, because in actuality it translates to

Code: Select all

enemystuff[enemy].y
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)

User avatar
zorg
Party member
Posts: 2909
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Getting value from table problem

Post by zorg » Mon Apr 24, 2017 4:07 pm

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 :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
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Getting value from table problem

Post by arampl » Mon Apr 24, 2017 5:19 pm

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.

User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm

Re: Getting value from table problem

Post by Sir_Silver » Mon Apr 24, 2017 5:24 pm

yetneverdone wrote:
Mon Apr 24, 2017 3:09 pm

Code: Select all

enemystuff[1].y
That also works, because in actuality it translates to

Code: Select all

enemystuff[enemy].y
No.

User avatar
yetneverdone
Party member
Posts: 358
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Getting value from table problem

Post by yetneverdone » Tue Apr 25, 2017 4:37 am

Sir_Silver wrote:
Mon Apr 24, 2017 5:24 pm
yetneverdone wrote:
Mon Apr 24, 2017 3:09 pm

Code: Select all

enemystuff[1].y
That also works, because in actuality it translates to

Code: Select all

enemystuff[enemy].y
No.

Okay i understand. Sorry for my mistakes.

User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm

Re: Getting value from table problem

Post by Sir_Silver » Tue Apr 25, 2017 5:03 am

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

Code: Select all

enemystuff[1].y
is not the same as this:

Code: Select all

enemystuff[enemy].y

User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm

Re: Getting value from table problem

Post by Sir_Silver » Tue Apr 25, 2017 5:09 am

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

Code: Select all

enemystuff[1].y
does not translate to this:

Code: Select all

enemystuff[enemy].y

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests