I struggle with relationships between game objects. (classes). Would appreciate assistance.

Hello All, first post, first day on this forum.

I’m wondering if someone could look over my rough outline of how I think a game could work.
I’ve been tinkering with game engine for around 10 years now, and not really getting anywhere
fast. I know basic syntax of python, C, C#, etc. - but when it comes time to actully applying the language
to a game - I get lost. I’ve yet to find a good book that applies a language to making a program,
rather - the books I own all seem to just teach the basics of a language.

This time around - I’d like to start from scratch with C++ and SDL2. This is about the only combination
I have not tried.

Anyway - if I can sketch out how I think a game could be made… could someone see if I am on the right track?
If it helps… I’ve been trying to make something 2D similar to Dwarf Fortress or Rim World.

  1. I think you start with a main loop… event handling, update, draw.
  2. In that loop you handle the different states ie: title screen, options screen, and then onto a main ‘play’ screen.
  3. My game objects will be classes… maptiles, npcs, mobs, items, quests, etc.
  4. My main file… has arrays… listofmaptiles , listofnpcs, listofitems., etc.

As I generate tiles, npcs, mobs, quests - any game object… I put them in the lists.
When the gamestate is ‘play’ - I then loop through the lists to conditional act upon each item. update / draw .

Bringing this all together - is how I have in the past with pygame and Godot… made games.

They have all performed horribly. They worked - but it was slow once I got 100’s of game objects deep.

I’m sure I’m not doing this right … and unfortunately - I don’t know, what I don’t know.

Can anyone critique this for me?
Am I missing a concept?
Was there a particular tutorial or textbook that helped you jump the gap between programming syntax and actually making a good program OOP structure?

Matt

Hi mattkw80,

I am in a vary similar position. I’m somewhat experienced in C++ but am also learning how to properly structure my code for an SDL game. For example as I add the audio system to my Tetris clone it blocks the rendering thread when I make a call to Mix_FadeOutMusic. I’m currently doing a lot of refactoring to try and make the game work as expected.

I think there are a few major points to take away from this. 1.) How to create a proper game loop. 2.) How to structure classes in an orderly fashion using proper inheritance. 3.) Useful patterns. 4.) An understanding of SDL and how it threads calls under the covers.

First off I would suggest learning the MVC (Model, View, Controller) pattern. It seems that separating logic in this manner is the best way to approach game programming. I’ve seen the MVC stated in a couple different ways. 1.) MVC1 2.) MVC2. Maybe someone can chime in a make a suggestion as to which MVC model is best for game programming.

The SDL Wiki seems quite useful and suggests a book called SDL Game Programming. I’ve started reading the book and worked through some of the exercises. It would probably be in my best interest to complete the book and all of the exercises. SDL Wiki, then click on books.

It sounds like your on the right track with your list above but likely needs to be a bit more detailed. A UML diagram may be able to provide the detail needed to design a proper class hierarchy. On point 2.) you are suggesting something like a State Pattern which I think is great. State Pattern.

I would suggest finding some SDL apps on GitHub and playing around with them as you read through the code. I found one called TeeWorlds but may be a bit complicated to learn from for a beginner. It seems like a fun game as I was able to build it and play it. TeeWorlds, details, github

I hope to learn more as I spend more time here in the discussion forums.

Electrosys

1 Like

Much to unpack here - and I appreciate your response.

I’ll go through all the the links you posted right now.

Thanks!

As for your performance problems, could you tell us about the code you run for every type of game-object for every frame? That’s usually where the problems come from. It could either be just an algorithmic thing, where you don’t realize that there’s a better way to do something, or you misuse SDL (calling SDL_RenderPresent() for each object), or maybe you really just do have very expensive operations you want to call many times a second.

I’m in a situation like yours, but, without the problem of slow run.
Justt a suggestion: try to show your code, is more easier when show some code, because the code shows like you thinking to solve something.

If you’re looking for programming patterns useful for game programming, I recommend http://gameprogrammingpatterns.com/ (you can read it for free on the web site)

Thanks for the reply’s all.

I can somewhat describe what I’m working on in the last issue I had, which I have actually solved since posting this.

I was making a proc-gen universe, similar to what Dwarf Fortress does. My performance
issue came from updating the GUI in Godot far too often. Every tick of time I was
updating the GUI text with information on 100+ objects. Once I stopped updating the GUI
text so often… it’s now very very fast. (Godot / GDscript ).
So that’s solved.

I appreciate the game programming patterns link… I think I have read about 1/2 of that book.
And it was very helpful. Turns out I had self dsicovered some of those patterns, but didn’t realize
what they were called. (I’ve been doing the Proto-type patter for a decade now).

So my biggest issue , and I will read through all you posted - is still the whole… how to make
objects deal with other objects when using something more bare… such as Monogame or C++ / SDL.

I’ve been thinking about this hard since I posted this, and I think I just need to get back to
actual programming … as I have had a few ideas. I’m trying to remember that the classic EVENTS / UPDATE / DRAW
loop - is just that… and I need to slot in all my other logic around it somehow.

‘Game’ needs a Universe which needs Maps which needs terrains or tiles which can have a player and npcs and mobs which can have
items or wounds / injuries or quests or ambitions.

I’ll go through some of the suggestions here in more detail.

Was also thinking of buying this:

Programming: Principles and Practice Using C++

I can do the basic syntax of many languages all day long… it’s the bringing it all together I’m still struggling with.