I want to optimize pathfinding in my game so I have created an autoload to process this using the code below. The enemies call the register_enemy_function when they enter the chase state.
For some reason, the game crashes when I use the WorkerThreadPool but it works just fine when I use the for loop. Why does this happen? I’ve tried only using the WorkerThreadPool when there are 20 or more enemies, but that doesnt change anything.
extends Node
var enemies := []
var enemies_to_deregister := []
func register_enemy(enemy: Enemy) -> void:
enemies.append(enemy)
print("Registered new enemy, now handling " + str(enemies.size()) + " enemies.")
func deregister_enemy(enemy: Enemy) -> void:
enemies_to_deregister.append(enemy)
func process_enemy_ai(enemy_index: int) -> void:
var enemy := enemies[enemy_index] as Enemy
enemy.navigation_agent.target_position = MainInstances.player.global_position
enemy.target_direction = enemy.global_position.direction_to(enemy.navigation_agent.get_next_path_position())
func _process(delta: float) -> void:
if enemies.is_empty():
return
#works fine
#for i in enemies.size():
#process_enemy_ai(i)
#
#return
# crashes the game
var task_id := WorkerThreadPool.add_group_task(process_enemy_ai, enemies.size())
WorkerThreadPool.wait_for_group_task_completion(task_id)
for enemy: Enemy in enemies_to_deregister:
enemies.erase(enemy)
enemies_to_deregister.clear()