@@ -3,12 +3,21 @@ extends Node
3
3
signal timeout
4
4
signal wait_started
5
5
6
- var _wait_time = 0.0
7
- var _wait_frames = 0
6
+ var _wait_time : = 0.0
7
+ var _wait_frames : = 0
8
8
var _signal_to_wait_on = null
9
9
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
12
21
13
22
14
23
func _physics_process (delta ):
@@ -22,49 +31,75 @@ func _physics_process(delta):
22
31
if (_elapsed_frames >= _wait_frames ):
23
32
_end_wait ()
24
33
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
+
25
42
26
43
func _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
+
27
51
if (_signal_to_wait_on != null and _signal_to_wait_on .is_connected (_signal_callback )):
28
52
_signal_to_wait_on .disconnect (_signal_callback )
29
53
30
54
_wait_time = 0.0
31
55
_wait_frames = 0
32
56
_signal_to_wait_on = null
57
+ _predicate_function_waiting_to_be_true = null
33
58
_elapsed_time = 0.0
34
59
_elapsed_frames = 0
35
60
timeout .emit ()
36
61
37
62
38
63
const ARG_NOT_SET = '_*_argument_*_is_*_not_set_*_'
39
64
func _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 ):
43
68
44
69
_signal_to_wait_on .disconnect (_signal_callback )
45
70
# DO NOT _end_wait here. For other parts of the test to get the signal that
46
71
# was waited on, we have to wait for a couple more frames. For example, the
47
72
# signal_watcher doesn't get the signal in time if we don't do this.
48
73
_wait_frames = 2
49
74
50
-
51
- func wait_for ( x ):
75
+ func wait_seconds ( x ):
76
+ _did_last_wait_timeout = false
52
77
_wait_time = x
53
78
wait_started .emit ()
54
79
55
80
56
81
func wait_frames (x ):
82
+ _did_last_wait_timeout = false
57
83
_wait_frames = x
58
84
wait_started .emit ()
59
85
60
86
61
- func wait_for_signal (the_signal , x ):
87
+ func wait_for_signal (the_signal , max_time ):
88
+ _did_last_wait_timeout = false
62
89
the_signal .connect (_signal_callback )
63
90
_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
65
101
wait_started .emit ()
66
102
67
103
68
104
func is_waiting ():
69
105
return _wait_time != 0.0 || _wait_frames != 0
70
-
0 commit comments