11extends Node
22
3+ class AwaitLogger :
4+ var _time_waited = 0.0
5+ var logger = GutUtils .get_logger ()
6+ var waiting_on = "nothing"
7+ var logged_initial_message = false
8+ var wait_log_delay := 1.0
9+ var disabled = false
10+
11+ func waited (x ):
12+ _time_waited += x
13+ if (! logged_initial_message and _time_waited >= wait_log_delay ):
14+ log_it ()
15+ logged_initial_message = true
16+
17+
18+ func reset ():
19+ _time_waited = 0.0
20+ logged_initial_message = false
21+
22+
23+ func log_it ():
24+ if (! disabled ):
25+ var msg = str ("--- Awaiting " , waiting_on , " ---" )
26+ logger .wait_msg (msg )
27+
28+
29+
30+
331signal timeout
432signal wait_started
533
34+ var await_logger = AwaitLogger .new ()
635var _wait_time := 0.0
7- var _wait_frames := 0
36+ var _wait_process_frames := 0
37+ var _wait_physics_frames := 0
838var _signal_to_wait_on = null
939
10- var _predicate_function_waiting_to_be_true = null
40+ var _predicate_method = null
41+ var _waiting_for_predicate_to_be = null
42+
1143var _predicate_time_between := 0.0
1244var _predicate_time_between_elpased := 0.0
1345
46+ var _elapsed_time := 0.0
47+ var _elapsed_frames := 0
48+
1449var _did_last_wait_timeout = false
1550var did_last_wait_timeout = false :
1651 get : return _did_last_wait_timeout
1752 set (val ): push_error ("Cannot set did_last_wait_timeout" )
1853
19- var _elapsed_time := 0.0
20- var _elapsed_frames := 0
54+
55+
56+ func _ready () -> void :
57+ get_tree ().process_frame .connect (_on_tree_process_frame )
58+ get_tree ().physics_frame .connect (_on_tree_physics_frame )
59+
60+
61+ func _on_tree_process_frame ():
62+ # Count frames here instead of in _process so that tree order never
63+ # makes a difference and the count/signaling happens outside of
64+ # _process being called.
65+ if (_wait_process_frames > 0 ):
66+ _elapsed_frames += 1
67+ if (_elapsed_frames > _wait_process_frames ):
68+ _end_wait ()
69+
70+
71+ func _on_tree_physics_frame ():
72+ # Count frames here instead of in _physics_process so that tree order never
73+ # makes a difference and the count/signaling happens outside of
74+ # _physics_process being called.
75+ if (_wait_physics_frames != 0 ):
76+ _elapsed_frames += 1
77+ if (_elapsed_frames > _wait_physics_frames ):
78+ _end_wait ()
2179
2280
2381func _physics_process (delta ):
82+ if (is_waiting ()):
83+ await_logger .waited (delta )
84+
2485 if (_wait_time != 0.0 ):
2586 _elapsed_time += delta
2687 if (_elapsed_time >= _wait_time ):
2788 _end_wait ()
2889
29- if (_wait_frames != 0 ):
30- _elapsed_frames += 1
31- if (_elapsed_frames >= _wait_frames ):
32- _end_wait ()
33-
34- if (_predicate_function_waiting_to_be_true != null ):
90+ if (_predicate_method != null ):
3591 _predicate_time_between_elpased += delta
3692 if (_predicate_time_between_elpased >= _predicate_time_between ):
3793 _predicate_time_between_elpased = 0.0
38- var result = _predicate_function_waiting_to_be_true .call ()
39- if (typeof (result ) == TYPE_BOOL and result ):
40- _end_wait ()
94+ var result = _predicate_method .call ()
95+ if (_waiting_for_predicate_to_be == false ):
96+ if (typeof (result ) != TYPE_BOOL or result != true ):
97+ _end_wait ()
98+ else :
99+ if (typeof (result ) == TYPE_BOOL and result == _waiting_for_predicate_to_be ):
100+ _end_wait ()
41101
42102
43103func _end_wait ():
104+ await_logger .reset ()
44105 # Check for time before checking for frames so that the extra frames added
45106 # when waiting on a signal do not cause a false negative for timing out.
46107 if (_wait_time > 0 ):
47108 _did_last_wait_timeout = _elapsed_time >= _wait_time
48- elif (_wait_frames > 0 ):
49- _did_last_wait_timeout = _elapsed_frames >= _wait_frames
50-
51- if (_signal_to_wait_on != null and _signal_to_wait_on .is_connected (_signal_callback )):
109+ elif (_wait_physics_frames > 0 ):
110+ _did_last_wait_timeout = _elapsed_frames >= _wait_physics_frames
111+ elif (_wait_process_frames > 0 ):
112+ _did_last_wait_timeout = _elapsed_frames >= _wait_process_frames
113+
114+ if (_signal_to_wait_on != null and \
115+ is_instance_valid (_signal_to_wait_on .get_object ()) and \
116+ _signal_to_wait_on .is_connected (_signal_callback )):
52117 _signal_to_wait_on .disconnect (_signal_callback )
53118
119+ _wait_process_frames = 0
54120 _wait_time = 0.0
55- _wait_frames = 0
121+ _wait_physics_frames = 0
56122 _signal_to_wait_on = null
57- _predicate_function_waiting_to_be_true = null
123+ _predicate_method = null
58124 _elapsed_time = 0.0
59125 _elapsed_frames = 0
60126 timeout .emit ()
@@ -68,38 +134,68 @@ func _signal_callback(
68134
69135 _signal_to_wait_on .disconnect (_signal_callback )
70136 # DO NOT _end_wait here. For other parts of the test to get the signal that
71- # was waited on, we have to wait for a couple more frames. For example, the
137+ # was waited on, we have to wait for another frames. For example, the
72138 # signal_watcher doesn't get the signal in time if we don't do this.
73- _wait_frames = 2
139+ _wait_process_frames = 1
140+
74141
75- func wait_seconds (x ):
142+ func wait_seconds (x , msg = '' ):
143+ await_logger .waiting_on = str (x , " seconds " , msg )
76144 _did_last_wait_timeout = false
77145 _wait_time = x
78146 wait_started .emit ()
79147
80148
81- func wait_frames (x ):
149+ func wait_process_frames (x , msg = '' ):
150+ await_logger .waiting_on = str (x , " idle frames " , msg )
151+ _did_last_wait_timeout = false
152+ _wait_process_frames = x
153+ wait_started .emit ()
154+
155+
156+ func wait_physics_frames (x , msg = '' ):
157+ await_logger .waiting_on = str (x , " physics frames " , msg )
82158 _did_last_wait_timeout = false
83- _wait_frames = x
159+ _wait_physics_frames = x
84160 wait_started .emit ()
85161
86162
87- func wait_for_signal (the_signal , max_time ):
163+ func wait_for_signal (the_signal : Signal , max_time , msg = '' ):
164+ await_logger .waiting_on = str ("signal " , the_signal .get_name (), " or " , max_time , "s " , msg )
88165 _did_last_wait_timeout = false
89166 the_signal .connect (_signal_callback )
90167 _signal_to_wait_on = the_signal
91168 _wait_time = max_time
92169 wait_started .emit ()
93170
94171
95- func wait_until (predicate_function : Callable , max_time , time_between_calls := 0.0 ):
172+ func wait_until (predicate_function : Callable , max_time , time_between_calls := 0.0 , msg = '' ):
173+ await_logger .waiting_on = str ("callable to return TRUE or " , max_time , "s. " , msg )
96174 _predicate_time_between = time_between_calls
97- _predicate_function_waiting_to_be_true = predicate_function
175+ _predicate_method = predicate_function
176+ _wait_time = max_time
177+
178+ _waiting_for_predicate_to_be = true
98179 _predicate_time_between_elpased = 0.0
99180 _did_last_wait_timeout = false
181+
182+ wait_started .emit ()
183+
184+
185+ func wait_while (predicate_function : Callable , max_time , time_between_calls := 0.0 , msg = '' ):
186+ await_logger .waiting_on = str ("callable to return FALSE or " , max_time , "s. " , msg )
187+ _predicate_time_between = time_between_calls
188+ _predicate_method = predicate_function
100189 _wait_time = max_time
190+
191+ _waiting_for_predicate_to_be = false
192+ _predicate_time_between_elpased = 0.0
193+ _did_last_wait_timeout = false
194+
101195 wait_started .emit ()
102196
103197
104198func is_waiting ():
105- return _wait_time != 0.0 || _wait_frames != 0
199+ return _wait_time != 0.0 || \
200+ _wait_physics_frames != 0 || \
201+ _wait_process_frames != 0
0 commit comments