Searching for nearest x in group not working

godot 4.3

I’m trying to create a game where any monster you kill will realign on you side
this is handled by this piece of code

@onready var speed = 1.0
@onready var turnspeed = 4
@onready var health=8
var current_state="spawn"
var next_state="spawn"
var previous_state
var biting = false
@onready var player
@export var friendly = false
@onready var players
@onready var nearest_player
func _ready():
	change_groups()
	print(player)

func change_groups():
	#changes the aligment of the mob
	print(get_groups())
	if friendly == false:
		if is_in_group("player"):
			remove_from_group("player")
		if is_in_group("enemy")==false:
			add_to_group("enemy")
		players= get_tree().get_nodes_in_group("player")
		nearest_player= players[0]
		for player in players:
			if player.global_position.distance_to(global_position) < nearest_player.global_position.distance_to(global_position):
				nearest_player = player
	else:
		player=get_tree().get_first_node_in_group("enemy")
		if is_in_group("enemy"):
			remove_from_group("enemy")
		if is_in_group("player")== false:
			add_to_group("player")
		players= get_tree().get_nodes_in_group("enemy")
		nearest_player= players[0]
		for player in players:
			if player.global_position.distance_to(global_position) < nearest_player.global_position.distance_to(global_position):
				nearest_player = player
func _physics_process(delta):
	realign()
	#state machine yayyyyyyy
	if previous_state!=current_state:
		$StateLabel.text=current_state
	previous_state = current_state
	current_state = next_state
	
	match current_state:
		"spawn":
			spawn()
		"bite":
			bite()
		"chase":
			chase(delta)
func chase(delta):
	velocity = (nav.get_next_path_position() - position).normalized() * speed * delta
	look_at(player.position, Vector3.UP)
	rotate_y(deg_to_rad(rotation.y * turnspeed))
	if  player.position.distance_to(self.position) < 2.5:
		next_state="bite"
	if  player.position.distance_to(self.position) > 1:
		nav.target_position = player.position
		move_and_collide(velocity)```
but when my state machine gets to "chase" it crashes since player is still a null value. could anyone who is more experienced than me in godot help me with this

To handle the null player, you could add a check to only run the code in chase() if the player isn’t null.

func chase(delta):
    if player:
        velocity = (nav.get_next_path_position() - position).normalized() * speed 
        ...

thank you but i was more wondering why searching for the closest player returned null

I might be misreading it, but I don’t think you set player at all?

friendly is false so when you call change_groups() in _ready() it processes the if block not the else. Looks like you only set player in the else. So when chase is called player hasn’t been set.

Unless player is set in code you’ve not posted above.

i did set it under the if block tough

The player in the for loop isn’t the same player as the var player set at the top of your script. chase() is checking the latter.

You may have a warning in the editor about a variable shadowing another of the same name. It’s always best to use different names for variables to avoid mistakenly setting the wrong variable/simplify debugging.

thank you now I understand

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