Question with signals

Godot Version

4.2.2

Question

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

extends Area2D
@onready var timer = $Timer

func _on_body_entered(body):
	print("You died")
	Engine.time_scale = 0.5
	timer.start()


func _on_timer_timeout():
	Engine.time_scale = 1
	get_tree().reload_current_scene()

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.

They are different scenes, but body is the node that entered the killzone, it must be the player

func _on_body_entered(body):
	body.animation_player.play("died")

	print("You died")
	Engine.time_scale = 0.5
	timer.start()
1 Like

this seems like a step to the right direction its just that I get this error message when I use it:

invalid get index ‘animation_player’ (on base: ‘CharacterBody2D (player.gd)’).

body is the player yeah

Yeah that’s just an example, I have no idea how your player is set up or how you want to play animations on the player.

here is my player.gd file if that helps?

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()

Then it would probably be body.animated_sprite.play("my animation")

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 :smiley:

Well the other problem is your player script is always playing a different animation, you would have to tell the _physics_process function to stop.

Logically one of these animations will always trigger being played every frame.

if is_on_floor():
	if direction == 0:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("run")
else:
	animated_sprite.play("jump")

oh my god… Im so dumb. Thank you so much

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.