Jump to content
  • Advertisement
Sign in to follow this  
Mekuri

How should I structure my game code?

This topic is 1927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For about a year I was working on a game, on a learning by doing basis. This worked well for me, and the result, so far, isn't that bad. I shelved that project for now though, and I started working on a new one, with a somewhat smaller scale.

A question that has been bothering me for a very long time, is surfacing again. Let me explain...

 

When I make a game, I make several "Screen" classes, that each contain a screen, like the menu screen or the playscreen, etc. When the game is playing the playscreen is of course shown. The playscreen then has an instance of a level class, that contains the level array, lists with mobs and items, and the starting location for the player. Each type of mob has it's own class, that derives from a base class (to make it possible to keep them all in the same list). Now this is all well and good, but my problem arises, when I start doing the movement logic for either the player or the mobs. Since each mob has its own movement logic, it will need to check its surroundings for obstacles and such. 

And here is the problem - What would be the best way to do this, while maintaining a solid code structure?

In my first project, each mob that was created received a reference to the world array, which fixed the problem. This time I was thinking about making the level array static, and then use a public static property to access it from wherever I would need it, but both of those solutions seems.. wrong somehow to me? I mean making my mobs dependent on a static variable in another class, doesn't that somehow break the whole idea of object oriented programming?

Passing down a reference somehow seems more preferable, but again, it seems wrong. I would then make the mobs dependent on the level class to be initialized, before it would even be possible to initialize it properly...

 

Am I seeing ghosts here? Or did I miss a much smoother option? 

 

Thanks for reading.

Share this post


Link to post
Share on other sites
Advertisement

Just some questions, what is your level structure like?. Are you working on a 2D environment?. Is it tile based?... Say you're working on a tile based world stored on a Level class... Well, you could go like this:

 

Have a game_control class, have it contain both the level and the game actors. When it comes the time to do your logic, have the game_control iterate your game actors and for each of them, have it check against the level class. You could have a method on it returning a list of relevant - read, surrouding - tiles depending on the actor position and then do your checks against them.

 

That's just one option, of course. I hope someone more experienced can provide you with better solutions :).

Share this post


Link to post
Share on other sites

I don't really have an issue with your actors knowing about your map. It doesn't really make much sense to me to instantiate an actor with no preëxisting world in which to place it.

 

If you really want to, you could wrap the logic into its own class, and have it hold the map reference. Then in your update loop you would maybe construct a vector of actors that need updating, feed the vector to the logic class, and let it iterate through their locations and determine where they're going.

 

But I really think your first solution is fine unless it actually starts giving you problems.

Share this post


Link to post
Share on other sites

Your base class for your characters should have virtual functions such as Update.  The implementation of Update will be different for each unique character class that inherits from the base class.  Now from the engine's point of you, it just need to call Update on your class, and by polymorphism, each character will do what it is supposed to do.

Share this post


Link to post
Share on other sites

It depends a lot of the structure of the game and what you're trying to do.

 

I.e. if you're making a pac-man game then you know the ghosts need to obtain information like the location of the player and other ghosts in comparison to them. You also know the map needs to be able to spawn things like bonus items.

 

Using that train of thought you can make a list of what each things needs to be able to access and try to design a single point of cohesion to it. I've personally found this to be one of the harder parts of designing. I.e. who should the ghosts talk to in order to get the location of the player, should the map have functions that locate characters and each character has a reference to the map? Or you could use something like a game referee class that has access to the map and sort of "moves the pieces around" based on its state vs the gameplay rules.

 

In general for something like what you're talking about you have to decide how the mobs will "find things" using the map is an okay idea since you can have functions that belong to the map that help return things in range of you, in that case you would either want to create the mob with a reference to the world or map object it belongs inside or pass it into the update function, you could also do something like make a "third party" object that all the map entities talk to in order to locate other things on the map and let them compare their positions.

 

Part of why I hate programming, there's a billion ways to do everything and many of them are just as "correct" as another.

 

