Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Downcasting with iterator

Started by Sivak Apr 4, 2010 at 9:44 PM 5 replies 1.2k views
Original Post
Sivak
Sivak
Hi all. I have a situation where I have two subclasses of a master class. Basically, when I'm updating one of them, I'd like to be able to modify another variable that's in the other subclass. Here's the code...

	std::list<GameObject*>::iterator it3;
	it3 = game.GameObjs.begin();
	while(it3 != game.GameObjs.end())
	{	if((*it3)->objectType == ENEMY_SHOT)	//Only check for enemy bullets...
		{	
Someone had mentioned doing a static_cast, but I can't make any code actually compile. I'm doing something wrong syntactically... But basically, I just want to access the variable in the subclass and change it. Help appreciated thanks.
Sivak
Hodgman
Hodgman
As shown in your code (*it3) gives you a 'GameObject*'.
So if you wanted to cast that pointer to a derived type, you should be able to write: static_cast(*it3) (assuming DerivedObject inherits from GameObject, and the GameObject pointer is actually pointing to a DerivedObject instance so the cast is valid)
Sivak
Sivak
Quote:
Original post by Hodgman
As shown in your code (*it3) gives you a 'GameObject*'.
So if you wanted to cast that pointer to a derived type, you should be able to write: static_cast(*it3) (assuming DerivedObject inherits from GameObject, and the GameObject pointer is actually pointing to a DerivedObject instance so the cast is valid)


Excellent! Thank you. I got it working. Just was thrown for a loop on the syntax.

But yeah, I am being careful with this static casting. :)
Sivak
rip-off
rip-off
Why do these shots need special handling? This is a symptom of a disease in your hierarchy design. Type-switching and casting cures the symptom, but you will likely run into similar problems again unless you find the root cause.

If they *really* need special handling, perhaps you should store them (or reference them, using a shared smart pointer) in a separate container. If the type information is important, don't throw it away. Or maybe this logic would work for all projectiles, so perhaps game objects and projectiles have enough difference in how they're handled that they shouldn't be stored together. Remember, just because you could say "EnemyShot is-a GameObject", doesn't necessarily mean this is the best way to model it in your code.

Another possibility is to add another virtual function to the game object interface, which other classes implement as an empty function. This is probably not the best solution here, but again it depends heavily on what you are trying to achieve.
Sivak
Sivak
Quote:
Original post by rip-off
Why do these shots need special handling? This is a symptom of a disease in your hierarchy design. Type-switching and casting cures the symptom, but you will likely run into similar problems again unless you find the root cause.


Okay, what basically happens is the player object updates and checks for being hit by shots. There is a var that the shots and ONLY the shots have, which would be a bool that says whether or not the bullet was grazed (i.e. comes close to your hitbox, but doesn't hit it). That gets set to true so that way a bullet can be grazed only once.

I needed to do it this way because having the player look for shots is one loop, whereas shots looking for player would have needed me to backup the iterator for the player and use that.

If you have a better suggestion, lay it on me. I'm all for good design.
Sivak
theOcelot
theOcelot
I would think that it would be better to let the bullet set its own grazed flag based on information it receives from the world about collisions. That, or put shots in a separate list, and check for collisions between the lists.

You might benefit from a "master list" of all objects for updating, and then separate collision lists with specific types of objects. This gives you some flexibility in which types get collided with which, without constantly checking the runtime types of two potential collidees. But I don't know how that would work in your specific situation.
rip-off
rip-off
I concur, managing the shots in a separate list would probably be better. The player probably shouldn't be checking if it has been shot. Instead, the calling code should notify the player when they get hit. This way, you can keep your logic in one loop:
for shot in shots:   if collision(shot, player):       player.onShot()       shot.onHit()

Topic Locked

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

Sign in to reply to this topic.