When creating a table, how can I access the value of the table while inside

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
ismyhc
Prole
Posts: 5
Joined: Tue Nov 20, 2012 4:01 am

When creating a table, how can I access the value of the table while inside

Post by ismyhc »

New lua user here. When creating a table, how can I access the value of a table while inside of the table?

Heres what Im trying to do:

Code: Select all


cool_table = {

	size = {
		w = 10,
		h = 10
	}
	
	quad = AssetManager:getQuad(size.w, size.h) -- < id like to access the size table here, but im not sure how
}

Any help much appreciated!
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: When creating a table, how can I access the value of the table while inside

Post by s-ol »

ismyhc wrote:New lua user here. When creating a table, how can I access the value of a table while inside of the table?

Heres what Im trying to do:

Code: Select all


cool_table = {

	size = {
		w = 10,
		h = 10
	}
	
	quad = AssetManager:getQuad(size.w, size.h) -- < id like to access the size table here, but im not sure how
}

Any help much appreciated!
that's impossible, the table doesn't exist until all of the statement id done. You can do this instead:

Code: Select all

cool_table = {}
cool_table.size = { w = 10, h = 10 }
cool_table.quad = AssetManager:getQuad(cool_table.size.w, cool_table.size.h)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: When creating a table, how can I access the value of the table while inside

Post by ivan »

Either:

Code: Select all

cool_table = {
   size = {
      w = 10,
      h = 10
   }
}
cool_table.quad = AssetManager:getQuad(cool_table.size.w, cool_table.size.h)
or

Code: Select all

local _w,_h = 10,10
cool_table = {
   size = {
      w = _w,
      h = _h
   },
   quad = AssetManager:getQuad(_w, _h) 
}
Since 10 is hard-coded in your example you can also do:

Code: Select all

cool_table = {
   size = {
      w = 10,
      h = 10
   },
   quad = AssetManager:getQuad(10, 10) 
}
Last edited by ivan on Wed Feb 10, 2016 3:35 pm, edited 1 time in total.
ismyhc
Prole
Posts: 5
Joined: Tue Nov 20, 2012 4:01 am

Re: When creating a table, how can I access the value of the table while inside

Post by ismyhc »

S0lll0s wrote:
ismyhc wrote:New lua user here. When creating a table, how can I access the value of a table while inside of the table?

Heres what Im trying to do:

Code: Select all


cool_table = {

	size = {
		w = 10,
		h = 10
	}
	
	quad = AssetManager:getQuad(size.w, size.h) -- < id like to access the size table here, but im not sure how
}

Any help much appreciated!
that's impossible, the table doesn't exist until all of the statement id done. You can do this instead:

Code: Select all

cool_table = {}
cool_table.size = { w = 10, h = 10 }
cool_table.quad = AssetManager:getQuad(cool_table.size.w, cool_table.size.h)
Well that explains it! :awesome:

Thanks for the help and quick reply!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests