Hi guys, I’m trying to trigger a death animation.
I have a “killzone” set up as a separate area2D scene with a on_body_entered funtion and level reset in the script. I have a signal from “killzone” script connected to Player script and then from Player script I’m trying to trigger the death animation.
The signal is working because it does reset the level. I have disabled reset level for now and I’m just trying to get the death animation to work.
I have set a variable “alive” in the Player script and applied it to the if statements for each movement Inputs. It’s a jet pack game so there is only left right and jet pack UP controls for now. So I set alive to false in the signal and then try to run the death animation.
I’m not sure I’m getting the syntax correct. I’ve just started on Godot and I’m a begginer coding. Any help highly appreciated!
I’ve attached screen shots.
A side note generally you want to write var under signals, you have your timer var above your signal.
Also, on your second screenshot your killzone is showing it doesnt have a collisionshape attached not sure if that is intended.
In your movement code aswell, if you dont want your code to execute if player isn’t alive you can simply put an if statement at the top of the func() If not alive: return
Hi, thanks for taking the time to reply.
In the Killzone script I created the signal player_died and used the emit-signal
then the signal shows up in my signal list like below
Then I get this function showing in my Player script
func_on_killzone_player_died() -void:
So that’s how I learned to connect it but maybe that’s wrong?
The killzone has no collision shape because I use it as a template in different scenes and put the collison shape there so for different enemies and stuff that have different collision shapes.
math makes a good point, but I would still try and figure out why the signal isnt connected, for the learning process.
Okay, so you connected it in inspector.
I would confirm it is connected and emitting. Usually signals connected by inspector have a green symbol to the left of the func showing they are connected. I didn’t see that.
Have you placed a print statement in both the body entered next to the emit signal line and another in the players death func()? To test where it is failing?
I think you’re overcomplicating this, your killzone already get the player reference in _on_body_entered so you can call anything directly from player node like:
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
# You already has the player reference here, just call
# whatever you need here
body._on_killzone_player_died()
thanks for tips!
I use the killzone for example like here I have an enemy scene then I add the kill zone to it and then the collision shape, so I have another enemy with a different shape I can do the same and put a different collision shape. Not sure it’s the most practical way to deal with different enemies. I’m on a vertical learning curve at the moment
I’m gonna try a print statement and try to confirm it’s connected.
I tried doing it like you said in Main script and created a func called death in player script and then in Main script wrote the following
$killzone.player_died.connect($player.death)
I got an error so I’m not sure if I did that correctly either.
Really appreciate the tips though. Been a couple of days trying to the enemies going but hit a brick wall with the death animation.
What matt suggested is probably the best method. With the killzones as children of another node in the scene connecting signals will get a bit complicated. You want to start learning simple signals before trying to make more complicated signaling patterns.
How do you have your scene set up. Is Killzone a child of main?
Ok so I took away the signal and tried to call the animation in the killzone script
but it doesn’t do anything, I think it’s because it’s conflicting with my idle state calling the idle animation. So I think I need to check for a var alive but not sure how to do that in the killzone script.
@philwil2012 When you put the killzone area in the enemies, you connect them in the player script? Because the only zone i see connect the one inside the main scene, and this one dont have a collision shape so will do nothing.
My guess of why this doesn’t work is because the player is not connected with the other killzones player_died signal added in the enemies, especially because this is done in other scene, so you can’t see the player node to connect them by editor.
Well if killzone is sending the signal and is looking for if player entered it that will create a lot of problems. If you want to test out killzone interactions keep it as a child of main, but you will need to have a collision shape attached if you want it to detect body entered.
Do you have a collision shape on it while you are testing the signals? Is it registering _on_body_entered?
Do you know how to use print() statements for testing?
Try this to simplify, create a new temp scene and set it up like this.
TempScene
Player (as child of temp)
Killzone (as child of temp)
Collision zone (child of killzone)
ColorRect (child of killzone) just to see it visually
(I understand that your movement works on player) If this is true put the player under the killzone, and then test the game by moving player into the killzone.
on player
func death():
print("dead")
on killzone
func _on_body_entered(body):
print("works")
if body.is_in_group("player")
print("yep")
body.death()
This will allow you to test bare minimum and move from there.
Also, you are calling proto like it is a child of killzone in the code you most recently posted.
func _on_body_entered(body:Node2D) -> void:
if body.is_in_group("Player"):
$proto.play("explode") #proto is a child of player not of killzone
body._on_killzone_player_died()
it should be $Player/proto.play("explode") though I would advise against doing it like this in general, it is easier to break code when you are calling non-children directly like this.
Ok I got the following working so far. The print statement works so I guess It’s detecting the collision shape in my enemy scene
I’m not sure how to call this animation in the killzone script though. like you say it’s not a good idea to call non children.
It wasn’t working when I has if body.is_in_group(“Player”): so I removed it and it worked.
Also, really quick can you run the test again but change print(“dead”) on the body_entered to just print(body) and tell me which body it is printing.
It is hard to know what you are working with exactly, can you paste all your player code in here?
If you paste it then select it all and use the gear that is at the end of the bar with Bold and Italics it will show me better what might be the problem. Use preformatted text at the top.