Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

When organising game classes, should I put all the global data in the main class?

This topic is 5006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, in my game, I've got a Main class, which basically organises how everything is run, eg functions from other classes, and is basically the engine of my game. Then I got other classes that are declared in the Main class that do things like create the landscape, units, load and play sound, music, get inputs, eg mouse, keys. So say I got my two classes, Objects: handles soldiers etc, Land: handles the land. And these are declared in the Main class. Objects class needs the Land's vertices, so do you think I should store my land vertices in the main class, and just let the Land class have access to these vertices to draw it etc? or should I give the Objects class a pointer to the Land class, so it get see the vertices instead? Thanks, btw sorry bout the fragmented explanation.

Share this post


Link to post
Share on other sites
Advertisement
I think the second choice is the best one. ie, give Objects ptr to Land.

I guess they need the Land data for physics? An alternitive is to add a global physics system where all physics data is stored, and let that system handle movement and such. Objects could also access this system to do linetraces and other things...

Share this post


Link to post
Share on other sites
I too would go with the second choice. There's nothing inherently wrong with making the vertices global data, but it seems like a better organization choice to keep them in the land class; keeps your global Main class from getting too cluttered up.

Share this post


Link to post
Share on other sites
>Objects class needs the Land's vertices, so do you think I should
>store my land vertices in the main class,

Since it's the land's vertices it would probably be the best to
store it in the land class (makes sense, doesn't it?)

Now why does an object need land vertices?
Maybe it rather needs to detect a collision?


Maybe sth like this gives you and idea =>

class land
{ vector < vec3d > vtx;

float getFloor (object* o) { /* determine z coordinate we are at */ }
}

class cObject; // will need fwd-decl

class engine : public singleton // (look for the singleton template)
{
private:
landPointer* theLand; // or maybe it's a vector for several lands

public:
vec3d getFloorHeight (cObject* o ) { theLand->getFloor (o); }
};


enum Family
{
FAM_OBJECT,
FAM_BIG_OBJECT,
...
};

class cObject
{
private:
Family fam;
vec3d pos;

public:

void putOnFloor
{
pos.z = engine::instance().getFloorHeight(this);
}
};

class cBiggerObject
{
};

class objectManager : public singleton
{
private :
vector<cObject*> allObjects;

public:
int createObject (Family fam, subType t)
{ if (fam==FAM_OBJECT) { create cObject, add to vec, return idx; }
if (fam==FAM_BIG_OBJECT) { create cBigObject, add 2 vec, return idx; }
return 0; // unknown fam
}

void destroyObject (int id);

// either use a query with virtual funcs to access cBigObject, or
// maybe make a second functions which doess a typecast and returns
// a bigObject
cObject* getObject (int it);
}



hmm... typed a bit more than I intended to :)

Share this post


Link to post
Share on other sites
Yeah, I guess I should try and stop the Main class from getting too cluttered.

I guess the global physics system could work, im not sure that I would require it, as I won't have a huge amount of physics in my game.

The only problem with using a function to get the height of a single unit is, I may have over 400 soldiers in the game at one time.

And 400 calls to the same function i think may cause a bit of a slowdown i guess?

It looks like a very nice idea though!

Do you think having a get function that retrieves all the vertices from the Land class would be fast enough? saying the vertex number could be in 9- 20 thousand(wild guess) range.

1. Idea one
--------------------------------------------------------------------------
And then in the Main class I could provide a return pointer function that returns each of the classes that contain variables that other classes need.


As my class layout has Main in the middle, with all the others in a circle around it with a line each going to the Main class.

So with these return functions, they could connect the other classes through the Main class.
--------------------------------------------------------------------------

or idea two
--------------------------------------------------------------------------
I could have say in the create function of Objects class like this.
bool objects.create(this, &land); //all the other classes it requires are put in here, 'this' is the main class.

And then inside the Objects class I have declared a pointer to Land so I then have direct access to the Land class the whole time.

So then I don't have to create a whole heap of return functions in the Main class.

Or would this not be very OOP?
--------------------------------------------------------------------------

btw im making a basic realtime strategy game.

thanks

Share this post


Link to post
Share on other sites
Here are two useful questions to ask yourself when evaluating a design for a class.

1. What would happen to the implementation if I decided to instantiate two of them? What about 100?
2. Can I take this class and drop it into another application?

