So, I just finished the brackeys tutorial on godot and everything seemed rather easy. for those who haven’t seen it it doesnt really affect the question its just a good point of reference for my code.
So, I have made a scene called “killzone” which has the following code in it
I want it to make the player play a specific animation when he enters the _on_body_entered(body) is run, but since the killzone and the player are in difrent scenes I cant find a way to do that. I tried using signals but it doesnt seem to work. Any ideas?
side note: the killzone scene is also used for enemies (imagine gumbas from mario) which when you touch you die, so I want the animation to play there too.
If your killzone script is on the root node of the killzone, and the function you need to call in the player scene is also defined in a script on the player scene’s root node, then you should be able to directly connect that in the main scene.
However, if either is on a node that’s nested inside one of those scenes, then they can’t be accessed in the main scene. In that case, you can create an autoload to act as a signal bus. It could look something like:
class_name SignalBus # this is the autoload
extends Node
signal player_died
And then in the killzone script:
func _on_body_entered(body):
print("You died")
SignalBus.player_died.emit() # this line is new
Engine.time_scale = 0.5
timer.start()
And finally, somewhere in some script in the player scene:
func _ready():
SignalBus.player_died.connect(_on_player_died)
func _on_player_died():
# do stuff
And, well, this means that now any node in any scene can ask to be notified when the player dies, which might come in handy later.
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -250.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite = $AnimatedSprite2D
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
#Get input direction: -1,0,1
var direction = Input.get_axis("move_left", "move_right")
#Flip sprite
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
#Play animations
if is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
#Apply movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
sadly no it doesn’t work. No errors no crashes no anything it just does everything besides that which I find interesting. I’ll see what else I can do . If you have any idea feel free to say