Skip to main content
gamedev.net
🔒 Locked

Game Classes

Started by programmermattc Jan 16, 2007 at 2:32 PM 3 replies 600+ views
Original Post
programmermattc
programmermattc
I've been writing code based on tutorials for the last few months and, even though they teach me a lot, I believe they use way too many seperate classes without enough instantiation. I guess I'm just wondering what kind of setup everyone else uses. I've been told by others that instantiation is quite a big part sometimes and the only time I use it is with my 'GameApp' being and derivation of 'Framework'. (Framework creating basic window and GameApp the game code) Also, not that it matters for the purpose of understanding your format, I'm using DirectX.
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
Captain P
Captain P
Using too many classes can be a danger, yes. However, you can use classes effectively to separate large chunks of code into smaller, more manageable pieces, and give each class a clear responsibility of it's own.

Having that said, at some point you'll probably need some objects you can reuse, like sprites and images, or game objects like enemies, projectiles, items, and so on. Good candidates for classes and usually also for a nice inheritance structure. You can then add one or more classes to manage these objects. The rest of the code only needs to worry about knowing that particular class, as it serves as an interface to the complex management going on underneath it.
In other words, there's a lot of uses for classes. Do a search for 'design patterns' and you'll see some creative uses. Oh, you may also want to know about 'anti-patterns', examples of how not to do things. :)

Don't worry too much about instantiation: it depends on how complex your class is. A class containing only an integer takes only slightly more time to construct than an integer itself. Only worry about things like this when you notice parts of your program are taking a lot of time (this is called profiling, e.g. pinpointing the bottlenecks in a program).
Sneftel
Sneftel
Quote:
Original post by programmermattc
I've been told by others that instantiation is quite a big part sometimes and the only time I use it is with my 'GameApp' being and derivation of 'Framework'.

Do you mean inheritance?
programmermattc
programmermattc
Quote:
Original post by Sneftel
Quote:
Original post by programmermattc
I've been told by others that instantiation is quite a big part sometimes and the only time I use it is with my 'GameApp' being and derivation of 'Framework'.

Do you mean inheritance?


Sorry, yes you're right, I did mean inheritance.
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
Sneftel
Sneftel
Beginning OOP programmers are often tempted to overuse inheritance. You seem to have overused it yourself, in terms of GameApp inheriting from Framework. You should prefer composition (object of class A as a member variable in class B) to inheritance (class A as the base of class B) where possible. Required reading.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.