why is the class not declared when its in its own class

Started by
13 comments, last by Zahlman 15 years ago
well i need a list of players stored somewhere. How would i store them?

My game will be choose a char from 4.
Stats will be generated and you play against the other 3 in a 1v1 game.

you level up, so do they, things happen along the way to increase decrease and area of the char, you play series of mini games to get stats up, set up your char and then play the game.

this post and this little project is about coming across common errors when programming and trying to give answers to a lot of errors and misconceptions other people come across in oo programming. this site has been the best resource over the yars i could have ever hoped for, there are so many talented people who come on here and responses are of a much high level here than some of the other forums i go on.

Anyway, programming, ill be back home soon so i can actually experiment, thanks for the advice so far though
Advertisement
Quote:Original post by DrPepperCorn
well i need a list of players stored somewhere. How would i store them?

How and where? That depends on a variety of factors. Did you think about your programs structure beforehand? About it's design? Once you know how those players need to be used, you'll get a better idea of how and where you can fit them into your programs design. Without further information, it's hard to give accurate advice on that matter.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by ravengangrel
cPlayer p1;
cPlayer p2;
cPlayer p3;
cPlayer p4;

I even guess that with the above declarations, most compilers could allow you to do:
cPlayer* p = &p1
And then access p3 with p[2]

Pointer arithmetic beyond array bounds yields undefined behavior. And since p does not point to an array, you are not allowed to do pointer arithmetic on p at all, so what you are demonstrating here is illegal.
Quote:Original post by DrPepperCorn
would making a function in the class called initPlayers and create them there be any different?


No. Having the class that defines what a player is also manage instances of the players is never going to work out well.


Quote:Original post by DrPepperCorn
OR should i make a seperate class called cGameAssets or something and create my vector of players there?


Now you're getting somewhere. The object and the container for objects are two different concepts and should be modelled as such.

However, your cGameAssets instance will also have to live somewhere so you haven't really solved anything by deciding to put your player list in a class. Unfortunately, as Captain P says, this level of design depends on too many factors for it to be feasible to offer much advice.

Personally, my game structure tends to look a bit like this (vastly oversimplified):

class Game{private:    std::vector<Actor> Actors;    GameMap Map;public:    Game(){ /* initialise the level */ }    void Update(){ /* do physics step etc */ }    void Render(){ /* render one frame */ }};int main(){    Game game;    while(Running)        {        game.Update();        game.Render();        }}


This is a huge simplification obviously but it illustrates where things "live". No global state this way so the lifetime of everything is based on the lifetime of the instance representing it.
Quote:Original post by DrPepperCorn
well i need a list of players stored somewhere.


No, you don't. Make the list as a local variable, and pass it around using the parameters of the functions that you call. This is the normal way of handling data. That's why they're called functions: the output is a function of the input, in the mathematical sense. (Well, roughly speaking, anyway.) So you give the function access to the data that's needed to perform the function's calculation. I.e., the input.

This topic is closed to new replies.

Advertisement