Sign in to follow this  
bedtime

General Class Structure Question

Recommended Posts

bedtime    159
I've made a 2D mario style side-scroller. It works fine. My issue is that I don't think I'm using inheritance and polymorphism to the best of my ability. The result is that to use one class method I need to bring over a whole bunch of parameters from other class methods and containers.

For example:

instead of just drawing the board this way:

[CODE]
draw(imageToDraw, x, y);
[/CODE]

I end up having to do this:

[CODE]
draw(game, draw, objects, x, y);
[/CODE]

This wouldn't be an issue if it wasn't happening on so many methods of different functions and makes the code so much more complex looking and cumbersome. Another issue that makes it a little more challenging is that this game allows for multiple games at once. Basically split screen for an infinite amount of games. I do not want to drop this functionality.

So here are my basic classes:

Game - main loop
Draw - draws objects
Camera - tells Draw the location of objects to draw
Object - stores object xy velocity, characteristics...
Enemy Player Board - sets object characterists
Factory - object factory creates games objects
Move - moves objects
Collision - checks for crash
Record - records objects movement and plays back
Keyboard - game input

Here is my idea and I'd like some input on it. What do you think of combination like this ( where '->' means derived from):

game ->
camera -> draw
factory -> object -> enemy/player/board
move -> record -> collision -> keyboard


This is basically how I have it except I'm not inheriting from 'game' so I'm a lot of passing parameters. Would this be the better option and just inherit from 'game'?:

game->
camera
draw
factory
object -> enemy/player/board
move
record
collision
keyboard

I could use some ideas. I ask this because reworking this will take days. If I can get some ideas perhaps this will make things easier in the future.

Share this post


Link to post
Share on other sites
de_mattT    308
Hi Bedtime,

I'm having some trouble following your "derives" relationships above. Are you asking if everything should inherit from "Game" so that an instance of "Game" doesn't have to be passed in function calls where it is used?

My general thoughts would be:
1. Inheritance is an "Is A" relationship, therefore a child object should only inherit from a parent object if it is a more specialised version of the parent object. So I would think that, based on your overview above, nothing would inherit from Game.
2. Would "Has A" relationships work instead? Is it possible to pass in the "Game" instance to "Draw" at initialisation and store the reference for later use? Or are you using a single "Draw" object to do the drawing for all split-screen games?

Matt

Share this post


Link to post
Share on other sites
bedtime    159
[quote name='de_mattT' timestamp='1353360741' post='5002446']
Hi Bedtime,

I'm having some trouble following your "derives" relationships above. Are you asking if everything should inherit from "Game" so that an instance of "Game" doesn't have to be passed in function calls where it is used?

My general thoughts would be:
1. Inheritance is an "Is A" relationship, therefore a child object should only inherit from a parent object if it is a more specialised version of the parent object. So I would think that, based on your overview above, nothing would inherit from Game.
2. Would "Has A" relationships work instead? Is it possible to pass in the "Game" instance to "Draw" at initialisation and store the reference for later use? Or are you using a single "Draw" object to do the drawing for all split-screen games?

Matt
[/quote]
I think 'has A' could work but would require more parameters to be passed. The thing is that that most objects are a part of game and need to be separated to work properly. This would be a different situation had I made this only a single game. Inheritance seems to make sense to me as it allows the objects to be easily divided at the beginning. Having said that, as for storing the game object reference I hadn't thought of that. It's a brilliant idea and I'll take a look into it. That could solve this, but is it good practice to do this? Not that what I have now is anywhere near good practice. But this would make it so much easier to fix.

Atm I'm using 'draw' to do both 'draws' job and 'cameras' job and so there are multiple 'draw' objects. I'm looking into changing this and making multiple 'camera' objects (camera calculates and stores the average position of multiple player objects to allow multi player camera focus). The only class that seems to have static functions is 'game' which holds the number of games as well as keyboard, which for now takes input once and gives to all games.

Share this post


Link to post
Share on other sites
FLeBlanc    3141
I would say that if you are minimizing inheritance and polymorphism to the absolute best of your ability, you're doing okay. Inheritance can make for a nasty tangle. Following on de_mattT, there is a rule: [url="http://en.wikipedia.org/wiki/Composition_over_inheritance"]Favor composition over inheritance[/url]. While it's not a concrete law, it's still a pretty good rule to go by.

