Game Development Community

InputMaps and player respawns

by Thomas Wrather · in Torque X 2D · 12/01/2007 (7:15 pm) · 9 replies

When the game starts up, a player is spawned. In the player's movement component various keys are bound to the InputMap. Everything works fine at this point.

When the player dies, it is marked for deletion and a new player is spawned 5 seconds later. The same code for binding the keys to the input map is run when the new player is spawned. However, now the keys no longer work. Is there some InputMap cleanup that I'm supposed to do before deleting the player?

#1
12/02/2007 (4:12 pm)
I've been able to reproduce the problem with involving player respawn at all.

In the _SetupInputMap method of the MovementComponent replace the line:

PlayerManager.Instance.GetPlayer(playerIndex).ControlObject = player;

with

PlayerManager.Instance.GetPlayer(playerIndex).ControlObject = player;
PlayerManager.Instance.GetPlayer(playerIndex).ControlObject = player;

With those 2 lines, the player cannot move.

OK, in this form the above code is a bit silly, but this is indirectly what I do when I respawn a player. Shouldn't it be legal to set the ControlObject multiple times?
#2
12/03/2007 (7:56 pm)
Are you cloning a template object to spawn your player? Check out the "Hooking up the Code" section of the Blaster tutorial. It covers spawning a player object there, and also covers player destruction in the "Collision Handling" section. I have no problem losing movement control when I use that method. The Blaster tutorial is awesome in general.

The behavior when setting the ControlObject to the same object twice is sort of buggy I guess, but you're not doing that when you use a template and clone it to spawn an object. The cloned object is a new object.
#3
12/03/2007 (8:46 pm)
Yes, I'm basically using the method described in the Blaster tutorial. The main difference is that instead of calling SpawnPlayer in the OnCollision method, I call Schedule to cause SpawnPlayer to run 5 seconds after the collision.
#4
12/04/2007 (12:22 pm)
I whipped up a quick version of the blaster player respawn and had no issues. Could you post your respawn method and some code from where you kill the player? I want to help more, but I can't seem to duplicate your problem.
#5
12/04/2007 (6:52 pm)
I'll try to come up with a version of the code that is not full of distracting game details. :)

I tried mod'ing the blaster tutorial to mimic my code. Of course it worked fine!

I seem to have some type of race condition. If I change my code to respawn immediately in the collision delegate, movement works after the respawn. The problem only occurs when I delay the respawn. However, the blaster tutorial works fine when delaying the respawn.
#6
12/04/2007 (7:55 pm)
I just found out that if I add the following line to the MovementComponent _OnUnregister method, the movement keys work after the respawn:

PlayerManager.Instance.GetPlayer(_playerNumber).ControlObject = new TorqueObject();

Maybe someone with access to the PlayerManager source code could shed some light as to why this fixes my problem?
#7
12/04/2007 (8:10 pm)
@Thomas - There is a bug which causes input maps to get disconnected when you assign the same object to be the ControlObject. Part of the code disconnects new input when a non-null object is assigned to the control object, but then another part of the code detects that the new object being assigned is the same as the old object, so it doesn't connect it, figuring that it's already connected. That's why you were seeing a problem when you assigned it twice.

Still, it's a little puzzling that you would see it if you're spawning a new object. Are you using object pooling by any chance?
#8
12/04/2007 (8:14 pm)
I have access to it, but I'll leave that question to an expert. :)

I traced it yesterday, but I don't recall what conclusion I came to. I commented out an if block that returned early along the way and I was able to set the same controlObject any number of times in a row without it breaking.

Edit: these guys are fast sometimes.

Quote:another part of the code detects that the new object being assigned is the same as the old object, so it doesn't connect it

that's the if block i was referring to. :) he explained it so well.
#9
12/05/2007 (7:15 pm)
@Dan - Yes, I am using object pooling. As a test, I turned off pooling and that also fixes the problem. For the player object I guess I don't really have to pool since the player shouldn't be respawned very often.

Thanks for everyone's help on this!