Jump to content

  • Log In with Google      Sign In   
  • Create Account


Structuring and organising classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Cosmic R   Members   -  Reputation: 157

Like
0Likes
Like

Posted 19 June 2014 - 07:57 PM

Hi all, I've been a long time lurker for a few years after a long break from the site.

 

I'm still a beginner, but am really pushing to get a few games under my belt.

 

Anyway, I've set up a basic game design like this:

 

Game class Singleton

Game State Class  (for playing/paused etc)

 - handle input

 - draw

 - update

 

Sprite Class

 

Game Object Class

 - list of sprites for animation

 - virtual handle input (calls state->handle_input)

 - virtual update (calls state->update)

 - virtual draw (calls state->draw)

 

 - Monster Class (derived from GameObject)

 - Player Class (derived from GameObject)

 

State Class

 - virtual handle_input

 - virtual update

 - virtual draw

MonsterState Class (derived from State)

PlayerState Class (derived from State)

 

Object Manager Class

 - manages a list of game objects of a type eg ObjectManager<Monster> monster_manager;

 - calling monster_manager.update() runs the update command on all the monster objects etc

 

Okay, so now that's out of the way, my problem is how do I get my objects talking to the manager? I want my player object for example to be able to tell the manager to create bullet objects.

 

But it seems the way I've designed it all only allows for messages to be sent downwards, not upwards. I tried including the calling objectmanager on each update call, but because the objectmanager is templated it didn't work.

 

I thought of creating a global messaging system, where a list of message objects is kept and every loop they are executed, but that's just moving the problem around, not really changing the structure (ie the message class would need access to the ObjectManager)

 

Am I going about this the wrong way?

 

Are there any good examples of basic games (but not Pong) that allow for expansion? Maybe something like object oriented space invaders?



Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13268

Like
10Likes
Like

Posted 19 June 2014 - 08:27 PM

Anyway, I've set up a basic game design like this:

  • Singletons are a common sign that you are doing something wrong.
  • Inputs are not events.
  • Objects/characters do not handle their own inputs.
  • I see a Sprite class but no Image class (loads image data from files, memory, whatever) and no Texture class (makes a texture out of an image).
  • A state class defines an object’s logic.  Rendering is just graphical representation of an object in its current state.  State classes have no reason to hold any form of drawing methods.

Okay, so now that's out of the way, my problem is how do I get my objects talking to the manager? I want my player object for example to be able to tell the manager to create bullet objects.

Once you have read the links I posted it should become obvious that your problem is that you are handling input at too low of a level.
Just as in my proposed example in which a character suddenly needs to know about the game rules, the physics system, the locations of all the enemies, projectiles, and platforms, so too does your system need illogical knowledge of higher-level components.
I have already explained it is quite a lot of detail so I will not repeat myself, but, to summarize, the game-logic system controls the game’s rules. It tells the player’s character to fire bullets, not the other way around.

 

But it seems the way I've designed it all only allows for messages to be sent downwards, not upwards.

I thought of creating a global messaging system

Get rid of the idea of a messaging system.
Do you consider that even the firing of a bullet is a message? If you don’t understand the role of messages in video games you are going to completely abuse them like this. It will bog down the performance of your game (a rapid-fire game in which every bullet causes a message to be sent to every actor for a potential response?) and lead to the ultimate debugging hell. Bugs will be untraceable, and there will be bugs as you change game states and either have left-over messages that get processed and shouldn’t or left-over messages that don’t but should (messaging-system flaws account for a large majority of bugs you see in professional games, especially where you can duplicate items, etc.)
Games are not message-driven.
Unless you need map triggers, networking, or medium-sized AI, don’t even think about having a message system. Simply by being there you will be tempted to use it for things for which it was not meant (like inputs and bullets).


L. Spiro

Edited by L. Spiro, 19 June 2014 - 08:28 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Cosmic R   Members   -  Reputation: 157

Like
0Likes
Like

Posted 22 June 2014 - 09:13 PM

thanks for the great reply.  I have taken into account your suggestions. My biggest problems as you noted appear to be numbers 2, 3 and 5. Basically my game should know when the player has fired etc, not have the player class tell it. I was delegating too much down the chain. All my state classes should do is update the object it's attached to.

 

As far as my sprites go, yeah I need to re-write it - at the moment it works, but not how it should so I'm aware of that one...

 

And with no 1, the singleton, if I shouldn't use it then how do I instantiate the game? A static class?



#4 L. Spiro   Crossbones+   -  Reputation: 13268

Like
6Likes
Like

Posted 22 June 2014 - 09:34 PM

My game class is on the stack inside main(). Since it gets passed to every state’s main set of methods (initialize, destroy, tick, and draw) and those are the only places in which any game logic can be executed there is no need to make it global in any way. A static class would be the worst option (I have very specific and carefully considered reasons for where I do this).


L. Spiro


Edited by L. Spiro, 22 June 2014 - 09:34 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Servant of the Lord   Crossbones+   -  Reputation: 18496

Like
6Likes
Like

Posted 22 June 2014 - 09:58 PM

Definitely not a singleton, global, or static class. smile.png

I either do dynamically created or created on the stack after main() is entered, and likely directly in main() (or an equivalent function) itself.

 

While singletons, globals, and static classes seem helpful at first, they make the code more complicated, harder to debug and maintain, and can also cause unexpected problems later down the road.

 

(Global constants are fine though!)


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS