Parcourir la source

Merge pull request #53591 from Calinou/timer-low-wait-time-add-warning-3.x

Rémi Verschelde il y a 3 ans
Parent
commit
512211b61c
3 fichiers modifiés avec 18 ajouts et 1 suppressions
  1. 2 1
      doc/classes/Timer.xml
  2. 14 0
      scene/main/timer.cpp
  3. 2 0
      scene/main/timer.h

+ 2 - 1
doc/classes/Timer.xml

@@ -51,7 +51,8 @@
 			[b]Note:[/b] You cannot set this value. To change the timer's remaining time, use [method start].
 			[b]Note:[/b] You cannot set this value. To change the timer's remaining time, use [method start].
 		</member>
 		</member>
 		<member name="wait_time" type="float" setter="set_wait_time" getter="get_wait_time" default="1.0">
 		<member name="wait_time" type="float" setter="set_wait_time" getter="get_wait_time" default="1.0">
-			Wait time in seconds.
+			The wait time in seconds.
+			[b]Note:[/b] Timers can only emit once per rendered frame at most (or once per physics frame if [member process_mode] is [constant TIMER_PROCESS_PHYSICS]). This means very low wait times (lower than 0.05 seconds) will behave in significantly different ways depending on the rendered framerate. For very low wait times, it is recommended to use a process loop in a script instead of using a Timer node.
 		</member>
 		</member>
 	</members>
 	</members>
 	<signals>
 	<signals>

+ 14 - 0
scene/main/timer.cpp

@@ -84,6 +84,7 @@ void Timer::_notification(int p_what) {
 void Timer::set_wait_time(float p_time) {
 void Timer::set_wait_time(float p_time) {
 	ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
 	ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
 	wait_time = p_time;
 	wait_time = p_time;
+	update_configuration_warning();
 }
 }
 float Timer::get_wait_time() const {
 float Timer::get_wait_time() const {
 	return wait_time;
 	return wait_time;
@@ -178,6 +179,19 @@ void Timer::_set_process(bool p_process, bool p_force) {
 	processing = p_process;
 	processing = p_process;
 }
 }
 
 
+String Timer::get_configuration_warning() const {
+	String warning = Node::get_configuration_warning();
+
+	if (wait_time < 0.05 - CMP_EPSILON) {
+		if (warning != String()) {
+			warning += "\n\n";
+		}
+		warning += TTR("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.");
+	}
+
+	return warning;
+}
+
 void Timer::_bind_methods() {
 void Timer::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
 	ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
 	ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
 	ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);

+ 2 - 0
scene/main/timer.h

@@ -73,6 +73,8 @@ public:
 
 
 	float get_time_left() const;
 	float get_time_left() const;
 
 
+	String get_configuration_warning() const;
+
 	void set_timer_process_mode(TimerProcessMode p_mode);
 	void set_timer_process_mode(TimerProcessMode p_mode);
 	TimerProcessMode get_timer_process_mode() const;
 	TimerProcessMode get_timer_process_mode() const;
 	Timer();
 	Timer();