Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Engine.get_{process,physics}_step() to get unscaled time delta #86766

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/config/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Engine {
uint32_t _frame_delay = 0;
uint64_t _frame_ticks = 0;
double _process_step = 0;
double _physics_step = 0;

int ips = 60;
double physics_jitter_fix = 0.5;
Expand Down Expand Up @@ -118,6 +119,7 @@ class Engine {
bool is_in_physics_frame() const { return _in_physics; }
uint64_t get_frame_ticks() const { return _frame_ticks; }
double get_process_step() const { return _process_step; }
double get_physics_step() const { return _physics_step; }
double get_physics_interpolation_fraction() const { return _physics_interpolation_fraction; }

void set_time_scale(double p_scale);
Expand Down
10 changes: 10 additions & 0 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,14 @@ double Engine::get_physics_jitter_fix() const {
return ::Engine::get_singleton()->get_physics_jitter_fix();
}

double Engine::get_process_step() const {
return ::Engine::get_singleton()->get_process_step();
}

double Engine::get_physics_step() const {
return ::Engine::get_singleton()->get_physics_step();
}

double Engine::get_physics_interpolation_fraction() const {
return ::Engine::get_singleton()->get_physics_interpolation_fraction();
}
Expand Down Expand Up @@ -1740,6 +1748,8 @@ void Engine::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_max_physics_steps_per_frame"), &Engine::get_max_physics_steps_per_frame);
ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
ClassDB::bind_method(D_METHOD("get_process_step"), &Engine::get_process_step);
ClassDB::bind_method(D_METHOD("get_physics_step"), &Engine::get_physics_step);
ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
ClassDB::bind_method(D_METHOD("set_max_fps", "max_fps"), &Engine::set_max_fps);
ClassDB::bind_method(D_METHOD("get_max_fps"), &Engine::get_max_fps);
Expand Down
2 changes: 2 additions & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ class Engine : public Object {

void set_physics_jitter_fix(double p_threshold);
double get_physics_jitter_fix() const;
double get_process_step() const;
double get_physics_step() const;
double get_physics_interpolation_fraction() const;

void set_max_fps(int p_fps);
Expand Down
15 changes: 14 additions & 1 deletion doc/classes/Engine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
Returns the fraction through the current physics tick we are at the time of rendering the frame. This can be used to implement fixed timestep interpolation.
</description>
</method>
<method name="get_physics_step" qualifiers="const">
<return type="float" />
<description>
Returns the time spent between the last two physics ticks in seconds. Unlike the [code]delta[/code] parameter in [method Node._physics_process], this value is not affected by [member time_scale]. See also [method get_process_step].
</description>
</method>
<method name="get_process_frames" qualifiers="const">
<return type="int" />
<description>
Expand All @@ -142,6 +148,12 @@
[/codeblocks]
</description>
</method>
<method name="get_process_step" qualifiers="const">
<return type="float" />
<description>
Returns the time spent between the last two rendered frames in seconds. Unlike the [code]delta[/code] parameter in [method Node._process], this value is not affected by [member time_scale]. See also [method get_physics_step].
</description>
</method>
<method name="get_script_language" qualifiers="const">
<return type="ScriptLanguage" />
<param index="0" name="index" type="int" />
Expand Down Expand Up @@ -261,7 +273,7 @@
func _enter_tree():
# Depending on when the node is added to the tree,
# prints either "true" or "false".
print(Engine.is_in_physics_frame())
print(Engine.is_in_physics_frame())

func _process(delta):
print(Engine.is_in_physics_frame()) # Prints false
Expand Down Expand Up @@ -321,6 +333,7 @@
<member name="max_physics_steps_per_frame" type="int" setter="set_max_physics_steps_per_frame" getter="get_max_physics_steps_per_frame" default="8">
The maximum number of physics steps that can be simulated each rendered frame.
[b]Note:[/b] The default value is tuned to prevent expensive physics simulations from triggering even more expensive simulations indefinitely. However, the game will appear to slow down if the rendering FPS is less than [code]1 / max_physics_steps_per_frame[/code] of [member physics_ticks_per_second]. This occurs even if [code]delta[/code] is consistently used in physics calculations. To avoid this, increase [member max_physics_steps_per_frame] if you have increased [member physics_ticks_per_second] significantly above its default value.
[b]Note:[/b] [member max_physics_steps_per_frame] is ignored if the [code]--fixed-fps &lt;fps&gt;[/code] [url=$DOCS_URL/tutorials/editor/command_line_tutorial.html]command line argument[/url] is used with a positive value, which also occurs when using Movie Maker mode. Simulation will always run at full speed in this scenario.
</member>
<member name="physics_jitter_fix" type="float" setter="set_physics_jitter_fix" getter="get_physics_jitter_fix" default="0.5">
How much physics ticks are synchronized with real time. If [code]0[/code] or less, the ticks are fully synchronized. Higher values cause the in-game clock to deviate more from the real clock, but they smooth out framerate jitters.
Expand Down
6 changes: 3 additions & 3 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<return type="void" />
<param index="0" name="delta" type="float" />
<description>
Called during the physics processing step of the main loop. Physics processing means that the frame rate is synced to the physics, i.e. the [param delta] variable should be constant. [param delta] is in seconds.
Called during the physics processing step of the main loop. Physics processing means that the frame rate is synced to the physics, i.e. the [param delta] variable should be constant. [param delta] is in seconds and is multiplied by [member Engine.time_scale]. To get the [param delta] that isn't scaled by [member Engine.time_scale], use [method Engine.get_physics_step].
It is only called if physics processing is enabled, which is done automatically if this method is overridden, and can be toggled with [method set_physics_process].
Corresponds to the [constant NOTIFICATION_PHYSICS_PROCESS] notification in [method Object._notification].
[b]Note:[/b] This method is only called if the node is present in the scene tree (i.e. if it's not an orphan).
Expand All @@ -81,7 +81,7 @@
<return type="void" />
<param index="0" name="delta" type="float" />
<description>
Called during the processing step of the main loop. Processing happens at every frame and as fast as possible, so the [param delta] time since the previous frame is not constant. [param delta] is in seconds.
Called during the processing step of the main loop. Processing happens at every frame and as fast as possible, so the [param delta] time since the previous frame is not constant. [param delta] is in seconds and is multiplied by [member Engine.time_scale]. To get the [param delta] that isn't scaled by [member Engine.time_scale], use [method Engine.get_process_step].
It is only called if processing is enabled, which is done automatically if this method is overridden, and can be toggled with [method set_process].
Corresponds to the [constant NOTIFICATION_PROCESS] notification in [method Object._notification].
[b]Note:[/b] This method is only called if the node is present in the scene tree (i.e. if it's not an orphan).
Expand Down Expand Up @@ -961,7 +961,7 @@
[b]Note:[/b] When changing the name, the following characters will be replaced with an underscore: ([code].[/code] [code]:[/code] [code]@[/code] [code]/[/code] [code]"[/code] [code]%[/code]). In particular, the [code]@[/code] character is reserved for auto-generated names. See also [method String.validate_node_name].
</member>
<member name="owner" type="Node" setter="set_owner" getter="get_owner">
The owner of this node. The owner must be an ancestor of this node. When packing the owner node in a [PackedScene], all the nodes it owns are also saved with it.
The owner of this node. The owner must be an ancestor of this node. When packing the owner node in a [PackedScene], all the nodes it owns are also saved with it.
[b]Note:[/b] In the editor, nodes not owned by the scene root are usually not displayed in the Scene dock, and will [b]not[/b] be saved. To prevent this, remember to set the owner after calling [method add_child]. See also (see [member unique_name_in_owner])
</member>
<member name="process_mode" type="int" setter="set_process_mode" getter="get_process_mode" enum="Node.ProcessMode" default="0">
Expand Down
1 change: 1 addition & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,7 @@
</member>
<member name="physics/common/max_physics_steps_per_frame" type="int" setter="" getter="" default="8">
Controls the maximum number of physics steps that can be simulated each rendered frame. The default value is tuned to avoid "spiral of death" situations where expensive physics simulations trigger more expensive simulations indefinitely. However, the game will appear to slow down if the rendering FPS is less than [code]1 / max_physics_steps_per_frame[/code] of [member physics/common/physics_ticks_per_second]. This occurs even if [code]delta[/code] is consistently used in physics calculations. To avoid this, increase [member physics/common/max_physics_steps_per_frame] if you have increased [member physics/common/physics_ticks_per_second] significantly above its default value.
[b]Note:[/b] [member physics/common/max_physics_steps_per_frame] is ignored if the [code]--fixed-fps &lt;fps&gt;[/code] [url=$DOCS_URL/tutorials/editor/command_line_tutorial.html]command line argument[/url] is used with a positive value, which also occurs when using Movie Maker mode. Simulation will always run at full speed in this scenario.
[b]Note:[/b] This property is only read when the project starts. To change the maximum number of simulated physics steps per frame at runtime, set [member Engine.max_physics_steps_per_frame] instead.
</member>
<member name="physics/common/physics_jitter_fix" type="float" setter="" getter="" default="0.5">
Expand Down
1 change: 1 addition & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3906,6 +3906,7 @@ bool Main::iteration() {
double scaled_step = process_step * time_scale;

Engine::get_singleton()->_process_step = process_step;
Engine::get_singleton()->_physics_step = physics_step;
Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;

uint64_t physics_process_ticks = 0;
Expand Down
Loading