#83686 - spencer723 - Wed May 17, 2006 9:36 pm
I've been trying to use a structure to store my players hp, position, ect, but I keep getting an error. My structure is setup like this:
Code: |
struct player {
int pos[2] = {0,0};
int hp = 100;
int hp_cap = 100;
int attack = 20;
int defense = 20;
int exp = 0;
int level = 1;
};
|
and the error I'm getting is:
Code: |
arm9_main.cpp:21:error: a brace-enclosed initializer is not allowed here before '{' token
arm9_main.cpp:21:error: ISO C++ forbids initialization of member 'pos'
arm9_main.cpp:21:error: making 'pos' static
arm9_main.cpp:21:error: ISO C++ forbids in-class initialization of non-constant static member 'pos'
ect.
|
Any help would be appreciated :)
#83690 - ProblemBaby - Wed May 17, 2006 9:55 pm
You cant give them initvalues in the definitiation
remove all "= blah"
#83696 - spencer723 - Wed May 17, 2006 10:11 pm
ProblemBaby wrote: |
You cant give them initvalues in the definitiation
remove all "= blah" |
Thanks! I also had to make all of them static ints.
#83705 - Lazy1 - Wed May 17, 2006 11:25 pm
They don't need to be static ints, just do something like...
Code: |
struct Player_S {
int playerPosition[ 2 ];
int health;
} Player_T;
void initPlayer( Player_T* player ) {
player->playerPosition[ 0 ] = 0;
player->playerPosition[ 1 ] = 0;
player->health = 100;
}
|
You are defining a structure, the type itself cannot contain data but variables of that type can.
#83729 - spencer723 - Thu May 18, 2006 2:01 am
Lazy1 wrote: |
...
You are defining a structure, the type itself cannot contain data but variables of that type can. |
Thanks! Even better. Now, will that be globally available to my whole program (excluding the arm7)? Sorry, I've never declared a structure like this before and I'm sorta new to the DS programming.
#83734 - wintermute - Thu May 18, 2006 2:31 am
You could also initialise it like this although it's not as clear as initialising the values directly.
Code: |
struct player {
int pos[2];
int hp;
int hp_cap;
int attack;
int defense;
int exp;
int level;
};
struct player player1 = {
{0,0},
100,
100,
20,
20,
0,
1
};
|
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog
#83737 - spencer723 - Thu May 18, 2006 2:52 am
Thanks everyone :D, I got it working perfectly
#83739 - Lazy1 - Thu May 18, 2006 3:08 am
Have you fully learned C/C++?
If not may I suggest this book:
http://www.amazon.ca/exec/obidos/ASIN/0072226803/qid=1147917749/sr=8-1/ref=sr_8_xs_ap_i1_xgl/702-9042055-0329641
It is possible to learn C or C++ by reading code and changing small things here and there,
but actually learning and understanding the language helps incredibly.
You'll understand what what those errors mean, how to solve them and better yet - not cause them.
It's also a good idea to learn on a standard platform, like win32, linux or macos x.
#83850 - HyperHacker - Thu May 18, 2006 11:44 pm
wintermute wrote: |
You could also initialise it like this although it's not as clear as initialising the values directly. |
That's whre comments come in.
Code: |
struct player player1 = {
{0,0}, //pos[2]
100, //hp
100, //hp_cap
20, //attack
20, //defense
0, //exp
1 //level
}; |
Ideally you'd put a tab before each comment, but you can't do that in the reply box.
#83863 - spencer723 - Fri May 19, 2006 1:18 am
Yep, I've learned C/C++, I taught myself and now I'm in computer science so I'm learning alot more (although I already know most of the stuff being taught).
#84145 - snowsquirrel - Sat May 20, 2006 7:07 pm
Wouldn' this work?
Code: |
struct player {
player()
{
pos[0]=pos[1]=0;
hp=hp_cap=100;
attack=defense=20;
exp=0;
level=1;
}
int pos[2];
int hp;
int hp_cap;
int attack;
int defense;
int exp;
int level;
};
|
Or even better
Code: |
struct player {
player(int p_x=0, int p_y=0, p_hp=100, p_hp_cap=100, p_attack=20, p_defense=100, p_exp=0, p_level=1)
:hp(p_hp), hp_cap( p_hp_cap ),
attack( p_attack ), defense( p_defense ),
exp( p_exp ), level( p_level )
{
pos[0] = x;
pos[1] = y;
}
int pos[2];
int hp;
int hp_cap;
int attack;
int defense;
int exp;
int level;
};
|
Then when you want to create your player, all you have to do is:
For the first suggestion
Code: |
player p1;
player p2;
|
or the second option gives you the ability to create the default, or change a few things:
Code: |
player p1(0,0, 120, 150); //make p1 a little tougher
player p2;
|
Good Luck.
#84359 - spencer723 - Mon May 22, 2006 2:39 am
Hmm, I actually may consider that. It could be easier for when I need monsters or enemies on screen with certain health. Thanks snowsquirrel!
#84362 - Lazy1 - Mon May 22, 2006 3:05 am
Unless there is some wierd compiler feature, as far as I know C structures cannot have constructors.
What you could do, is take an object-oriented approach to your game.
For example:
class Entity:
health
armor
x
y
class Player inherits from entity but adds:
weapons
class Enemy inherits from entity but adds:
target
ect..
#84367 - wintermute - Mon May 22, 2006 4:11 am
Lazy1 wrote: |
Unless there is some wierd compiler feature, as far as I know C structures cannot have constructors.
|
In C++ structs are classes where everything defaults to public.
The code up there should have been
Code: |
struct player {
player(int p_x=0, int p_y=0, int p_hp=100, int p_hp_cap=100, int p_attack=20, int p_defense=100, int p_exp=0, int p_level=1)
:hp(p_hp), hp_cap( p_hp_cap ),
attack( p_attack ), defense( p_defense ),
exp( p_exp ), level( p_level )
{
pos[0] = p_x;
pos[1] = p_y;
}
int pos[2];
int hp;
int hp_cap;
int attack;
int defense;
int exp;
int level;
};
|
it was missing types on most of the constructor parameters and the input position was in p_x & p_y
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog
#84424 - snowsquirrel - Mon May 22, 2006 1:58 pm
oops that is what I get for coding on the fly. Also note that this is legal C++, but not legal C. So if you have a reason not to use C++, you will have to stick with the other suggestions.
~S