• Create Account

## General Class Structure Question

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.

8 replies to this topic

### #1bedtime  Members

159
Like
0Likes
Like

Posted 19 November 2012 - 02:15 PM

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:

draw(imageToDraw, x, y);


I end up having to do this:

draw(game, draw, objects, x, y);


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.

### #2de_mattT  Members

308
Like
1Likes
Like

Posted 19 November 2012 - 03:32 PM

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

### #3bedtime  Members

159
Like
0Likes
Like

Posted 19 November 2012 - 04:00 PM

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

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.

### #4FLeBlanc  Members

3133
Like
2Likes
Like

Posted 19 November 2012 - 04:01 PM

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: Favor composition over inheritance. While it's not a concrete law, it's still a pretty good rule to go by.

### #5frob  Moderators

41344
Like
2Likes
Like

Posted 19 November 2012 - 04:17 PM

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.

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.

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.

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #6bedtime  Members

159
Like
0Likes
Like

Posted 19 November 2012 - 04:38 PM

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: Favor composition over inheritance. While it's not a concrete law, it's still a pretty good rule to go by.

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.

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.

Yes, this has been an issue from the start.

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.

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

### #7FLeBlanc  Members

3133
Like
1Likes
Like

Posted 19 November 2012 - 04:49 PM

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/

### #8bedtime  Members

159
Like
0Likes
Like

Posted 19 November 2012 - 05:12 PM

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.n...e-such-as-pong/

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.

### #9rip-off  Moderators

10730
Like
1Likes
Like

Posted 19 November 2012 - 05:59 PM

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.

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.