|
|
|
|
Reply From: |
kubecz3k |
You need to check which object is getting freed, and instead of storing reference to this object you should store weakref
to it. Thanks to weakref
you will be able to check if object is still alive: Detect if an object reference is Freed? - Archive - Godot Forum
"While you know the object is stll alive:
var wr = weakref(object);
Then you can check like this:
if (!wr.get_ref()):
#object is erased
else:
#object is fine so you can do something with it:
wr.get_ref().doSomeOperation();
"
I can’t do this because the object on which I call set_translation
is implicitly self
:
func _process(delta):
_anim_time += delta
var k = _anim_time
if k < 1.0:
var pos = _normal_pos
pos.y -= (1-k)*(1-k) * 64.0
set_translation(pos)
else:
set_translation(_normal_pos) # here
set_process(false)
My script basically sets the translation of self
in process, then suddenly it becomes freed…
I should have mentionned that my game uses threading. It’s not directly related to my script, but if I remove the thread I can’t reproduce the error (but the game gets laggy).
Basically the thread performs calculations, notifies the main thread with call_deferred("thread_finished")
, and then in thread_finished
I wait for the thread to finish and spawn the MeshInstance on which my script is.
Zylann | 2016-05-02 14:03
Can a weakref() be invalidated between the time it is checked and the time it is used in the method? Safer would be
var ref = wr.get_ref()
if ref:
#object is fine so you can do something with it:
ref.doSomeOperation();
else:
#object is erased
paul.holt | 2019-04-28 14:00