I wanted to do some background loading in my game. My idea was to detach the childs and do the calculations like the code below. But this was 10 times slower than just doing the calculations in the main thread because the remove_child and add_child took so long. Did I do something wrong with the code? Please be aware that the syntax maybe is wrong because this is just pseudocode as I'm usually programming in C# but I think this is the correct code for GdScript.

for i in range(0, 3):
  remove_child(childs[i])\
//...
// do calculations on child nodes in another thread

// check if the thread is done and re-add childs if it is or do something else if not.
for i in range(0, 3):
  add_child(childs[i])
8 months later

I'm not sure why - perhaps some locking issue because the thread is owned by the node you add children to? Something that seems to work is to use call_deferred instead. You can do this from the main thread as well, since the calls will be queued for execution at idle time.


for i in range(0, 3):
    call_deferred("add_in_background", childs[i])

func add_in_background(child):
    add_child(child)

a year later