How do I manually control the execution order of "_process()"

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

Let’s say that I have a node structure like this, where A is the parent of B and C:

A
  B
  C

Each has scripts attached, which use _process for logic. Currently, it executes A, then B, then C. However, I need the parent node’s code to execute after the child nodes. How can I force the script attached to A to run after everything else?

:bust_in_silhouette: Reply From: 2D

Don’t use _process in the child nodes and only call it in the parent. In the parent _process function, call another function in the child nodes passing the delta as an argument. Finally, after you have called the children nodes, continue in the parent _process function.

Parent:

_process(delta):
  a.some_function(delta)
  b.some_function(delta)

  #children done, now run parent stuff

That’s really impractical and doesn’t follow the node-based design. Parents shouldn’t have to worry about their children.

Aaron Franke | 2019-05-10 04:02

On the contrary, in Godot parents do have to worry about their children. It’s children that shouldn’t have to know anything about their parent. Best practice in Godot is for any given branch of the scene tree to be able to run independently, not any given node. Indeed, many built in nodes have specific requirements for children.

Ruckus T-Boom | 2019-05-10 15:25

@Aaron Franke AFAIK you can’t change the calling order of the built in functions. What are you needing to accomplish? Perhaps, there is another way?

2D||!2D | 2019-05-10 19:58

I think what I will end up doing is adding another node as a child, and using that child to call the parent’s scripts. That way, it will be executed last.

Aaron Franke | 2019-05-10 20:18

:bust_in_silhouette: Reply From: swift502

The solution seems to be Node’s process_priority.

Nodes whose process priority value is lower will have their processing callbacks executed first.

Set it in the _ready function like so:

func _ready():
    process_priority = 10
1 Like
:bust_in_silhouette: Reply From: yrtv

You do not control it.

  1. In godot things are not done in order. They are done in parallel by servers on threads
    Custom Godot servers — Godot Engine (stable) documentation in English

  2. Godot itself does not control the execution order OS does.
    Scheduler — The Linux Kernel documentation

  3. Some time even OS is not in control.
    https://youtu.be/6NWfznwFnMs?t=2461

For simple use cases, the process_priority lets us control the execution order of every Node, which is what many people find useful, including myself.

I believe that’s what the person asking this question was after.

swift502 | 2021-02-11 18:28

It controls both _process and _physics_process is there any way to separate them?

yrtv | 2021-02-11 21:24

I suppose not. That’d be more complex. I’m thinking of either using multiple nodes or manually calling the functions of a “virtual” node in the order you need.

swift502 | 2021-02-11 21:41

This is completely wrong. Scripts are executed in a specific order and they are not multithreaded. If they were, it would be incredibly easy to shoot yourself in the foot.

Using SceneTree — Godot Engine (stable) documentation in English

sirdorius | 2023-06-17 17:06