Run Code Before First Frame Renders

Godot Version

4.2.1

Question

Hey Everybody!

I’ve been using Godot a lot recently, but I’ve run into a slight problem: I don’t know how to run code before the first frame of the scene renders.

So, for example, say in my scene I want my character to move to a different position at the start of the scene. If I change his position in the _ready() function, it happens after the scene has already rendered him in his initial position, and so you can clearly see him change position.

Is there any way to move him before the scene renders, so that it looks like he started in that new position? Thanks!

p.s. I really hope that this made sense. Thanks again!

There is one method named “_init()” that you may use to achieve what you want.

This is what the docs say:

● void _init() virtual

Called when the object’s script is instantiated, oftentimes after the object is initialized in memory (through Object.new() in GDScript, or new GodotObject in C#). It can be also defined to take in parameters. This method is similar to a constructor in most programming languages.

Note: If _init() is defined with required parameters, the Object with script may only be created directly. If any other means (such as PackedScene.instantiate() or Node.duplicate()) are used, the script’s initialization will fail.

I believe you could set the player’s position there, not sure though

2 Likes

Another alternative to @wender 's answer is using constructors.

1 Like

Hey! Thank you for your reply. It was very helpful.

Do you know a way to access a different node through code before the scene renders? It doesn’t seem to work in the init() function.

1 Like

There is no way to access the scene tree before _ready.

but from the example you given, there is many work around, i think the simpliest one is make if visible false, then visible true after you moved it.

2 Likes