thread.gd 665 B

1234567891011121314151617181920212223242526272829
  1. extends Node2D
  2. # Member variables
  3. var thread = Thread.new()
  4. # This function runs in a thread!
  5. # Threads always take one userdata argument
  6. func _bg_load(path):
  7. print("THREAD FUNC!")
  8. # Load the resource
  9. var tex = ResourceLoader.load(path)
  10. # Call _bg_load_done on main thread
  11. call_deferred("_bg_load_done")
  12. return tex # return it
  13. func _bg_load_done():
  14. # Wait for the thread to complete, get the returned value
  15. var tex = thread.wait_to_finish()
  16. # Set to the sprite
  17. get_node("sprite").set_texture(tex)
  18. func _on_load_pressed():
  19. if thread.is_active():
  20. # Already working
  21. return
  22. print("START THREAD!")
  23. thread.start(self, "_bg_load", "res://mona.png")