Skip to content

Commit 7b04092

Browse files
committed
Add code
1 parent b70cc27 commit 7b04092

File tree

7 files changed

+157
-1
lines changed

7 files changed

+157
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Godot 4+ specific ignores
2+
.godot/

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
Godot License
24+
25+
Copyright (c) 2014-present Godot Engine contributors.
26+
Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.
27+
28+
Permission is hereby granted, free of charge, to any person obtaining a copy
29+
of this software and associated documentation files (the "Software"), to deal
30+
in the Software without restriction, including without limitation the rights
31+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32+
copies of the Software, and to permit persons to whom the Software is
33+
furnished to do so, subject to the following conditions:
34+
35+
The above copyright notice and this permission notice shall be included in all
36+
copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44+
SOFTWARE.
45+
46+
-- Godot Engine <https://godotengine.org>

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
# godot-4-awaiter
1+
# godot-4-awaiter
2+
3+
For when you need to `await` multiple signals. No idea if this is something you _should_ do, but at least now you _can_ :).
4+
5+
**Godot 4 Only**
6+
7+
## Setup
8+
Download and move `./addons/awaiter` to your godot project's `./addons` folder.
9+
10+
11+
## Examples
12+
13+
14+
Wait for all of the signals to fire:
15+
```
16+
var t1_finished = get_tree().create_timer(1.0).timeout
17+
var t2_finished = get_tree().create_timer(2.0).timeout
18+
19+
# Expects an Array[Signal]
20+
await Awaiter.wait_for([t1_finished, t2_finished])
21+
22+
print('All Finished!')
23+
```
24+
25+
Wait until the first signal fires:
26+
```
27+
t1_finished = get_tree().create_timer(1.0).timeout
28+
t2_finished = get_tree().create_timer(2.0).timeout
29+
t2_finished.connect(func(): print("T2 Finished!"))
30+
31+
# Expects an Array[Signal]
32+
await FirstAwaiter.wait_for([t1_finished, t2_finished])
33+
34+
print('First Finished!')
35+
```
36+
37+
Prevent waiting forever if the node you are waiting for goes away:
38+
```
39+
var t1 = Timer.new()
40+
t1.wait_time = 2.0
41+
t1.autostart = true
42+
43+
var t2 = Timer.new()
44+
t2.wait_time = 1.0
45+
t2.autostart = true
46+
t2.timeout.connect(t1.queue_free)
47+
48+
add_child(t1)
49+
add_child(t2)
50+
51+
await FirstAwaiter.wait_for([t1.tree_exiting, t1.timeout])
52+
53+
print("Did not wait forever!")
54+
```

addons/awaiter/awaiter.gd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class_name Awaiter
2+
extends Object
3+
4+
signal finished
5+
6+
var total = 0
7+
var total_done = 0
8+
9+
static func wait_for(signals: Array[Signal]) -> Signal:
10+
return new(signals).finished
11+
12+
func _init(signals: Array[Signal]):
13+
total = signals.size()
14+
for sig in signals:
15+
await_signal(sig)
16+
17+
func await_signal(sig: Signal):
18+
await sig
19+
total_done += 1
20+
if _is_finished():
21+
finished.emit()
22+
call_deferred('free')
23+
24+
func _is_finished():
25+
return total_done >= total

addons/awaiter/examples/example.gd

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extends Node
2+
3+
func _ready():
4+
var t1_finished = get_tree().create_timer(1.0).timeout
5+
var t2_finished = get_tree().create_timer(2.0).timeout
6+
7+
await Awaiter.wait_for([t1_finished, t2_finished])
8+
9+
print('All Finished!')
10+
11+
t1_finished = get_tree().create_timer(1.0).timeout
12+
t2_finished = get_tree().create_timer(2.0).timeout
13+
t2_finished.connect(func(): print("T2 Finished!"))
14+
15+
await FirstAwaiter.wait_for([t1_finished, t2_finished])
16+
17+
print('First Finished!')
18+
19+
var t1 = Timer.new()
20+
t1.wait_time = 2.0
21+
t1.autostart = true
22+
23+
var t2 = Timer.new()
24+
t2.wait_time = 1.0
25+
t2.autostart = true
26+
t2.timeout.connect(t1.queue_free)
27+
28+
add_child(t1)
29+
add_child(t2)
30+
31+
await FirstAwaiter.wait_for([t1.tree_exiting, t1.timeout])
32+
33+
print("Did not wait forever!")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://be07pajuy7ua3"]
2+
3+
[ext_resource type="Script" path="res://addons/awaiter/examples/example.gd" id="1_pajuv"]
4+
5+
[node name="example" type="Node"]
6+
script = ExtResource("1_pajuv")

addons/awaiter/first_awaiter.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class_name FirstAwaiter
2+
extends Awaiter
3+
4+
5+
static func wait_for(signals: Array[Signal]) -> Signal:
6+
return new(signals).finished
7+
8+
9+
func _is_finished():
10+
var done = (total == 0 or total_done >= 1)
11+
return done
12+

0 commit comments

Comments
 (0)