thread.gd 646 B

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