@@ -3,12 +3,21 @@ extends Node
33signal timeout
44signal wait_started
55
6- var _wait_time = 0.0
7- var _wait_frames = 0
6+ var _wait_time : = 0.0
7+ var _wait_frames : = 0
88var _signal_to_wait_on = null
99
10- var _elapsed_time = 0.0
11- var _elapsed_frames = 0
10+ var _predicate_function_waiting_to_be_true = null
11+ var _predicate_time_between := 0.0
12+ var _predicate_time_between_elpased := 0.0
13+
14+ var _did_last_wait_timeout = false
15+ var did_last_wait_timeout = false :
16+ get : return _did_last_wait_timeout
17+ set (val ): push_error ("Cannot set did_last_wait_timeout" )
18+
19+ var _elapsed_time := 0.0
20+ var _elapsed_frames := 0
1221
1322
1423func _physics_process (delta ):
@@ -22,49 +31,75 @@ func _physics_process(delta):
2231 if (_elapsed_frames >= _wait_frames ):
2332 _end_wait ()
2433
34+ if (_predicate_function_waiting_to_be_true != null ):
35+ _predicate_time_between_elpased += delta
36+ if (_predicate_time_between_elpased >= _predicate_time_between ):
37+ _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 ()
41+
2542
2643func _end_wait ():
44+ # Check for time before checking for frames so that the extra frames added
45+ # when waiting on a signal do not cause a false negative for timing out.
46+ if (_wait_time > 0 ):
47+ _did_last_wait_timeout = _elapsed_time >= _wait_time
48+ elif (_wait_frames > 0 ):
49+ _did_last_wait_timeout = _elapsed_frames >= _wait_frames
50+
2751 if (_signal_to_wait_on != null and _signal_to_wait_on .is_connected (_signal_callback )):
2852 _signal_to_wait_on .disconnect (_signal_callback )
2953
3054 _wait_time = 0.0
3155 _wait_frames = 0
3256 _signal_to_wait_on = null
57+ _predicate_function_waiting_to_be_true = null
3358 _elapsed_time = 0.0
3459 _elapsed_frames = 0
3560 timeout .emit ()
3661
3762
3863const ARG_NOT_SET = '_*_argument_*_is_*_not_set_*_'
3964func _signal_callback (
40- arg1 = ARG_NOT_SET , arg2 = ARG_NOT_SET , arg3 = ARG_NOT_SET ,
41- arg4 = ARG_NOT_SET , arg5 = ARG_NOT_SET , arg6 = ARG_NOT_SET ,
42- arg7 = ARG_NOT_SET , arg8 = ARG_NOT_SET , arg9 = ARG_NOT_SET ):
65+ _arg1 = ARG_NOT_SET , _arg2 = ARG_NOT_SET , _arg3 = ARG_NOT_SET ,
66+ _arg4 = ARG_NOT_SET , _arg5 = ARG_NOT_SET , _arg6 = ARG_NOT_SET ,
67+ _arg7 = ARG_NOT_SET , _arg8 = ARG_NOT_SET , _arg9 = ARG_NOT_SET ):
4368
4469 _signal_to_wait_on .disconnect (_signal_callback )
4570 # DO NOT _end_wait here. For other parts of the test to get the signal that
4671 # was waited on, we have to wait for a couple more frames. For example, the
4772 # signal_watcher doesn't get the signal in time if we don't do this.
4873 _wait_frames = 2
4974
50-
51- func wait_for ( x ):
75+ func wait_seconds ( x ):
76+ _did_last_wait_timeout = false
5277 _wait_time = x
5378 wait_started .emit ()
5479
5580
5681func wait_frames (x ):
82+ _did_last_wait_timeout = false
5783 _wait_frames = x
5884 wait_started .emit ()
5985
6086
61- func wait_for_signal (the_signal , x ):
87+ func wait_for_signal (the_signal , max_time ):
88+ _did_last_wait_timeout = false
6289 the_signal .connect (_signal_callback )
6390 _signal_to_wait_on = the_signal
64- _wait_time = x
91+ _wait_time = max_time
92+ wait_started .emit ()
93+
94+
95+ func wait_until (predicate_function : Callable , max_time , time_between_calls := 0.0 ):
96+ _predicate_time_between = time_between_calls
97+ _predicate_function_waiting_to_be_true = predicate_function
98+ _predicate_time_between_elpased = 0.0
99+ _did_last_wait_timeout = false
100+ _wait_time = max_time
65101 wait_started .emit ()
66102
67103
68104func is_waiting ():
69105 return _wait_time != 0.0 || _wait_frames != 0
70-
0 commit comments