How to iterate a big array without blocking the main process?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dimdev4

I need to iterate 100k+ size of arrays without blocking/freezing the app, or sometimes some pathfinding algos that could take some time but I want to do all of them in the background. Is that possible? Using threads seem doesn’t work on me (still freezing) :confused: or did I do something wrong?

You could use a timer and do a small amount on each event?

SteveSmith | 2023-01-23 09:22

:bust_in_silhouette: Reply From: LeslieS

You could use the one of the _process functions:

var test:Array = [1,2, 3, 4, 5, 6, 7, 8, 9, 10]
var index:int = 0

func _physics_process(delta: float) -> void:
	if index < test.size():
		test[index] *= 2
		index += 1
	else: 
		set_physics_process(false)
		print(test)

Any idea to make it in a “function” format? So let’s say I have these 2 functions that needs to be run in the background.

func a() -> int: pass # the time consuming function
func b() -> int: pass # the time consuming function

And I want to be able to call it as needed, without blocking other activities.

dimdev4 | 2023-01-25 17:01

I would have to know what the functions do to help with that.
If the functions can be broken down into iterations then it is possible to use the method I gave.
If the functions must run and complete without interruption then you are definitely going to have to use threading. And I have no knowledge of threading in Godot.

LeslieS | 2023-01-26 01:49