+1 vote

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?

in Engine by (111 points)

3 Answers

+3 votes
Best answer

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
by (50 points)
edited by
+1 vote

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
by (391 points)

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

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.

@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?

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.

0 votes

You do not control it.

  1. In godot things are not done in order. They are done in parallel by servers on threads
    https://docs.godotengine.org/en/stable/development/cpp/custom_godot_servers.html

  2. Godot itself does not control the execution order OS does.
    https://www.kernel.org/doc/html/latest/scheduler/index.html

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

by (889 points)

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.

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.