Skip to content

Instantly share code, notes, and snippets.

@AnidemDex
Created August 28, 2022 03:03
Show Gist options
  • Save AnidemDex/be28d032e06622822b70c8806cb19cdc to your computer and use it in GitHub Desktop.
Save AnidemDex/be28d032e06622822b70c8806cb19cdc to your computer and use it in GitHub Desktop.
A little snippet to do task in big loops between frames to avoid hanging the engine
## Emmited when the process start.
## Is a good idea to update your nodes according to
## the generated total_steps
signal started()
## Emmited when the process ends.
signal ended()
## Emmited everytime the processor reach the chunk limit and will start
## another chunk of load.
signal step()
var chunk_limit = 10000
var total_steps = 1
var current_step = 0
## Do a task until reach x_limit and y_limit
## The task is performed every y for every x in every y_limit and x_limit range
func do_recursively(in_object:Object, do_method:String, x_limit:int, y_limit:int=1) -> void:
assert(in_object.has_method(do_method))
total_steps = x_limit * y_limit
emit_signal("started")
for x in x_limit:
current_step += 1
for y in y_limit:
current_step += 1
in_object.call(do_method, x, y)
if current_step % chunk_limit == 0:
emit_signal("step")
yield(get_tree(), "idle_frame")
emit_signal("step")
emit_signal("ended")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment