Advanced game structure

Started by
17 comments, last by Eliburgi 9 years, 9 months ago

There's no right answer to these questions, but the most important thing is that you're asking the questions and thinking about it.

In the case of collisions what I might do sounds pretty similar to what you've described doing in the past. In Java, I would create an interface called "Collidable". I would then keep track of all of the Collidables in a list (where list could be an ArrayList, HashMap or whatever's appropriate) and perform collision checks on the objects in that list. If you want to get more fancy you can have your list hold WeakReference objects so you don't have to worry about destructors (but now we're getting into Java specifics, and you haven't said you're using Java).

As far as your achievement system, you may be able to find some fancy way to abstract it and provide complete de-coupling. But sometimes the best thing to do is just:


// TODO - move this into a framework later
doUglyAchievementCheckInPlayerClass()

And then if "later" never comes, it probably means you've better spent that time on some other part of your game.

Advertisement

Thanks for your answer. I am using C# (XNA) so I don´t have to care about Destructors like in C++. Your way to handle collisions is the only way I know so far and therefore the one I am also using. But how do others handle collision(also tilemap) without having everything coupled? When looking in the internet, I only find things like quadtrees, ... , but I am interested in way different objects communicate in a game. What about that approach(first answer): http://stackoverflow.com/questions/11323413/handling-interaction-between-objects. As I said above I want to make a RPG, not too big but still that it looks and feels like a classic RPG. So now I am planning things and one problem that occured is, that I want a way to interact with game objects(treasures, traps, obstacles like stones, ...) without having coupling(or at least not much coupling) between the different classes, so that I can reuse parts of the code in other similar projects. Once again sorry for my english.

Thanks for your help so far :D

haegarr how do you handle interactions between different game parts/objects?


haegarr how do you handle interactions between different game parts/objects?

This cannot be answered in a sentence except with "it depends" ;)

BTW: Don't think that I reject the use of Observer principally. All things have their use, even Singletons, but only at the right place.

I'm using a game loop in which sub-systems are updated in a defined order. This is, as said, not uncommon. I like to hint at the chapter of Jason Gregory's book that can be found as excerpt at Gamasutra. With such an approach each sub-system knows that the state of the world at the moment of its own update is complete with respect to all the sub-systems prior in the loop. For example, all animations are already run before collision detection is invoked. If collision detection would be invoked when only a part of the animations were run, then it may detect false collisions simply because some of the Placements are still set to their obsolete position. Later on, when rendering takes place, the rendering sub-system takes all Placements as they are. It is not interested in how many "change events" would have occurred; it is just interested in the current state. In this example states are written and read but no notification about state changes are made.

Another example is input processing. I do not propagate input events into the various sub-systems. The Input sub-systems knows of its devices, gathers / collects all available input state changes, timestamps and unifies them, and puts them into a queue (this process is not part of the game loop because the Input sub-system runs in a thread apart from the game loop). When a sub-system in the game loop is interested in input, it has to investigate the said input queue when it is called for update, detect input situations matching the own input configuration, and react accordingly. Here the input state as well as state changes are memorized (for some time, e.g. allowing for detection on input combos), but no notifications are made.

Another example is the implementation of the sense system. This is part of the AI sub-system where visual, aural, touch and even olfactory stimuli are produced and may be detected by AI (usually of NPCs). Some visual and aural stimuli are generated on-the-fly when the AnimationServices is updated. Other stimuli, also visual but also olfactory, last longer. Every existing stimulus is linked in a spatial structure where the AI components can query for collision with their respective senses. Also here the occurrence of a new stimulus is not notified.

That said, low level services like the SpatialService manage low level data like the Placement (btw, for game objects I'm using a component based entity architecture with sub-systems, although they are called "services"; just for the case you don't have assumed it already ;) ). Higher level services query lower level services for data, e.g. the SpatialServices provides queries for collision, proximity, and line-of-sight. The lower level services do not know how to deal with the results, that is the job of the higher level services. Hence there is a strict top-down coupling.

Wow thanks. That was that kind of answer, which is helping a lot. I also read the article at Gamasutra, which is sometimes a little bit hard to follow, but I understood what the author meant, I think. But such a type of structure is too professional for me I think at that point(I am game programming one year now), and I think I will go with a different, easier approach. Even all the questions didn´t really solve my problem with the RPG, it helped a lot. Thanks for all your patient and interesting answeres. I think I am going to start a new thread for my question about the RPG.

So have a nice day :D


I also read the article at Gamasutra, which is sometimes a little bit hard to follow, but I understood what the author meant, I think. But such a type of structure is too professional for me I think at that point(I am game programming one year now), and I think I will go with a different, easier approach.

Many game loops are not so complex like the one shown there. However, it is important to understand that there is a defined order of updates, regardless of how many steps your loop has. That is done in all game loops I know of.

Traditionally one does something like the following: At the beginning of the game loop run the input sub-system (which is in that case not running in its own thread, of course) to collect input, then run AI if necessary, then run animation, then run rendering.

Yeah. I do it that way you explained it, but I handle animation and AI inside the respective classes(for example in a platformer I have a class AnimationStrip and every animated objects has a dictionary(C#) with strings as keys(so I can say currentAnimation = animations["jump"]) and AnimationStrips as data. So would it be a good idea to put that all into one sub-system? And I let every object draw itself,so that I can say for example:

foreach(GameObject o in gameObjects)

{

o.Draw(spriteBatch); //Spritebatch is used for drawing in XNA

}

so but thats ok or?

Thanks a lot for your answeres :D

I now have a new idea to tackle that problem with interacting with world objects(treasures, ...). I simply add triggers to my map and when the player is near a trigger he can press a button assigned to interacting with objects(lets say space) and then I simply fire an event assigned to that object(for example when interacting with a sign a dialogue window opens). So would that be a good idea?

Ok so thread closed :D

This topic is closed to new replies.

Advertisement