Jump to content

  • Log In with Google      Sign In   
  • Create Account

Handling of game objects (projectiles, players etc)

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.

  • You cannot reply to this topic
6 replies to this topic

#1   Members   -  Reputation: 1126


Posted 07 May 2012 - 06:28 AM

I started to write some code for weapons and firing them when I realized the structure of the game code was a bit weird. I store an array of players in my Game class, I store an array of enemies in my AIManager class but I didn't know where to store the projectiles. I wanted to store the projectiles in Game first, but I can't "put it there" because Game includes Projectile.h and Projectile.h would then have to include Game.h to be able to call its methods, right? Now I wonder how a good way to structure all of the game code would be. It's a very broad term I know and there are probably very many different ways to go about it.

Say I have stored my projectiles somewhere and then I want to detonate it and deal damage to everyone around it, then I need access to all players and enemies and perhaps doodads to check the distance and calculate damage. But how do I access it? What's a general good advice? I thought of sending a pointer to the array of objects to be possible victims but then again I stored all objects in different arrays depending on what kind of object, and in different places and I then I took a break.

What are some general ways to structure this whole mess? I want to have some kind of decoupling of the responsibilities. For example the renderer class is separate from everything. I would like the objects to handle themselves like the projectile would do the exploding itself and doing damage in its own method or something similar.

I suppose the question is kind of unclear and I'm sorry for that, but I don't really know how to put it in a different way. I'm very thankful for any input I may receive!

EDIT: I was thinking of a system where every object handled everything themselves, collision detection and response, updating health points, animations and other possible events. I was looking at the SC2 map editor and it seems kind of complex but incredibly powerful and flexible. Are they doing something close to this?

Right now the code is kind of like containers of players, projectiles, enemies and the Game performs the calculations and updates, I suppose it would generally be a better idea to leave this to the individual objects considering they do different things in their updates. But then I imagine everything would be like a huge array of objects that are just looped through and have their Update method called, more or less. What are you thoughts on this?

Edited by Hannesnisula, 07 May 2012 - 08:50 AM.

#2   Members   -  Reputation: 1126


Posted 07 May 2012 - 09:50 AM


#3   Members   -  Reputation: 279


Posted 07 May 2012 - 10:30 PM

I don't really understand why your code in ProjectTile.h should call methods from Game.h... Normaly I would call methods in the source file and not the header.

Although it is doable, I think the method you want to choose, is a complex one. A tile that handle an explosion and damage other tiles? Is that really the responsibility of the tile, or is that the responsibility of the "Explosion". It is a though decission and both ways are correct, but in my opinion, putting to much logic in an object is generally not a good thing.
Other than this I can't give you much advise...

#4   Members   -  Reputation: 440


Posted 07 May 2012 - 11:15 PM

What I do is that I have all objects (player, enemies, projectiles) in a single array. They are all derived from a base class "Agent" and have a property Type, which says if they are creature or projectile.

So, if you had an explosion damaging only enemies, you could have:
	 foreach (Agent a in AllAgents)
		  if (a.Type == Enemy) a.Die();

#5   Crossbones+   -  Reputation: 2918


Posted 08 May 2012 - 01:36 AM

Storing all the objects in a huge array? Basically it is ok. However, for things such as projectiles and other short time existing objects, I'd make another system which would allow fast spawning of projectile objects and other stuff (spells etc)

Loop through the huge array to update them? Not a good idea. First of all, not all objects need to update every frame. Some objects need to update more often and others less often. So, if each object could be tweaked to update at desired interval you'd save a huge amount of CPU power. Also, the update frequency may be affected by things such as proximity to player.

Causing explosive damage to entities: I assume that you use some kind of accelerator tree (octree perhaps) for storing objects. You may use that tree to fetch objects at certain radius to your explosive center and then for each object send a message "explosion of x force at point x,y,z" and each object reacts to the explosion if they need to.

Render components are easily separated from the entities. You may create a separate octree for objects you wish to cull and render. You could check for discussions about game entities composed of components.

Best regards!

#6   Members   -  Reputation: 200


Posted 09 May 2012 - 08:35 AM

If you want to decouple your objects, why do they handle everything to begin with?

SC2 objects don't control collisions, they have attributes that tell the engine how it should handle them.

It makes a lot more sense (at least to me) that all objects are owned by some logic game class that actually runs the game. That class, then, has access to all the needed components (rendering code, physics code, etc.) and runs them on the objects it owns.

The objects themselves only own attributes to tell the game what they are and what to do with them (which by itself is a whole new topic on how to design this, e.g. Inheritence vs components).

#7   Members   -  Reputation: 279


Posted 09 May 2012 - 08:42 AM

I agree with wolfscaptain, I just didn't found the right words to say it Posted Image

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.