06-20-2013, 09:42 PM | #1 | |
worst ff player eu
Join Date: Jun 2012
Location: South Yorks., England
Class/Position: o - no, d - scout Gametype: IvDZ Posts Rated Helpful 18 Times
|
Boo, I suck with C++ D:
Gah.
How do I access something inside a table inside a table? Table: Code:
static struct classstats { int speed, weapon1, weapon2, class_maxhealth; const char *name; } classstats[]= { { 1.3, 6, 0, 70, "Scout" }, { 1, 4, 0, 80, "Sniper" }, { 0.7, 3, 1, 120, "Soldier" }, { 0.9, 5, 6, 90, "Demoman" }, { 1.1, 1, 6, 90, "Medic" }, { 0.65, 2, 1, 140, "Heavy Weapons" }, { 1, 1, 6, 100, "Pyro" }, { 1, 6, 0, 80, "Spy" }, { 1, 1, 6, 80, "Engineer" }, { 0.65, 0, 0, 70, "Civilian" } }; Code:
if(m_fortress) { int wep1 = classstats[classnum[weapon1]]; int wep2 = classstats[classnum[weapon2]]; ammo[wep1] = (itemstats[wep1[add]]*2); ammo[wep2] = (itemstats[wep2[add]]*2); armour = 0; maxhealth = classstats[classnum[class_maxhealth]]; health = maxhealth; } L: When attempting to compile, I get: Quote:
I just replaced the definitions with their values. Hence the 0-6's in the 'weapon1' and 'weapon2' slots. Yeah, if you haven't noticed yet (you probably have), I've been trying to make a Fortress mod for Cube 2. While teaching myself how to use C++ with it's source code. Also, I assume this should go in the 'tech' thread, 'cause there's no other place for it :L Damn, this post keeps getting longer. I used the values of health and speed in FF, if you were wondering, and changed them using various formulae to be more suitable for Cube 2. L:
__________________
gg ff not ded ff very much alive |
|
|
06-21-2013, 03:05 AM | #2 |
internet user
Fortress Forever Staff
Join Date: Jun 2007
Posts Rated Helpful 42 Times
|
int wep1 = classstats[classnum[weapon1]];
int wep2 = classstats[classnum[weapon2]]; ammo[wep1] = (itemstats[wep1[add]]*2); ammo[wep2] = (itemstats[wep2[add]]*2); armour = 0; maxhealth = classstats[classnum[class_maxhealth]]; health = maxhealth; things in bold don't exist, its expecting something that evaluates to integer index which i assume classnum contains,not the name of the struct field. its very confusing tho, are you trying something like this? int classThatKilledMe = 0; classstats[classThatKilledMe ].weapon1 -> evaluates to 6 (scout weapon1)
__________________
9:17 PM - DEXTER: can you teach me how to play o 9:17 PM - squeek.: you jump a lot 9:18 PM - squeek.: and do sweet moves 8:55 PM - FDA: fart in you fridge and blame it on wild animals |
|
06-21-2013, 09:53 PM | #3 |
Stuff Do-er
Lua Team
Wiki Team Fortress Forever Staff |
The syntax here is actually the same as it would be in Lua.
The corresponding table in Lua would look like: Code:
classstats = { { speed = 1.3, weapon1 = 6, weapon2 = 0, class_maxhealth = 70, name = "Scout" }, { speed = 1, weapon1 = 4, weapon2 = 0, class_maxhealth = 80, name = "Sniper" }, ... } Code:
wep1 = classstats[ classnum ].weapon1 ... maxhealth = classstats[ classnum ].class_maxhealth ...
__________________
#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 |
|
06-22-2013, 04:24 PM | #4 |
worst ff player eu
Join Date: Jun 2012
Location: South Yorks., England
Class/Position: o - no, d - scout Gametype: IvDZ Posts Rated Helpful 18 Times
|
:L
I never remember when to use [] or . anyway :L Just guess and randomly pick till it works. Never thought to use a[b].c tho, I just kept trying a[b.c] :P I used it and it works, but now somehow I've broken the entire program and get errors in the main files of the game. Whoops. I'll try what I'm doing again, but in a different way, and see whether it works. TY for helping me out though.
__________________
gg ff not ded ff very much alive |
|
06-22-2013, 10:54 PM | #5 |
Stuff Do-er
Lua Team
Wiki Team Fortress Forever Staff |
[] 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 ] 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.
__________________
#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 11:05 PM. |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|