Jump to content
  • Advertisement
Sign in to follow this  
gchewood

Very general question about structuring of game classes.

This topic is 2093 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

Ok so I have a physics heavy simulation that's being chiefly run in my main level class.

I then have various characters that inherit from a general 'unit' class (it's similar to a top down RTS). I'm struggling to see which approach is best/ will be best for future efficiency when it comes to having these units interact with the physics of the world.

 

1) Pass all of the relevant data into each units Update function and allow them to manipulate it from there.

 

2) Have the units update function return an integer or something that corresponds to a particular action which is then carred out in the main game class.

 

So one is helpful in that it allows the entirety of the unit's capability to be contained within the class file. The other is helpful in that you don't need to pass a huge selection of variables to the update each time and all of the physics can be handled from within the main class.

Thoughts and suggestions are welcome. Is there a clear benefit to one over the other here that I'm missing or is it just a matter of preference.

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

What kind of variables are you having to feed from the Main Game class to the Unit?  This sounds to me like it's indicative of a larger design problem.  Can you give a little bit more information on how your current system is set up, what is being passed between, and what is the relationship of the main level & the unit class.

Share this post


Link to post
Share on other sites

You might well be right that it's indicative of a larger design problem. I'm largely self taught so the usual design tends to fit a 'whatever happens to work' style. Although I will say that the rest seems to hang together quite neatly.

 

The variables needed to be fed to the units are a few lists of physics objects and an integer array that describes the status of the 'cells' in the game. At the moment it's working fine by using the other approach of having the units update method return an integer that corresponds with a particular action. Granted, that's obviously not the ideal way.

 

What would you suggest? Maybe have the main class implement an interface that handles all of the appropriate methods which can then be accessed by the units?

Share this post


Link to post
Share on other sites

Sorry for the delay.

 

Thanks for the advice L.Spiro. So maybe something like a 'Unit Manager' class that handles all of the behaviour and then has access to the physics engine?

That's almost what I'm doing at the moment, I just need to tidy it up and separate it a bit.

 

Thanks

Share this post


Link to post
Share on other sites

You might well be right that it's indicative of a larger design problem. I'm largely self taught so the usual design tends to fit a 'whatever happens to work' style. Although I will say that the rest seems to hang together quite neatly.

 

The variables needed to be fed to the units are a few lists of physics objects and an integer array that describes the status of the 'cells' in the game. At the moment it's working fine by using the other approach of having the units update method return an integer that corresponds with a particular action. Granted, that's obviously not the ideal way.

 

What would you suggest? Maybe have the main class implement an interface that handles all of the appropriate methods which can then be accessed by the units?

 

I'd like to add to this quote by including my personal methods when dealing with heavy simulations. I should preface this by stating that there are countless good methods.. but here is one object oriented approach:

 

I will typically have a unit manager / collection that contains a reference to all the current units in the game. I will also have a simulation class (or multiple simulation classes for different types of sims) that will request a pointer to a collection of specific units from the unit manager to act upon. The class, which has a global understanding of the current state of the units and game, takes ownership of updating the information contained in the unit class (speed, location, state) based on the simulation criteria within the simulation class. If you have multiple simulation steps (classes for different types of simulations), the simulation manager simply passes the relevant collection of units from one sub-system to another. 

Share this post


Link to post
Share on other sites

I've come to prefer an alternative approach where state for an entity is self contained in specific systems and essentially isn't "shared".  As a system is ticked in the main loop, it operates across a subset of entities (units) that have specific characteristics pertinent to that system.  As conditions are met during the system update, the system notifies other areas of the game using as series of listeners and passes whatever pieces of data is needed for that condition only.  Those callbacks could then take a series of actions themselves or perhaps alter the state of the entity by returning specific values and having the original system update behave accordingly.

 

The only tricky part I've found with such an alternative is making sure that as you create entities or modify them during game play (add or remove characteristics), that these systems are notified so that if an entity no longer qualifies, that it gets added/removed from it's internal list.

Share this post


Link to post
Share on other sites

I have a physics sub-system and the physics sub-system has it's own "primative" classes that you must construct your object out of.

 

A final game object, a 'boid', in the world ties together the data needed by each sub-system to create a coherent simulation of that object.
For efficiency purposes you might have the physics data itself stored in a structure inside the physics engine and the boid just references it.

That way the physics engine can perform passes over the entire game world structure of physics and use its own optimal spatial sorting.

(I opted for simplicity here and each boid owns it's data.)

 

I decide to go this way a long time ago with the observation that the optimal audio spatial sorting was markedly different than the optimal graphics sorting and obviously the data is completely different.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!