Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/timer.cpp
20841 views
1
/**************************************************************************/
2
/* timer.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "timer.h"
32
33
#include "core/config/engine.h"
34
35
void Timer::_notification(int p_what) {
36
switch (p_what) {
37
case NOTIFICATION_READY: {
38
if (autostart) {
39
#ifdef TOOLS_ENABLED
40
if (is_part_of_edited_scene()) {
41
break;
42
}
43
#endif
44
start();
45
autostart = false;
46
}
47
} break;
48
49
case NOTIFICATION_INTERNAL_PROCESS: {
50
if (!processing || timer_process_callback == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
51
return;
52
}
53
if (ignore_time_scale) {
54
time_left -= Engine::get_singleton()->get_process_step();
55
} else {
56
time_left -= get_process_delta_time();
57
}
58
59
if (time_left < 0) {
60
if (!one_shot) {
61
time_left += wait_time;
62
} else {
63
stop();
64
}
65
66
emit_signal(SNAME("timeout"));
67
}
68
} break;
69
70
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
71
if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
72
return;
73
}
74
if (ignore_time_scale) {
75
time_left -= Engine::get_singleton()->get_process_step();
76
} else {
77
time_left -= get_physics_process_delta_time();
78
}
79
80
if (time_left < 0) {
81
if (!one_shot) {
82
time_left += wait_time;
83
} else {
84
stop();
85
}
86
emit_signal(SNAME("timeout"));
87
}
88
} break;
89
}
90
}
91
92
void Timer::set_wait_time(double p_time) {
93
ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
94
wait_time = p_time;
95
update_configuration_warnings();
96
}
97
98
double Timer::get_wait_time() const {
99
return wait_time;
100
}
101
102
void Timer::set_one_shot(bool p_one_shot) {
103
one_shot = p_one_shot;
104
}
105
106
bool Timer::is_one_shot() const {
107
return one_shot;
108
}
109
110
void Timer::set_autostart(bool p_start) {
111
autostart = p_start;
112
}
113
114
bool Timer::has_autostart() const {
115
return autostart;
116
}
117
118
void Timer::start(double p_time) {
119
ERR_FAIL_COND_MSG(!is_inside_tree(), "Unable to start the timer because it's not inside the scene tree. Either add it or set autostart to true.");
120
121
if (p_time > 0) {
122
set_wait_time(p_time);
123
}
124
time_left = wait_time;
125
_set_process(true);
126
}
127
128
void Timer::stop() {
129
time_left = -1;
130
_set_process(false);
131
autostart = false;
132
}
133
134
void Timer::set_paused(bool p_paused) {
135
if (paused == p_paused) {
136
return;
137
}
138
139
paused = p_paused;
140
_set_process(processing);
141
}
142
143
bool Timer::is_paused() const {
144
return paused;
145
}
146
147
void Timer::set_ignore_time_scale(bool p_ignore) {
148
ignore_time_scale = p_ignore;
149
}
150
151
bool Timer::is_ignoring_time_scale() {
152
return ignore_time_scale;
153
}
154
155
bool Timer::is_stopped() const {
156
return get_time_left() <= 0;
157
}
158
159
double Timer::get_time_left() const {
160
return time_left > 0 ? time_left : 0;
161
}
162
163
void Timer::set_timer_process_callback(TimerProcessCallback p_callback) {
164
if (timer_process_callback == p_callback) {
165
return;
166
}
167
168
switch (timer_process_callback) {
169
case TIMER_PROCESS_PHYSICS:
170
if (is_physics_processing_internal()) {
171
set_physics_process_internal(false);
172
set_process_internal(true);
173
}
174
break;
175
case TIMER_PROCESS_IDLE:
176
if (is_processing_internal()) {
177
set_process_internal(false);
178
set_physics_process_internal(true);
179
}
180
break;
181
}
182
timer_process_callback = p_callback;
183
}
184
185
Timer::TimerProcessCallback Timer::get_timer_process_callback() const {
186
return timer_process_callback;
187
}
188
189
void Timer::_set_process(bool p_process, bool p_force) {
190
switch (timer_process_callback) {
191
case TIMER_PROCESS_PHYSICS:
192
set_physics_process_internal(p_process && !paused);
193
break;
194
case TIMER_PROCESS_IDLE:
195
set_process_internal(p_process && !paused);
196
break;
197
}
198
processing = p_process;
199
}
200
201
PackedStringArray Timer::get_configuration_warnings() const {
202
PackedStringArray warnings = Node::get_configuration_warnings();
203
204
if (wait_time < 0.05 - CMP_EPSILON) {
205
warnings.push_back(RTR("Very low timer wait times (< 0.05 seconds) may behave in significantly different ways depending on the rendered or physics frame rate.\nConsider using a script's process loop instead of relying on a Timer for very low wait times."));
206
}
207
208
return warnings;
209
}
210
211
void Timer::_bind_methods() {
212
ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
213
ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
214
215
ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
216
ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
217
218
ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
219
ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
220
221
ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
222
ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
223
224
ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
225
ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
226
227
ClassDB::bind_method(D_METHOD("set_ignore_time_scale", "ignore"), &Timer::set_ignore_time_scale);
228
ClassDB::bind_method(D_METHOD("is_ignoring_time_scale"), &Timer::is_ignoring_time_scale);
229
230
ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
231
232
ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
233
234
ClassDB::bind_method(D_METHOD("set_timer_process_callback", "callback"), &Timer::set_timer_process_callback);
235
ClassDB::bind_method(D_METHOD("get_timer_process_callback"), &Timer::get_timer_process_callback);
236
237
ADD_SIGNAL(MethodInfo("timeout"));
238
239
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_callback", "get_timer_process_callback");
240
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wait_time", PROPERTY_HINT_RANGE, "0.001,4096,0.001,or_greater,exp,suffix:s"), "set_wait_time", "get_wait_time");
241
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
242
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
243
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
244
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_time_scale"), "set_ignore_time_scale", "is_ignoring_time_scale");
245
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
246
247
BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
248
BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
249
}
250
251