I have a problem with pathfinding on a separate thread. I saw some similar questions here but none of the answers seem to be working for me - I'm clearly misunderstanding something very basic.
I have a scene with multiple enemies in it which are kinematic bodies that follow paths on a navigation node.
I also have a global script (singleton) with a pathfinding function. Each enemy sends a path calculation request to that function every second or so. I want that function to run on a separate thread. My singleton script is this:
thread = Thread.new()
#this is the function called be each enemy. It's passing in some data
func get_path_to_target(me, target_pos, path, nav):
agent = me
navigation = nav
thread.start(self, "calcpath", target_pos)
var requested_path = thread.wait_to_finish()
path = requested_path
var pth = navigation.get_simple_path(agent.global_transform.origin, target_pos)
This theoretically works but the FPS drops to like 20 and there is a significant stutter each time an enemy requests a path. What am I doing wrong?