Interaction between scenes

Godot Version

4.6.2

Question

i have 2 scenes, one containing my game character and one a killzone, i would like to make the character play an animation and change one of it’s variables once it enters the killzone but since they are 2 different scenes(which i need them to be in order to be re usable) i can’t get them to do that, all ‘ve done for now is making the killzone remove the collider but i can’t seem to find a way to change it’s variable or play the animation i want it to play

If your kill zone is an area or physics body, and your player is too, it can detect the player through collision. Then use a function on the player.

Typically this is done through a body_entered signal assuming your killzone extends Area2D and your player is a CharacterBody2D. Giving your players a class_name also helps.

# connected kill zone signal through the editor
func _on_body_entered(body: Node2D) -> void:
    # here `Player` assumes your player's script has `class_name Player` at the top
    if body is Player:
        # call the player's function to act appropriately
        body.kill()
1 Like

I don’t need to delete the player and everything, i need to interact with the player’s variables and animated sprite, which are well, in the player scene, while the kill zone is in it’s own scene, the most i could do was

“““Func on_body_entered

Body.get_node(“CollisionShape2D).queue_free”””

it obviously just removes the collision box but i need it to play an animation and change a variable that is inside the player’s script

I didn’t mention deleting the player and everything, my sample used body.kill() which isn’t a built-in Godot function, you would be making said func kill() in your player’s script.

1 Like

If u create a variable in gdscript in the player script like say

@onready var animated_sprite:AnimatedSprite2D = $AnimatedSprite2D

U should be able to access its methods like this, even if it is in the Area2D entered method

player.animated_sprite.current_animation=“what_you_want_to_play”,

If u are using an animation player, just expose it in the player script and then call it from the area2d with dot referencing. Basically, player.animationplayer.play(“whatever”)

P.S You sound quite new, so I recommend you doing Brackey’s and Clear Codes godot tutorials first. That should cover the basics for platform and 2D games

Also, I think @gertkeno’s advise on invoking it from the player as a function is correct. I am suffering from abit of brain fog before I realised yes, the die function should be in the player script and called from there to do everything that involves dying

Oh oarry didn’t realize, that worked btw! So thanks