Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/timer.cpp
9903 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
void Timer::_notification(int p_what) {
34
switch (p_what) {
35
case NOTIFICATION_READY: {
36
if (autostart) {
37
#ifdef TOOLS_ENABLED
38
if (is_part_of_edited_scene()) {
39
break;
40
}
41
#endif
42
start();
43
autostart = false;
44
}
45
} break;
46
47
case NOTIFICATION_INTERNAL_PROCESS: {
48
if (!processing || timer_process_callback == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
49
return;
50
}
51
if (ignore_time_scale) {
52
time_left -= Engine::get_singleton()->get_process_step();
53
} else {
54
time_left -= get_process_delta_time();
55
}
56
57
if (time_left < 0) {
58
if (!one_shot) {
59
time_left += wait_time;
60
} else {
61
stop();
62
}
63
64
emit_signal(SNAME("timeout"));
65
}
66
} break;
67
68
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
69
if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
70
return;
71
}
72
if (ignore_time_scale) {
73
time_left -= Engine::get_singleton()->get_process_step();
74
} else {
75
time_left -= get_physics_process_delta_time();
76
}
77
78
if (time_left < 0) {
79
if (!one_shot) {
80
time_left += wait_time;
81
} else {
82
stop();
83
}
84
emit_signal(SNAME("timeout"));
85
}
86
} break;
87
}
88
}
89
90
void Timer::set_wait_time(double p_time) {
91
ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
92
wait_time = p_time;
93
update_configuration_warnings();
94
}
95
96
double Timer::get_wait_time() const {
97
return wait_time;
98
}
99
100
void Timer::set_one_shot(bool p_one_shot) {
101
one_shot = p_one_shot;
102
}
103
104
bool Timer::is_one_shot() const {
105
return one_shot;
106
}
107
108
void Timer::set_autostart(bool p_start) {
109
autostart = p_start;
110
}
111
112
bool Timer::has_autostart() const {
113
return autostart;
114
}
115
116
void Timer::start(double p_time) {
117
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.");
118
119
if (p_time > 0) {
120
set_wait_time(p_time);
121
}
122
time_left = wait_time;
123
_set_process(true);
124
}
125
126
void Timer::stop() {
127
time_left = -1;
128
_set_process(false);
129
autostart = false;
130
}
131
132
void Timer::set_paused(bool p_paused) {
133
if (paused == p_paused) {
134
return;
135
}
136
137
paused = p_paused;
138
_set_process(processing);
139
}
140
141
bool Timer::is_paused() const {
142
return paused;
143
}
144
145
void Timer::set_ignore_time_scale(bool p_ignore) {
146
ignore_time_scale = p_ignore;
147
}
148
149
bool Timer::is_ignoring_time_scale() {
150
return ignore_time_scale;
151
}
152
153
bool Timer::is_stopped() const {
154
return get_time_left() <= 0;
155
}
156
157
double Timer::get_time_left() const {
158
return time_left > 0 ? time_left : 0;
159
}
160
161
void Timer::set_timer_process_callback(TimerProcessCallback p_callback) {
162
if (timer_process_callback == p_callback) {
163
return;
164
}
165
166
switch (timer_process_callback) {
167
case TIMER_PROCESS_PHYSICS:
168
if (is_physics_processing_internal()) {
169
set_physics_process_internal(false);
170
set_process_internal(true);
171
}
172
break;
173
case TIMER_PROCESS_IDLE:
174
if (is_processing_internal()) {
175
set_process_internal(false);
176
set_physics_process_internal(true);
177
}
178
break;
179
}
180
timer_process_callback = p_callback;
181
}
182
183
Timer::TimerProcessCallback Timer::get_timer_process_callback() const {
184
return timer_process_callback;
185
}
186
187
void Timer::_set_process(bool p_process, bool p_force) {
188
switch (timer_process_callback) {
189
case TIMER_PROCESS_PHYSICS:
190
set_physics_process_internal(p_process && !paused);
191
break;
192
case TIMER_PROCESS_IDLE:
193
set_process_internal(p_process && !paused);
194
break;
195
}
196
processing = p_process;
197
}
198
199
PackedStringArray Timer::get_configuration_warnings() const {
200
PackedStringArray warnings = Node::get_configuration_warnings();
201
202
if (wait_time < 0.05 - CMP_EPSILON) {
203
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."));
204
}
205
206
return warnings;
207
}
208
209
void Timer::_bind_methods() {
210
ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
211
ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
212
213
ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
214
ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
215
216
ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
217
ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
218
219
ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
220
ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
221
222
ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
223
ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
224
225
ClassDB::bind_method(D_METHOD("set_ignore_time_scale", "ignore"), &Timer::set_ignore_time_scale);
226
ClassDB::bind_method(D_METHOD("is_ignoring_time_scale"), &Timer::is_ignoring_time_scale);
227
228
ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
229
230
ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
231
232
ClassDB::bind_method(D_METHOD("set_timer_process_callback", "callback"), &Timer::set_timer_process_callback);
233
ClassDB::bind_method(D_METHOD("get_timer_process_callback"), &Timer::get_timer_process_callback);
234
235
ADD_SIGNAL(MethodInfo("timeout"));
236
237
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_callback", "get_timer_process_callback");
238
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");
239
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
240
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
241
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
242
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_time_scale"), "set_ignore_time_scale", "is_ignoring_time_scale");
243
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
244
245
BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
246
BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
247
}
248
249