Hi!
I have some character nodes (area2d), which use the getsimplepath () function on a 200x200 tilemap.
I had problems, each node took time to calculate the path, and the main thread was blocking a few milliseconds, so I have tried using threads.
Now, if I put only one character, it works correctly, with 2 or 3, it takes a while to throw the error. And if for example I put 5 or more, throw error instantaneous. And the game breaks and closes.
The error message I get is:
E 0: 00: 08.518 get: FATAL: Index p_index = -1 is out of bounds
(size () = 4).
C ++ source ./core/cowdata.h:152 @ get ()
Character node code:
var speed : = 400.0
var path : = []
var thread = Thread.new()
func _ready() -> void:
randomize()
newPath()
func _unhandled_input(event: InputEvent) -> void:
if not event is InputEventMouseButton:
return
if event.button_index != BUTTON_LEFT or not event.pressed:
return
func _process(delta: float) -> void:
if path.size() != 0:
var move_distance : = speed * delta
move_along_path(move_distance)
else:
newPath()
func move_along_path(distance : float) -> void:
var last_point : = global_position
for index in range(path.size()):
var distance_to_next = last_point.distance_to(path[0])
if distance <= distance_to_next and distance >= 0.0:
global_position = last_point.linear_interpolate(path[0], distance / distance_to_next)
break
elif distance < 0.0:
global_position = path[0]
newPath()
break
distance -= distance_to_next
last_point = path[0]
path.remove(0)
func newPath():
if (thread.is_active()):
# Already working
return
print("START THREAD!")
thread.start(self, "_on_newPath", get_instance_id())
func _on_newPath(id):
print("THREAD FUNC!")
print(id)
var newPosition = Vector2(randi()%200, randi()%200)
var tile_center_pos = get_parent().get_parent().get_child(0).get_child(0).map_to_world(newPosition) #map in tree
var newPath = get_parent().get_parent().get_child(0).get_simple_path(global_position,tile_center_pos) #navigation2D node in tree
#get_parent().get_child(2).points = newPath
#newPath.remove(0)
call_deferred("_on_newPath_done")
return newPath
func _on_newPath_done():
# Wait for the thread to complete, get the returned value
var minionPath = thread.wait_to_finish()
path = minionPath
The code without thread functions worked fine, with any number of nodes. I thing that the problem is with threads, but I cant understand the error message.