Share this post


Link to post
Share on other sites
>And 400 calls to the same function i think may cause a bit of a
>slowdown i guess?
Don't guess - We are generally very bad at guessing what the bottleneck
in an application is; use a profiler to check - I would be _very_
surprised if these 400 functioncalls would appear to be the bottleneck
in your application.

Usually I don't like objects having pointers to other objects, that
always gives a headache in savegames. Having id's and then doing a
lookup for the id to get the pointer when you need it workes better -
at least for me it did :)
Just like having an object have a pointer to a texture - better to
let the object have a textureID and do a lookup on the texturemanager
when you actually need the texture-pointer for rendering.

>Do you think having a get function that retrieves all the vertices
>from the Land class would be fast enough? saying the vertex number
>could be in 9- 20 thousand(wild guess) range.
Sure, just return a pointer or a reference to the data - doesn't get
any faster than that.

And still my question stands - why do you need access to the vertices?
Just guessing but if it's for a renderfunction in your 'game' class,
then instead add a rendercall in 'land' and call this function from
game. I'm pretty sure you don't really need access to the vertices,
just things calculated with the vertices (and these would be very
suitable imho for functions in your 'land' class)

>So then I don't have to create a whole heap of return functions in
>the Main class.
I prefer to have functions instead of direct access to data - you
can always make a modification to this function if needed, but
changing all the 3000 places where you directly access (modify?)
a variable isn't very comfortable.
And small functions like int getData () const { return intdata; }
are very good candidates for inlining!


Share this post


Link to post
Share on other sites
"1. What would happen to the implementation if I decided to instantiate two of them? What about 100?"
Instantiate? sorry whats that? :)

"2. Can I take this class and drop it into another application?"

Thanks for that advice, its much clearer on how i'd design my classes now.



The only reason I need to access the vertices is to see what the current height is so i can place the object on it or eg a soldier, cannon shell.
So I guess I should make a function that just retrieves the height
eg: land.getHeight(int x, int y);
ive got a create,run and function in every mainish class that i use are called from the main class in the appropiate places.

With the function speed thing, I've just had problems before when calling a get function maybe 10,000 times i got an extreme slow down, but I guess 10,000 is quite alot.
and whats a profiler?

And with all the pointers, the reason I thought to do it like so is, for example:
I've got the gui, with a volume control on it, so the user clicks on the volume control to reduce the sound, I then need to check for a click and mouse coordinates from the input class.
Then to reduce the sound volume, I got to run the function in the sound class to reduce the function, also I have to get the current volume from the sound class to show in the Gui class.

I guess its possible to use the windows message procedure, but I'm not sure thats wise learning wise, as I don't know of any other systems using such a message system, eg apart from Windows. even though I only plan to make my game on windows, for jobs etc I don't expect to only use that system.




Share this post


Link to post
Share on other sites
>I then need to check for a click and mouse coordinates from the
>input class.
Your input class isn't interested at all in a gui (you don't want
to couple these classes tightly together... reusability remember,
you want to use the input handler in the next project, but with
a different gui)
So your input class should do what the name suggests - deliver input
to the gui. Then the gui has to react to events which were fed into it

Probably you should look into "signals/slots" to see how to decouple
a control (button) from the action it executes (you don't want your
generic button/slider/whatever-control to know about a soundmanager
either)

>I guess its possible to use the windows message procedure,
Google for "crazyed gui", maybe that will give you an idea on GUI's

>and whats a profiler?
A tool/piece of code which you use to profile the code, ie see
how quick it is.. This can be pretty simple (getTime, execute
your function 10000 times, getTime, calc timedif/10000 = an
indication how fast your function is).
For a much more extended example, see one of the game programming
gems books - it contains an example profiles from C&C renegade.
There are also tools like VTune (for intel) - which show processor
time in each function so you know _what_ to optimize (don't optimize
before you know what to optimize!)

>but I guess 10,000 is quite alot.
Depends also on what you do in this function, 10.000 times trying to
get PI to 1mio places is probably a big speed hit, yes :)
The overhead for 5000 calls is probably minimal (if I would take all
functions calls I have in my program/frame I would probably be far
over 10000) - again, don't assume function-calls are slow, use a profiler
to see _exactly_ what piece of code is slow

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!