Ver código fonte

Allow Timer nodes to ignore engine time scale

Hakim 11 meses atrás
pai
commit
483c1348d0
3 arquivos alterados com 29 adições e 2 exclusões
  1. 3 0
      doc/classes/Timer.xml
  2. 22 2
      scene/main/timer.cpp
  3. 4 0
      scene/main/timer.h

+ 3 - 0
doc/classes/Timer.xml

@@ -45,6 +45,9 @@
 			[b]Note:[/b] After the timer enters the tree, this property is automatically set to [code]false[/code].
 			[b]Note:[/b] After the timer enters the tree, this property is automatically set to [code]false[/code].
 			[b]Note:[/b] This property does nothing when the timer is running in the editor.
 			[b]Note:[/b] This property does nothing when the timer is running in the editor.
 		</member>
 		</member>
+		<member name="ignore_time_scale" type="bool" setter="set_ignore_time_scale" getter="get_ignore_time_scale" default="false">
+			If [code]true[/code], the timer will ignore [member Engine.time_scale] and update with the real, elapsed time.
+		</member>
 		<member name="one_shot" type="bool" setter="set_one_shot" getter="is_one_shot" default="false">
 		<member name="one_shot" type="bool" setter="set_one_shot" getter="is_one_shot" default="false">
 			If [code]true[/code], the timer will stop after reaching the end. Otherwise, as by default, the timer will automatically restart.
 			If [code]true[/code], the timer will stop after reaching the end. Otherwise, as by default, the timer will automatically restart.
 		</member>
 		</member>

+ 22 - 2
scene/main/timer.cpp

@@ -48,7 +48,11 @@ void Timer::_notification(int p_what) {
 			if (!processing || timer_process_callback == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
 			if (!processing || timer_process_callback == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
 				return;
 				return;
 			}
 			}
-			time_left -= get_process_delta_time();
+			if (ignore_time_scale) {
+				time_left -= Engine::get_singleton()->get_process_step();
+			} else {
+				time_left -= get_process_delta_time();
+			}
 
 
 			if (time_left < 0) {
 			if (time_left < 0) {
 				if (!one_shot) {
 				if (!one_shot) {
@@ -65,7 +69,11 @@ void Timer::_notification(int p_what) {
 			if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
 			if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
 				return;
 				return;
 			}
 			}
-			time_left -= get_physics_process_delta_time();
+			if (ignore_time_scale) {
+				time_left -= Engine::get_singleton()->get_process_step();
+			} else {
+				time_left -= get_physics_process_delta_time();
+			}
 
 
 			if (time_left < 0) {
 			if (time_left < 0) {
 				if (!one_shot) {
 				if (!one_shot) {
@@ -134,6 +142,14 @@ bool Timer::is_paused() const {
 	return paused;
 	return paused;
 }
 }
 
 
+void Timer::set_ignore_time_scale(bool p_ignore) {
+	ignore_time_scale = p_ignore;
+}
+
+bool Timer::get_ignore_time_scale() {
+	return ignore_time_scale;
+}
+
 bool Timer::is_stopped() const {
 bool Timer::is_stopped() const {
 	return get_time_left() <= 0;
 	return get_time_left() <= 0;
 }
 }
@@ -206,6 +222,9 @@ void Timer::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
 	ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
 	ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
 	ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
 
 
+	ClassDB::bind_method(D_METHOD("set_ignore_time_scale", "ignore"), &Timer::set_ignore_time_scale);
+	ClassDB::bind_method(D_METHOD("get_ignore_time_scale"), &Timer::get_ignore_time_scale);
+
 	ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
 	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("get_time_left"), &Timer::get_time_left);
@@ -220,6 +239,7 @@ void Timer::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
 	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, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
+	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_time_scale"), "set_ignore_time_scale", "get_ignore_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, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
 
 
 	BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
 	BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);

+ 4 - 0
scene/main/timer.h

@@ -41,6 +41,7 @@ class Timer : public Node {
 	bool autostart = false;
 	bool autostart = false;
 	bool processing = false;
 	bool processing = false;
 	bool paused = false;
 	bool paused = false;
+	bool ignore_time_scale = false;
 
 
 	double time_left = -1.0;
 	double time_left = -1.0;
 
 
@@ -69,6 +70,9 @@ public:
 	void set_paused(bool p_paused);
 	void set_paused(bool p_paused);
 	bool is_paused() const;
 	bool is_paused() const;
 
 
+	void set_ignore_time_scale(bool p_ignore);
+	bool get_ignore_time_scale();
+
 	bool is_stopped() const;
 	bool is_stopped() const;
 
 
 	double get_time_left() const;
 	double get_time_left() const;