Avoid getting "Invalid get index (previously freed instance)" error?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MAxSaL


I have been running into an error, which I don’t understand. It crashes my game and it looks like it happens when I try to set the position of an object that is not there. However, before setting the position, I check if the object is there.

func _process(delta):
if carried_object:
	carried_object.position = position + $Body.position + Body/CarryPosition.position

The error occurs when I try to set the position of carried_object, and the debugger marks the variable as unoccupied. However, shouldn’t the line, if carried_object: prevent this from happening? I assign carried_object a value in _physics_process.

I hope you can help me. Thanks :slight_smile:

:bust_in_silhouette: Reply From: Archtects

I had this problem as well. I assume carried_object is an onready var?

onready var Life = get_node("/root/Main/GUI/Game/Score")

You have to call the node in functions. Otherwise restarting the code explodes in ball of flames. Idk why. I ended up doing this.

extends CanvasLayer

onready var Score
onready var Life

Then in my function

func updatescore():
	Score = get_node("/root/Main/GUI/Game/Score")
	Score.text = str(global.score)

Idk if there is an easier way to do it? But thats how i fixed the issue

Thanks for your help. However, I have not been able to transfer this concept to my code. carried_object is a node which is spawned in the game. When I tried to re-define the node its movement started lagging… But thanks anyway :slight_smile:

MAxSaL | 2018-09-26 14:07