disabling collisionshape has weird results

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mc_Cheadle

I am programming a game where two spaceships battle each other. once a player dies there is a victory screen. The player who won can still move around and shoot( which is what I want) and the other player should vanish from the screen. But I keep running into the problem that when I hide the player that lost, it technically is still there, so when the player who won shoots in the victory screen it sometimes just hits nothing because the losing players collision shape/polygon is still there. To get around this I tried a lot of things: first I tried to turn of the collision layer and mask from the loser which in theory should have prevented it from colliding with bullets. The second thing I tried was turning the monitoring and monitorable from the losing player to false. This also didn’t work. The last thing I tried (which was also the most obvious possible solution) was to disable the collisionshape/polygon of the losing player but I got some very weird results with that. The first situation was when I turned the collisionshape.disabled to true manually. This worked great since the bullets were going straight through the player but I still needed to have the player be hit when he wasn’t dead yet. So I tried to instead of disabling the collision shape manually, disabling it via code but then it got really weird. I found that whenever I toggled the collision shape from disabled = true to disabled = false, I wasn’t able to turn it back to true.

The code that I use is this:
in this bit of code I check whether a player has dropped to 0 hp. if it has it hides, the collision polygon is disabled and it is prevented from moving. It also initiates the victory screen through the game_over() function. I tried to get the state of the Collisionpolygon.disabled via the print function and the weird thing was that it always said true and false when it was supposed to but the players were still being hit even when their collisionpolygon was disabled.

func _on_Red_hit():
rhealth -= 1
if rhealth <= 0 and yhealth > 0:
	$Red/CollisionPolygon2D.disabled = true
	$Red.can_move = false

This is the code for the other player

func _on_Yellow_hit():
yhealth -= 1
if yhealth <= 0 and rhealth > 0:
	$Yellow/CollisionPolygon2D.disabled = true
	$Yellow.can_move = false

This is the code that’s inside the scene of both of the players that enables the CollisionPolygon again at the start of a new game.

func start(spawn):
position = spawn
can_move = true
$CollisionPolygon2D.disabled = false

first question: Can someone please explain why it is not working when I do it via code but it does work when I do it manually? I also don’t think that I wrote the code wrong since when I turned the collisionpolygon.disabled to true manually before the game starts and then turn it to false via code when the game starts the players are still hit and when I don’t turn it to false via code the bullets pass through the players.

Second question: Why am I failing to turn it back to true when a player dies?

Third question: Why does it say the collisionpolygon.disabled = true even though the player is still getting hit?

Last question: Have I overlooked something?

Ideally, you want to change the property like this:

$Red/CollisionPolygon2D.set_deferred("disabled", true)

This informs godot to do so when it’s ‘safe’ (kinda), propagating the change to the engine.

spaceyjase | 2023-01-19 13:47

I don’tknow why but that fixed it!! thanks

Mc_Cheadle | 2023-01-19 14:17

:bust_in_silhouette: Reply From: spaceyjase

Answered in the comment here: disabling collisionshape has weird results - Archive - Godot Forum