|
| 1 | +extends Node |
| 2 | + |
| 3 | +signal timeout |
| 4 | +signal wait_started |
| 5 | + |
| 6 | +var _wait_time := 0.0 |
| 7 | +var _wait_frames := 0 |
| 8 | +var _signal_to_wait_on = null |
| 9 | + |
| 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 |
| 21 | + |
| 22 | + |
| 23 | +func _physics_process(delta): |
| 24 | + if(_wait_time != 0.0): |
| 25 | + _elapsed_time += delta |
| 26 | + if(_elapsed_time >= _wait_time): |
| 27 | + _end_wait() |
| 28 | + |
| 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): |
| 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 | + |
| 42 | + |
| 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 | + |
| 51 | + if(_signal_to_wait_on != null and _signal_to_wait_on.is_connected(_signal_callback)): |
| 52 | + _signal_to_wait_on.disconnect(_signal_callback) |
| 53 | + |
| 54 | + _wait_time = 0.0 |
| 55 | + _wait_frames = 0 |
| 56 | + _signal_to_wait_on = null |
| 57 | + _predicate_function_waiting_to_be_true = null |
| 58 | + _elapsed_time = 0.0 |
| 59 | + _elapsed_frames = 0 |
| 60 | + timeout.emit() |
| 61 | + |
| 62 | + |
| 63 | +const ARG_NOT_SET = '_*_argument_*_is_*_not_set_*_' |
| 64 | +func _signal_callback( |
| 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): |
| 68 | + |
| 69 | + _signal_to_wait_on.disconnect(_signal_callback) |
| 70 | + # 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 |
| 72 | + # signal_watcher doesn't get the signal in time if we don't do this. |
| 73 | + _wait_frames = 2 |
| 74 | + |
| 75 | +func wait_seconds(x): |
| 76 | + _did_last_wait_timeout = false |
| 77 | + _wait_time = x |
| 78 | + wait_started.emit() |
| 79 | + |
| 80 | + |
| 81 | +func wait_frames(x): |
| 82 | + _did_last_wait_timeout = false |
| 83 | + _wait_frames = x |
| 84 | + wait_started.emit() |
| 85 | + |
| 86 | + |
| 87 | +func wait_for_signal(the_signal, max_time): |
| 88 | + _did_last_wait_timeout = false |
| 89 | + the_signal.connect(_signal_callback) |
| 90 | + _signal_to_wait_on = the_signal |
| 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 |
| 101 | + wait_started.emit() |
| 102 | + |
| 103 | + |
| 104 | +func is_waiting(): |
| 105 | + return _wait_time != 0.0 || _wait_frames != 0 |
0 commit comments