Skip to content

Implement set_time_left in Timer node #107920

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

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
6 changes: 3 additions & 3 deletions doc/classes/Timer.xml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more accurate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the property while stopped does nothing and prints an error.

Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
<member name="process_callback" type="int" setter="set_timer_process_callback" getter="get_timer_process_callback" enum="Timer.TimerProcessCallback" default="1">
Specifies when the timer is updated during the main loop.
</member>
<member name="time_left" type="float" setter="" getter="get_time_left">
The timer's remaining time in seconds. This is always [code]0[/code] if the timer is stopped.
[b]Note:[/b] This property is read-only and cannot be modified. It is based on [member wait_time].
<member name="time_left" type="float" setter="set_time_left" getter="get_time_left">
The timer's remaining time in seconds.
[b]Note:[/b] If the timer is stopped, the getter always returns [code]0.0[/code]. Setting this property while stopped updates the value, but does not start the timer.
</member>
<member name="wait_time" type="float" setter="set_wait_time" getter="get_wait_time" default="1.0">
The time required for the timer to end, in seconds. This property can also be set every time [method start] is called.
Expand Down
7 changes: 7 additions & 0 deletions misc/extension_api_validation/4.4-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,10 @@ Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/add_image/a
Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/update_image/arguments': size changed value in new API, from 11 to 12.

Optional argument added. Compatibility methods registered.


GH-107920
---------
Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/Timer/properties/time_left': setter

Setter function added.
8 changes: 7 additions & 1 deletion scene/main/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ double Timer::get_time_left() const {
return time_left > 0 ? time_left : 0;
}

void Timer::set_time_left(double p_time) {
ERR_FAIL_COND_MSG(!processing, "Setting time_left when the timer is stopped. Use start(time_left) to start automatically.");
time_left = p_time;
}

void Timer::set_timer_process_callback(TimerProcessCallback p_callback) {
if (timer_process_callback == p_callback) {
return;
Expand Down Expand Up @@ -228,6 +233,7 @@ void Timer::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);

ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
ClassDB::bind_method(D_METHOD("set_time_left", "time_left"), &Timer::set_time_left);

ClassDB::bind_method(D_METHOD("set_timer_process_callback", "callback"), &Timer::set_timer_process_callback);
ClassDB::bind_method(D_METHOD("get_timer_process_callback"), &Timer::get_timer_process_callback);
Expand All @@ -240,7 +246,7 @@ void Timer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_time_scale"), "set_ignore_time_scale", "is_ignoring_time_scale");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_time_left", "get_time_left");

BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
Expand Down
1 change: 1 addition & 0 deletions scene/main/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Timer : public Node {
bool is_stopped() const;

double get_time_left() const;
void set_time_left(double p_time);

PackedStringArray get_configuration_warnings() const override;

Expand Down