Share this post


Link to post
Share on other sites
frob    44913
[quote name='bedtime' timestamp='1353356110' post='5002426']
The result is that to use one class method I need to bring over a whole bunch of parameters from other class methods and containers.
[/quote]
If a class requires a lot of parameters, or if it requires a great deal of outside information, it means your work load is not properly partitioned.


[quote name='bedtime' timestamp='1353356110' post='5002426']
I could use some ideas. I ask this because reworking this will take days. If I can get some ideas perhaps this will make things easier in the future.
[/quote]

A class should be a noun. Functions/methods should be verbs. Several of your classes are verbs in disguise which is probably why they aren't working too well. Move, Collision, Record, Draw, these may be problematic.

Use composition instead of inheritance, and don't use either when it isn't necessary.

The composed data structures, not object state, should generally provide meaning. For example, object rendering doesn't take place in a vacuum, but rather in a rendering hierarchy.



Finally, start smaller. If you are having problems with it then you should probably tackle a smaller game instead.

Share this post


Link to post
Share on other sites
bedtime    159
[quote]I would say that if you are minimizing inheritance and polymorphism to the absolute best of your ability, you're doing okay. Inheritance can make for a nasty tangle. Following on de_mattT, there is a rule: [url="http://en.wikipedia.org/wiki/Composition_over_inheritance"]Favor composition over inheritance[/url]. While it's not a concrete law, it's still a pretty good rule to go by.
[/quote]
Thank you. I've checked the link out and though I understand most of it I think implementing it might be a different story. But this just may be the way to go as the game objects need to be extremely flexible to do what I want them to do.


[quote name='frob' timestamp='1353363469' post='5002468']
If a class requires a lot of parameters, or if it requires a great deal of outside information, it means your work load is not properly partitioned.

[/quote]
Yes, this has been an issue from the start.

[quote]
A class should be a noun. Functions/methods should be verbs. Several of your classes are verbs in disguise which is probably why they aren't working too well. Move, Collision, Record, Draw, these may be problematic.

Use composition instead of inheritance, and don't use either when it isn't necessary.

The composed data structures, not object state, should generally provide meaning. For example, object rendering doesn't take place in a vacuum, but rather in a rendering hierarchy.

Finally, start smaller. If you are having problems with it then you should probably tackle a smaller game instead.
[/quote]
I did take on a lot with this game. I may decide to simplify it if I continue to be at a standstill. I'm going to look into tutorials on composition and see how I can implement this.

Thanks

Share this post


Link to post
Share on other sites
FLeBlanc    3141
Some time back, a fellow asked for an example of object composition in a simple game such as pong. I wrote a couple examples, and while there is a whole lot about them that I would do differently now, it still might be somewhat useful to you.

http://www.gamedev.net/topic/623945-examples-of-component-design-in-simple-game-such-as-pong/

Share this post


Link to post
Share on other sites
bedtime    159
[quote name='FLeBlanc' timestamp='1353365366' post='5002483']
Some time back, a fellow asked for an example of object composition in a simple game such as pong. I wrote a couple examples, and while there is a whole lot about them that I would do differently now, it still might be somewhat useful to you.

[url="http://www.gamedev.net/topic/623945-examples-of-component-design-in-simple-game-such-as-pong/"]http://www.gamedev.n...e-such-as-pong/[/url]
[/quote]
Thats a lot of information to take in so I'll likely be busy for a while before i come back to ask anymore questions. The more i read about composition the more it looks like the best fit.

and once again, thanx all.
Thanks for looking up that link for me.

Share this post


Link to post
Share on other sites
rip-off    10976
I don't see any obvious inheritance between any of the classes you mentioned. This is something it took me a while to understand as a beginner, but inheritance is actually quite rare.

As for your actual problem, it sounds like your code is too coupled. Too many classes want to be aware of other classes, rather than just talking to the ones they need to. The next step involves learning how to modularise your code.

I wouldn't recommend component-oriented design for the time being, it is probably overkill for the scale of the game I think you are aiming for.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this