Godot Version
4.6
Question
I am new to Godot, and game creation as a whole, and following a guide as to how to get started. My issue is that I get an error that I cannot find the solution to by myself, in the comments of the guide, or on this forum.
There is a global variable being Autoloaded called GlobalPlayerManger that loads the script below, the error points to the player = PLAYER.instantiate() line for the error.
extends Node
const PLAYER = preload("uid://7rksxinscnwf")
var player : Player
func _ready() -> void:
add_player_instance()
func add_player_instance() -> void:
player = PLAYER.instantiate()
add_child( player )
pass
The error was there previously when a script for Player Interactions was created but I could work around it and was hoping to learn enough to solve it myself by the time it would be a real issue. It was at that time pointing to the @onready var and most likely will do so again if the issue above is solved.
class_name PlayerInteractionsHost extends Node2D
@onready var player: Player = $".."
# Called when the node enters the scene tree for the first time.
func _ready():
player.DirectionChanged.connect( UpdateDirection )
pass # Replace with function body.
func UpdateDirection( new_direction : Vector2 ) -> void:
match new_direction:
Vector2.DOWN:
rotation_degrees = 0
Vector2.UP:
rotation_degrees = 180
Vector2.LEFT:
rotation_degrees = 90
Vector2.RIGHT:
rotation_degrees = -90
_:
rotation_degrees = 0
pass
The player.gd script looks like this
class_name Player extends CharacterBody2D
signal DirectionChanged ( new_direction : Vector2 )
signal player_damaged( hurt_box : HurtBox )
const DIR_4 = [ Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT, Vector2.UP ]
var cardinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO
var invulerable : bool = false
var hp : int = 6
var max_hp : int = 6
@onready var animation_player : AnimationPlayer = $AnimationPlayer
@onready var effect_animation_player: AnimationPlayer = $EffectAnimationPlayer
@onready var hit_box: HitBox = $HitBox
@onready var sprite: Sprite2D = $Sprite2D
@onready var state_machine: PlayerStateMachine = $StateMachine
@onready var shadow: Sprite2D = $Sprite2D/ShadowSprite
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
PlayerManager.player = self
state_machine.Initialize(self)
hit_box.damaged.connect( _take_damage )
update_hp( 99 )
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
direction = Vector2(
Input.get_axis("left", "right"),
Input.get_axis("up", "down")
).normalized()
pass
func _physics_process(_delta: float) -> void:
move_and_slide()
func SetDirection() -> bool:
if direction == Vector2.ZERO:
return false
var direction_id : int = int( round ( ( direction + cardinal_direction * 0.1 ).angle() / TAU * DIR_4.size() ) )
var new_direction = DIR_4[ direction_id ]
if new_direction == cardinal_direction:
return false
cardinal_direction = new_direction
DirectionChanged.emit( new_direction )
sprite.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
return true
func UpdateAnimation( state : String ) -> void:
animation_player.play( state + "_" + AnimDirection() )
pass
func AnimDirection() -> String:
if cardinal_direction == Vector2.DOWN:
return "down"
elif cardinal_direction == Vector2.UP:
return "up"
else:
return "side"
func _take_damage( hurt_box : HurtBox ) -> void:
if invulerable == true:
return
update_hp( -hurt_box.damage )
if hp > 0:
player_damaged.emit( hurt_box )
else:
player_damaged.emit( hurt_box )
update_hp( 99 )
pass
func update_hp( delta : int ) -> void:
hp = clampi( hp + delta, 0, max_hp )
PlayerHud.update_hp( hp, max_hp )
pass
func make_invulernable( _duration : float = 1.0 ) -> void:
invulerable = true
hit_box.monitoring = false
await get_tree().create_timer( _duration ).timeout
invulerable = false
hit_box.monitoring = true
pass
Folder structure is res://Player/Scripts/ for scripts and the CharacterBody2D scene is under res://Player/ and the Autoload script is under res://00_Globals/
Please let me know how I can solve this issue and if more information is needed to help me.
Thank you