0 votes

I'm trying to make enemies perform certain behaviors when a player is present in the scene but when the player has queue_free(), instead of the application crashing, the enemies would change behaviors. I tried using weakref but the application just freezes when the enemy is present. Any suggestions? Here is my code:

onready var plr = get_node("/root/game/player")
var plrw = weakref(plr)

func _process(delta):
    if !plrw.get_ref(): # Has the player gone
      move_local_y(0)
    if plrw.get_ref():
            if get_pos().y < plr.get_pos().y:
               move_local_y(SPEED)
            if get_pos().y > plr.get_pos().y:
               move_local_y(-SPEED)
            pass
 pass
asked Mar 30, 2018 in Engine by Dava (115 points)

1 Answer

0 votes

I used Object.is_inside_tree() instead of Object.get_ref() and it now works as expected!

answered Mar 30, 2018 by Dava (115 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.