Referring to custom properties by name

Hi, I’m looking for a way to refer to custom properties by name stored in a variable.

e.g. 

local object = {} local propertyname = "health" object.health = 50 -- the normal way object.\<propertyname\> = 50 -- the way I need

The reason I’m doing it is because I have  player object with multiple dynamically selected named properties (I could put them in a numbered table (object.properties[5] = 100) but shortly speaking this would make everything less readable.

Is there a way to do this? Is it good practice or somewhat ‘hacky’ ?

Tables in Lua can be indexed both with integers or string indexers. Integer indexers are done like this:

object[12]

To get the 12th value.

String indexers can be used just like integer indices, but also as properties - as you have above. So, where you have <propertyname> you would simply replace the ‘12’ above with the variable or string literal…

object.health = 50 -- accessed by property object["health"] = 50 -- accessed by string literal indexer object[propertyname] = 50 -- accessed by variable string indexer

@horacebury Neat, I wasn’t aware you could use strings as indexes. Thanks!

Tables in Lua can be indexed both with integers or string indexers. Integer indexers are done like this:

object[12]

To get the 12th value.

String indexers can be used just like integer indices, but also as properties - as you have above. So, where you have <propertyname> you would simply replace the ‘12’ above with the variable or string literal…

object.health = 50 -- accessed by property object["health"] = 50 -- accessed by string literal indexer object[propertyname] = 50 -- accessed by variable string indexer

@horacebury Neat, I wasn’t aware you could use strings as indexes. Thanks!