Help understanding processing.

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

I have a pretty basic handling of the basics (variables, operators, loops, functions, constants stuff like that.) but I’m still struggling with simple stuff like this. I’m in the middle of my second read of the step by step part of the godot guide and I’m struggling to wrap my brain around processing within the godot engine. I think its a vocabulary issue I’m unfamiliar with coding.

does processing code happen within an object/within a class i thought functions only run within those two things so is processing code how often the computer runs that class/objects code? If it needs to be read over and over or passed over to other code until referenced again?

“the delta parameter contains the time elapsed in seconds as a floating-point number since the previous call to _process().” I straight up just can not wrap my head around this line.

Thank you for any help I’ll try to clarify if needed.

:bust_in_silhouette: Reply From: Tim Martin


Every node in your scene which has a script… which has a _process(var delta) function… will be called by Godot for you automatically - once every frame.

So there can potentially be a lots and lots of these functions firing every frame, on many different nodes, with many different scripts.

Suppose you put a script on a box with a _process(var delta) function, and the script’s job is to move the box right at 1 meter/second. When your game is running at a smooth 60 FSP, Godot will be executing your script 60 times a second so delta = 1/60 = 0.0166. But suppose your game were to start to chug and be down to 30 frames per second, now delta = 1/30 = 0.0333. By multiplying the amount you want to move the box by this delta value, your box’s speed in the world stays the same even if the game cannot manage a steady 60 FPS. Otherwise, at 30 FPS it would only be going at 50% of the speed you wanted it to be going.

Your answer very logical and esp helped me understand the function of the delta tag. I think the godot guide is super good so far just I think sometimes its hard to understand lol. I’m continuing my reading and understanding but thank you :slight_smile:

NeetoBurrito | 2020-05-11 20:32

:bust_in_silhouette: Reply From: kidscancode

At the core of your Godot project is a SceneTree. The SceneTree is the main “loop” of the application. It manages other objects, processes inputs, timing, etc. You can think of it as a big while loop, repeating as long as the application is running.

Nodes are objects that live in the SceneTree. Every time through the loop, aka every frame, the SceneTree calls functions on any nodes inside it. For example, it calls the _process() function on every node that has that function defined.

When it calls _process() it passes a parameter named delta. This value is the amount of time that has elapsed since the last frame. You can use this value to ensure that movement (for example) occurs with the right timing.

So, to give a concrete example, if I add a Sprite node to the tree and put the following code on it:

func _process(delta):
    position.x += 200 * delta

Then every frame (typically about 1/60 of a second), my sprite will move about 3 pixels to the right.

Further reading:

Thank you for your answer. And thanks for keeping it within concepts I understand already (though I suppose that’s to be expected of an educational account lol) and seriously thank you for the further reading. I’ve been trying to read up as much as I can over the past week or so to get a hang of things. Big learning curve and all that.

NeetoBurrito | 2020-05-11 20:29

:bust_in_silhouette: Reply From: jgodfrey

So, there are a number of functions that are called continuously while you’re game is running. As mentioned in the docs you linked, those are _process and _physics_process.

As explained there, the _process function is called once per frame while your application is running. Since frame rates may vary (sometimes wildly) during a game (or between different games), the frequency of the calls to _process may vary somewhat from frame to frame.

The mentioned delta argument that’s passed in to process is just the amount of time since the last time _process was called (in the previous frame). For a game running at 60fps (for example), the delta value will be approximately 1/60 of a second. But, remember, frame rates vary based on lots of things. So, on one frame it could be 1/60 or 1/55, or 1/30, or …

That value is important when you’re trying to make something appear to operate consistently, regardless of varying frame rates. That delta value can be used to help “scale” motion (for example) to keep the look / feel consistent. For example, the distance that a running character needs to move in one frame will be different for a frame that took only 1/60 as compared to one that took, say 1/30 of a second. Again, that value allows things to be “scaled” according to the duration of the frame in an effort to make then appear consistent to a player.

The _physics_process function is similar, except that it fires at a consistent rate - based on your project’s Physics Fps setting (default = 60 fps).

Back to the high-level overview though, when a Godot game is running, all of the _process and _physics_process functions found in any script will be executed once per frame or physics frame as outlined above.

So, these are good places to put things that must constantly be monitored, such as User input.

Thank you for your answer! I was personally struggling to find more examples I could use it, so user input was a super smart way of putting it! A lot of it is still uh like theoretical and I have a lot more reading to do but this helped a lot thanks :slight_smile:

NeetoBurrito | 2020-05-11 20:27