I have a script attached to an AnimationPlayer
node and it does some long calculations,
so in order to avoid the game engine from hanging while those calculations take place
I created a separate thread for that function,
but the seek()
function doesn't update the animation despite adding update=true
I've narrowed it down to this simple example:
extends AnimationPlayer
tool
export(float,0,1,0.1) var aniTimer = 0 setget aniTimer_Changed;
var thread;
func sep():
self.seek(aniTimer,true);
# calculations #
func aniTimer_Changed(new_val):
aniTimer=new_val;
thread = Thread.new();
thread.start(self, "sep");
here's how the tree looks like:
so how to I get the seek()
to work or is there any workaround with what I'm trying to achieve?
Edit:
I tried applied the solution @Theraot gave & modified it to loop through all the animations
like this:
func sep(thread:Thread):
var AnimationList=self.get_animation_list();
for animation_name in AnimationList:
self.current_animation=animation_name;
var ongoing_animation=self.get_animation(animation_name);
for track_indx in ongoing_animation.get_track_count():
for key_indx in ongoing_animation.track_get_key_count(track_indx):
var key_time=ongoing_animation.track_get_key_time(track_indx, key_indx);
self.seek(key_time,true);
Translate=false;
property_list_changed_notify();
thread.call_deferred("wait_to_finish")
func Translate_Changed(new_val):
if _thread == null or _thread.is_active():
_thread = Thread.new();
_thread.start(self, "sep", _thread);
But when I run this for a big animation it gets stuck in between & the entire godot game engine hangs
I'm guessing it's a memory leak?
what am I trying to achieve?
I've actually created custom properties on a node and added those custom properties as keys in an AnimationPlayer
but these custom properties all effect position
, rotation
& other inbuilt properties.
so I thought by seek()
I could see the end result of all them combined and then key the position
, rotation
& other inbuilt properties to another AnimationPlayer
AnimationPlayer
replacement with a different set of features. Edit: if you need to seek before the computations, I'm guessing you don't have to do it from another thread, do you?AnimationPlayer
with upgrades. Edit part: I don't understand what you mean but I'm using only 1 threadseek
must happen before the computations, you could callseek
on the main thread before starting the one you create for the computations. In other words you could callseek
inaniTimer_Changed
and let only the computation tosep
. However, it appears the Animation panel will not change regardless.old_ani
, so I'm not sure how to test this. However, you are callingseek
for every key frame, for every track, for every animation. Sinceseek
will be moving nodes around, it will cause redraws and UI updates that must happen on the main thread, I guess that is what is making Godot unresponsive. I don't know where you are going with this, but hopefully there is a way to avoidseek
, or at least reduce the number of calls you have to do.yield(get_tree(), "idle_frame")
either on the outer or the innerfor
loop, that might be enough to keep Godot responsive. By the way, I had to add a call tostop
because the last animation continued playing after theThread
finished.