Fortress Forever

Go Back   Fortress Forever > Off Topic > Tech

Reply
 
Thread Tools Display Modes
Old 06-20-2013, 09:42 PM   #1
ddm999
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 accessing table (which doesn't work):
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;
}
I realise I've probably thought 'hey its like lua' and it won't be, at all. Ever. Darn C++.

L:

When attempting to compile, I get:

Quote:
c:\cube 2\sauerbraten - collect edition\cube 2 fortress\src\fpsgame\game.h|456|error C2065: 'weapon1' : undeclared identifier
c:\cube 2\sauerbraten - collect edition\cube 2 fortress\src\fpsgame\game.h|457|error C2065: 'weapon2' : undeclared identifier
c:\cube 2\sauerbraten - collect edition\cube 2 fortress\src\fpsgame\game.h|458|error C2065: 'add' : undeclared identifier
c:\cube 2\sauerbraten - collect edition\cube 2 fortress\src\fpsgame\game.h|461|error C2065: 'class_maxhealth' : undeclared identifier
=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===
Still, managed to (sort of) fix my first problem, which involved me trying to use something that's defined in a file which is ran after the current file :P

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
ddm999 is offline   Reply With Quote


Old 06-21-2013, 03:05 AM   #2
Dexter
internet user
Fortress Forever Staff
 
Dexter's Avatar
 
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
Dexter is offline   Reply With Quote


Old 06-21-2013, 09:53 PM   #3
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.
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"
  },
  ...
}
You'd access it like so:
Code:
wep1 = classstats[ classnum ].weapon1
...
maxhealth = classstats[ classnum ].class_maxhealth
...
The only difference here is that the starting index in Lua tables is 1 while the starting index for arrays in C++ is 0.
__________________
#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
squeek. is offline   Reply With Quote


Old 06-22-2013, 04:24 PM   #4
ddm999
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
ddm999 is offline   Reply With Quote


Old 06-22-2013, 10: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 11:05 PM.
squeek. is offline   Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.