View Single Post
Old 06-22-2013, 09:54 PM   #5
squeek.
Stuff Do-er
Lua Team
Wiki Team
Fortress Forever Staff
 
squeek.'s Avatar
 
Join Date: Mar 2007
Location: Northern California
Class/Position: Rallygun Shooter
Gametype: Conc tag (you just wait)
Affiliations: Mustache Brigade
Posts Rated Helpful 352 Times
Send a message via AIM to squeek.
[] and . aren't very distinct in Lua, because tables are so flexible (they are used for what would be both arrays and structs in C++). For example, in Lua, table["key"] is the same as table.key. The only reason to use one over the other is if you use a variable that holds the key.

Code:
-- I know the name of the property
local val1 = table.property

-- The name of the property is stored in a variable
local propertyname = "property"
local val2 = table[ propertyname ]
In C++, there is a major difference, because arrays and structs are distinct. Arrays can only have numerical indexes; array["key"] is invalid. Conversely, structs can not use []. To access any properties of structs or classes, you use struct.propertyname (or -> if using a pointer); struct["propertyname"] is invalid.

classstats is an array of structs, so you index into the array using [] and then access the properties of the struct at that index using .

Another way to think about how to access what you want is evaluate things piece by piece. Things inside a [] will get evaluated first and then the result will be used as the index.
  • classstats[ classnum ].propertyname = get the property "propertyname" of the struct at index classnum in the array classstats
  • classstats[ classnum.propertyname ] = get the property "propertyname" of the struct/class classnum (not a struct/class; will generate a compile error), then use that property as the index to get the struct in the array classstats
  • classstats[ classnum[propertyname] ] = get the variable at the index propertyname of the array classnum (not an array; will generate a compile error), then use that variable as the index to get the struct in the array classstats
__________________
#FF.Pickup ยค Fortress-Forever pickups

My Non-official Maps
Released FF_DM_Squeek - FF_2Mesa3_Classic - FF_Siege_Classic
Beta FF_Myth - FF_Redlight_Greenlight

Sick of the people on the internet, always moanin'. They just moan.
- Karl Pilkington

Last edited by squeek.; 06-22-2013 at 10:05 PM.
squeek. is offline   Reply With Quote