Page 1 of 1

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

Posted: Wed Feb 10, 2016 2:15 pm
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!

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

Posted: Wed Feb 10, 2016 3:31 pm
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)

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

Posted: Wed Feb 10, 2016 3:33 pm
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) 
}

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

Posted: Wed Feb 10, 2016 3:34 pm
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!