In your case this applies as delegation of responsibility, do the mobs just get put into the world and act by themselves or are they just puppets some scripted object needs to control? In a game like minecraft for instance the mobs just get tossed into the world and get an update method called on them and they use the world object to find players and obstacles and decide on their behavior.

Share this post


Link to post
Share on other sites

Hey all.

 

Thanks a lot for your replies. I've read through them all and based on that I've decided to use the first method I wrote- Giving a reference to the level to each mob as they are instantiated.

 

Just some questions, what is your level structure like?. Are you working on a 2D environment?. Is it tile based?... Say you're working on a tile based world stored on a Level class... Well, you could go like this:

 

Have a game_control class, have it contain both the level and the game actors. When it comes the time to do your logic, have the game_control iterate your game actors and for each of them, have it check against the level class. You could have a method on it returning a list of relevant - read, surrouding - tiles depending on the actor position and then do your checks against them.

 

The game I am working on is a 2D tile based game. I really like the idea of a game control object, this way each entity in the game can have their own movement logic, and afterwards the game control unit can handle any collision that might need attention.

 

I don't really have an issue with your actors knowing about your map. It doesn't really make much sense to me to instantiate an actor with no preëxisting world in which to place it.

 

If you really want to, you could wrap the logic into its own class, and have it hold the map reference. Then in your update loop you would maybe construct a vector of actors that need updating, feed the vector to the logic class, and let it iterate through their locations and determine where they're going.

 

But I really think your first solution is fine unless it actually starts giving you problems.

 

You have a good point. The map will always be instantiated first, and then after, the mobs will be placed, based on data from the level class. I think I am going to stick with my first solution, since I can't really see that it should cause any problems.

 

Your base class for your characters should have virtual functions such as Update.  The implementation of Update will be different for each unique character class that inherits from the base class.  Now from the engine's point of you, it just need to call Update on your class, and by polymorphism, each character will do what it is supposed to do.

I might have been a bit unclear in my explanation above (I tend to be that, sorry). But I am actually using polymorphism. I have a base "Actor class" that has several virtual methods like Update and Draw. Both the player, and the mobs inherit from this base class. These are then called through the playscreens update and draw methods accordingly.

 

It depends a lot of the structure of the game and what you're trying to do.

 

I.e. if you're making a pac-man game then you know the ghosts need to obtain information like the location of the player and other ghosts in comparison to them. You also know the map needs to be able to spawn things like bonus items.

 

Using that train of thought you can make a list of what each things needs to be able to access and try to design a single point of cohesion to it. I've personally found this to be one of the harder parts of designing. I.e. who should the ghosts talk to in order to get the location of the player, should the map have functions that locate characters and each character has a reference to the map? Or you could use something like a game referee class that has access to the map and sort of "moves the pieces around" based on its state vs the gameplay rules.

 

In general for something like what you're talking about you have to decide how the mobs will "find things" using the map is an okay idea since you can have functions that belong to the map that help return things in range of you, in that case you would either want to create the mob with a reference to the world or map object it belongs inside or pass it into the update function, you could also do something like make a "third party" object that all the map entities talk to in order to locate other things on the map and let them compare their positions.

 

Part of why I hate programming, there's a billion ways to do everything and many of them are just as "correct" as another.

 

In your case this applies as delegation of responsibility, do the mobs just get put into the world and act by themselves or are they just puppets some scripted object needs to control? In a game like minecraft for instance the mobs just get tossed into the world and get an update method called on them and they use the world object to find players and obstacles and decide on their behavior.

This was very helpful. The idea of having methods in the map that assists mobs in finding things near it.. That's a splendid idea, I haven't thought of that. Most of my mobs are going to be more or less static in their behavior, like moving around until they encounter an obstacle, then turn around. Bosses and at least one of the mobs will need to know where the player is though. 

Nice to know about how things work in Minecraft- Being a Minecrafter myself, I've always been curious about how things work. 

 

Thanks a lot for all of your replies. You guys have some very nice ideas, that I can definitely use.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!