The boss enemy can't find, the player in the scene tree

Godot Version

4.2.1

Question

Hello. I have added a very simple boss fight to my game, a boss that is using a finite statemachine it only has 1 state currently and that is the ‘chase’ state that is supposed to chase the player when the boss fight is triggered, when I load the scene that contains the boss fight it works how it should, however when I load a different scene and I move into the scene with the boss fight the boss can’t find the player node in the scene tree for some odd reason. I can’t figure out what I need to do in order solve this problem.

This clip shows exactly, what I mean.

Boss chase state code

extends State
class_name BossChase

@export var boss: CharacterBody2D
@export var movementSpeed : float = 15
var target = null

func _ready():
	target = get_tree().get_first_node_in_group("Player")


func physics_update(delta : float):
	if is_instance_valid(target):
		var direction = target.position - boss.position
		
		boss.velocity = direction.normalized() * movementSpeed

What the boss scene tree looks like
Skærmbillede 2024-06-10 195551

The player may not be ready by the time the troll is ready. Maybe spawn the troll when the player enters the arena? Another solution is waiting a single frame for everything to be ready.

func _ready() -> void:
    await get_tree().process_frame
    target = get_tree().get_first_node_in_group("Player")
1 Like

Well ok, that was a lot easier to solve than I thought XD

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