Godot Version
4.3
Question
I have been following Brackey’s tutorial here, and have run into a snag when handling the player’s death.
What I’ve done (in following the tutorial) is to create a Scene called “killzone” (35:30 in the video if you care to watch it) which can then have some sort of CollisionShape2d attached to it to make it multipurpose. The intent here, as I understand it, is to have a “general purpose” Scene that can be used in a variety of ways to achieve the same end result: handle the player’s death.
In addition, the player character has it’s own Scene and associated script to handle movement, animation, etc.
My goal here is to expand the death functionality of the game so that the player’s speed drops to 0 and the game plays a death animation while the game restarts.
As I understand it, there are two ways to do this:
Create a reference to the player.gd script to directly modify the speed and animation from within the killzone.gd script. The tutorial made it sound like I could ctrl+click drag scenes into scripts in order to make references to them (and their subordinates), but that doesn’t seem to work when I try it with the killzone.gd script.
or
Use a signal in the killzone.gd that player.gd watches for and uses as a cue to set the player speed to 0 and change the animation. But when I put
signal isdead(status)
in the killzone.gd script, I get the following error on that line:
Unexpected "Identifier" in class body
As is likely very obvious at this point, I am quite new to GDscript and how different scripts interact with each other. There is clearly something fundamental I am missing here and I’m hoping someone can point me in the direction of some documentation or guide that will explain what I’m doing wrong and maybe even suggest a better way to do what I’m trying to do. Thank you in advance for your help!
killzone.gd:
extends Area2D
@onready var timer: Timer = $Timer
signal isdead(status)
emit_signal("isdead",0)
func _on_body_entered(body: Node2D) -> void:
print("you died")
emit_signal("isdead",1)
Engine.time_scale = .75
timer.start()
func _on_timer_timeout() -> void:
Engine.time_scale = 1
emit_signal("isdead",0)
get_tree().reload_current_scene()
player.gd:
extends CharacterBody2D
var SPEED = 130.0
const JUMP_VELOCITY = -300.0
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("move_left","move_right")
if direction > 0:
animated_sprite.flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
if is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()