Very general question about structuring of game classes.

Started by
6 comments, last by Shannon Barber 10 years, 2 months ago

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.

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.

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?

3) High-level classes are designed specifically to act as controllers over the units. They are high-level enough to be able to interact with other parts of the game such as physics and they themselves can generate their own “action” commands and carry them out or distribute them as necessary to as many sub-systems as needed (and no more).

The units in your game are a slave to the game logic, not the other way around. Upper classes know what the rules of the game are and have access to the physics engine.

The interaction between units and the rest of the world must be done at a high level. But you are correct in wanting to isolate that type of code into 1 or more classes. So a high-level class designed to act as a controller over the units, extracting from them properties such as field-of-view and determining what enemies are in sight etc., plus forcing them within the bounds of the physics engine, is the correct solution.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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

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.

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.

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.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement