Skip to content

Commit 850d0ab

Browse files
authored
Merge pull request #61 from endlessm/store-audiostream-in-var_dict
block_code/picker: Store the AudioStreamPlayer in the VAR_DICT
2 parents 944a25c + fccd0ad commit 850d0ab

File tree

5 files changed

+277
-82
lines changed

5 files changed

+277
-82
lines changed

addons/block_code/ui/picker/categories/category_factory.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,14 @@ static func get_general_categories() -> Array[BlockCategory]:
237237
b = BLOCKS["statement_block"].instantiate()
238238
b.block_type = Types.BlockType.EXECUTE
239239
b.block_format = "Load file {file_path: STRING} as sound {name: STRING}"
240-
b.statement = "var sound = AudioStreamPlayer.new()\nsound.name = {name}\nsound.set_stream(load({file_path}))\nadd_child(sound)\nsound.set_owner(self)"
240+
b.statement = "VAR_DICT[{name}] = AudioStreamPlayer.new()\nVAR_DICT[{name}].name = {name}\nVAR_DICT[{name}].set_stream(load({file_path}))\nadd_child(VAR_DICT[{name}])"
241241
sound_list.append(b)
242242

243243
b = BLOCKS["statement_block"].instantiate()
244244
b.block_type = Types.BlockType.EXECUTE
245245
b.block_format = "Play the sound {name: STRING} with Volume dB {db: FLOAT} and Pitch Scale {pitch: FLOAT}"
246-
b.statement = "var sound = find_child({name})\nsound.volume_db = {db}\nsound.pitch_scale = {pitch}\nsound.play()"
246+
b.statement = "VAR_DICT[{name}].volume_db = {db}\nVAR_DICT[{name}].pitch_scale = {pitch}\nVAR_DICT[{name}].play()"
247+
b.defaults = {"db": "0.0", "pitch": "1.0"}
247248
sound_list.append(b)
248249

249250
var sound_category: BlockCategory = BlockCategory.new("Sound", sound_list, Color("e30fc0"))

pong_game/ball.tscn

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
[gd_scene load_steps=7 format=3 uid="uid://c7l70grmkauij"]
1+
[gd_scene load_steps=5 format=3 uid="uid://c7l70grmkauij"]
22

33
[ext_resource type="Script" path="res://pong_game/ball.gd" id="1_vdrab"]
44
[ext_resource type="Texture2D" uid="uid://bcgr5amsq3jfl" path="res://pong_game/ball.png" id="2_xkrmm"]
5-
[ext_resource type="AudioStream" uid="uid://bc7jd3d8m5spt" path="res://pong_game/wall_hit.ogg" id="3_mjbsd"]
6-
[ext_resource type="AudioStream" uid="uid://jk0oapxjw354" path="res://pong_game/paddle_hit.ogg" id="4_skr8k"]
75

86
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_c3m63"]
97
friction = 0.0
@@ -31,11 +29,3 @@ shape = SubResource("CircleShape2D_sntrn")
3129
[node name="Sprite2D" type="Sprite2D" parent="."]
3230
unique_name_in_owner = true
3331
texture = ExtResource("2_xkrmm")
34-
35-
[node name="WallAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
36-
stream = ExtResource("3_mjbsd")
37-
38-
[node name="PaddleAudioStreamPlayer" type="AudioStreamPlayer" parent="."]
39-
stream = ExtResource("4_skr8k")
40-
41-
[connection signal="body_entered" from="." to="." method="_on_body_entered"]

pong_game/pong_game.tscn

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=33 format=3 uid="uid://tf7b8c64ecc0"]
1+
[gd_scene load_steps=61 format=3 uid="uid://tf7b8c64ecc0"]
22

33
[ext_resource type="PackedScene" uid="uid://s7enbp56f256" path="res://pong_game/paddle.tscn" id="1_1k5k2"]
44
[ext_resource type="Script" path="res://pong_game/pong_game.gd" id="1_bjkc8"]
@@ -95,6 +95,210 @@ func _process(delta):
9595
9696
"
9797

98+
[sub_resource type="Resource" id="Resource_w7llf"]
99+
script = ExtResource("5_wr38c")
100+
block_class = &"StatementBlock"
101+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.890196, 0.0588235, 0.752941, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Load file {file_path: STRING} as sound {name: STRING}"], ["statement", "VAR_DICT[{name}] = AudioStreamPlayer.new()
102+
VAR_DICT[{name}].name = {name}
103+
VAR_DICT[{name}].set_stream(load({file_path}))
104+
add_child(VAR_DICT[{name}])"], ["defaults", {}], ["param_input_strings", {
105+
"file_path": "res://pong_game/wall_hit.ogg",
106+
"name": "wall_hit"
107+
}]]
108+
109+
[sub_resource type="Resource" id="Resource_dvwua"]
110+
script = ExtResource("4_qtggh")
111+
serialized_block = SubResource("Resource_w7llf")
112+
path_child_pairs = []
113+
114+
[sub_resource type="Resource" id="Resource_rrqjv"]
115+
script = ExtResource("5_wr38c")
116+
block_class = &"StatementBlock"
117+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.890196, 0.0588235, 0.752941, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Load file {file_path: STRING} as sound {name: STRING}"], ["statement", "VAR_DICT[{name}] = AudioStreamPlayer.new()
118+
VAR_DICT[{name}].name = {name}
119+
VAR_DICT[{name}].set_stream(load({file_path}))
120+
add_child(VAR_DICT[{name}])"], ["defaults", {}], ["param_input_strings", {
121+
"file_path": "res://pong_game/paddle_hit.ogg",
122+
"name": "paddle_hit"
123+
}]]
124+
125+
[sub_resource type="Resource" id="Resource_c4ce1"]
126+
script = ExtResource("4_qtggh")
127+
serialized_block = SubResource("Resource_rrqjv")
128+
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_dvwua")]]
129+
130+
[sub_resource type="Resource" id="Resource_r8cwo"]
131+
script = ExtResource("5_wr38c")
132+
block_class = &"EntryBlock"
133+
serialized_props = [["block_name", "ready_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(54, 47)], ["block_format", "On Ready"], ["statement", "func _ready():"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]]
134+
135+
[sub_resource type="Resource" id="Resource_pgiuj"]
136+
script = ExtResource("4_qtggh")
137+
serialized_block = SubResource("Resource_r8cwo")
138+
path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_c4ce1")]]
139+
140+
[sub_resource type="Resource" id="Resource_lkcfx"]
141+
script = ExtResource("5_wr38c")
142+
block_class = &"ParameterBlock"
143+
serialized_props = [["block_name", "parameter_block"], ["label", "Param"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 3], ["position", Vector2(0, 0)], ["block_format", "body"], ["statement", "body"], ["defaults", {}], ["variant_type", 22], ["param_input_strings", {}]]
144+
145+
[sub_resource type="Resource" id="Resource_vne13"]
146+
script = ExtResource("4_qtggh")
147+
serialized_block = SubResource("Resource_lkcfx")
148+
path_child_pairs = []
149+
150+
[sub_resource type="Resource" id="Resource_mkixe"]
151+
script = ExtResource("5_wr38c")
152+
block_class = &"ParameterBlock"
153+
serialized_props = [["block_name", "parameter_block"], ["label", "Param"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 3], ["position", Vector2(0, 0)], ["block_format", "body"], ["statement", "body"], ["defaults", {}], ["variant_type", 22], ["param_input_strings", {}]]
154+
155+
[sub_resource type="Resource" id="Resource_ja8qr"]
156+
script = ExtResource("4_qtggh")
157+
serialized_block = SubResource("Resource_mkixe")
158+
path_child_pairs = []
159+
160+
[sub_resource type="Resource" id="Resource_6233g"]
161+
script = ExtResource("5_wr38c")
162+
block_class = &"ParameterBlock"
163+
serialized_props = [["block_name", "parameter_block"], ["label", "Param"], ["color", Color(0.941176, 0.764706, 0, 1)], ["block_type", 3], ["position", Vector2(0, 0)], ["block_format", "Is {node: NODE_PATH} in group {group: STRING}"], ["statement", "get_node({node}).is_in_group({group})"], ["defaults", {}], ["variant_type", 1], ["param_input_strings", {
164+
"group": "paddles",
165+
"node": ""
166+
}]]
167+
168+
[sub_resource type="Resource" id="Resource_ar1vw"]
169+
script = ExtResource("4_qtggh")
170+
serialized_block = SubResource("Resource_6233g")
171+
path_child_pairs = [[NodePath("MarginContainer/HBoxContainer/ParameterInput0/SnapPoint"), SubResource("Resource_ja8qr")]]
172+
173+
[sub_resource type="Resource" id="Resource_hbwws"]
174+
script = ExtResource("5_wr38c")
175+
block_class = &"StatementBlock"
176+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.890196, 0.0588235, 0.752941, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Play the sound {name: STRING} with Volume dB {db: FLOAT} and Pitch Scale {pitch: FLOAT}"], ["statement", "VAR_DICT[{name}].volume_db = {db}
177+
VAR_DICT[{name}].pitch_scale = {pitch}
178+
VAR_DICT[{name}].play()"], ["defaults", {}], ["param_input_strings", {
179+
"db": "0.0",
180+
"name": "paddle_hit",
181+
"pitch": "1.0"
182+
}]]
183+
184+
[sub_resource type="Resource" id="Resource_xb342"]
185+
script = ExtResource("4_qtggh")
186+
serialized_block = SubResource("Resource_hbwws")
187+
path_child_pairs = []
188+
189+
[sub_resource type="Resource" id="Resource_y0dfo"]
190+
script = ExtResource("5_wr38c")
191+
block_class = &"ParameterBlock"
192+
serialized_props = [["block_name", "parameter_block"], ["label", "Param"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 3], ["position", Vector2(0, 0)], ["block_format", "body"], ["statement", "body"], ["defaults", {}], ["variant_type", 22], ["param_input_strings", {}]]
193+
194+
[sub_resource type="Resource" id="Resource_lkc6w"]
195+
script = ExtResource("4_qtggh")
196+
serialized_block = SubResource("Resource_y0dfo")
197+
path_child_pairs = []
198+
199+
[sub_resource type="Resource" id="Resource_ou8eo"]
200+
script = ExtResource("5_wr38c")
201+
block_class = &"ParameterBlock"
202+
serialized_props = [["block_name", "parameter_block"], ["label", "Param"], ["color", Color(0.941176, 0.764706, 0, 1)], ["block_type", 3], ["position", Vector2(0, 0)], ["block_format", "Is {node: NODE_PATH} in group {group: STRING}"], ["statement", "get_node({node}).is_in_group({group})"], ["defaults", {}], ["variant_type", 1], ["param_input_strings", {
203+
"group": "walls",
204+
"node": ""
205+
}]]
206+
207+
[sub_resource type="Resource" id="Resource_jp11d"]
208+
script = ExtResource("4_qtggh")
209+
serialized_block = SubResource("Resource_ou8eo")
210+
path_child_pairs = [[NodePath("MarginContainer/HBoxContainer/ParameterInput0/SnapPoint"), SubResource("Resource_lkc6w")]]
211+
212+
[sub_resource type="Resource" id="Resource_rer42"]
213+
script = ExtResource("5_wr38c")
214+
block_class = &"StatementBlock"
215+
serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.890196, 0.0588235, 0.752941, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Play the sound {name: STRING} with Volume dB {db: FLOAT} and Pitch Scale {pitch: FLOAT}"], ["statement", "VAR_DICT[{name}].volume_db = {db}
216+
VAR_DICT[{name}].pitch_scale = {pitch}
217+
VAR_DICT[{name}].play()"], ["defaults", {}], ["param_input_strings", {
218+
"db": "0.0",
219+
"name": "wall_hit",
220+
"pitch": "1.0"
221+
}]]
222+
223+
[sub_resource type="Resource" id="Resource_nanxw"]
224+
script = ExtResource("4_qtggh")
225+
serialized_block = SubResource("Resource_rer42")
226+
path_child_pairs = []
227+
228+
[sub_resource type="Resource" id="Resource_8ljc2"]
229+
script = ExtResource("5_wr38c")
230+
block_class = &"ControlBlock"
231+
serialized_props = [["block_name", "control_block"], ["label", "Control Block"], ["color", Color(1, 0.678431, 0.462745, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_formats", ["if {condition: BOOL}"]], ["statements", ["if {condition}:"]], ["defaults", {}], ["param_input_strings_array", [{
232+
"condition": true
233+
}]]]
234+
235+
[sub_resource type="Resource" id="Resource_ewj1y"]
236+
script = ExtResource("4_qtggh")
237+
serialized_block = SubResource("Resource_8ljc2")
238+
path_child_pairs = [[NodePath("VBoxContainer/MarginContainer/Rows/Row0/RowHBoxContainer/RowHBox/ParameterInput0/SnapPoint"), SubResource("Resource_jp11d")], [NodePath("VBoxContainer/MarginContainer/Rows/SnapContainer0/SnapPoint"), SubResource("Resource_nanxw")]]
239+
240+
[sub_resource type="Resource" id="Resource_nwfcj"]
241+
script = ExtResource("5_wr38c")
242+
block_class = &"ControlBlock"
243+
serialized_props = [["block_name", "control_block"], ["label", "Control Block"], ["color", Color(1, 0.678431, 0.462745, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_formats", ["if {condition: BOOL}"]], ["statements", ["if {condition}:"]], ["defaults", {}], ["param_input_strings_array", [{
244+
"condition": true
245+
}]]]
246+
247+
[sub_resource type="Resource" id="Resource_up363"]
248+
script = ExtResource("4_qtggh")
249+
serialized_block = SubResource("Resource_nwfcj")
250+
path_child_pairs = [[NodePath("VBoxContainer/MarginContainer/Rows/Row0/RowHBoxContainer/RowHBox/ParameterInput0/SnapPoint"), SubResource("Resource_ar1vw")], [NodePath("VBoxContainer/MarginContainer/Rows/SnapContainer0/SnapPoint"), SubResource("Resource_xb342")], [NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_ewj1y")]]
251+
252+
[sub_resource type="Resource" id="Resource_uuy53"]
253+
script = ExtResource("5_wr38c")
254+
block_class = &"EntryBlock"
255+
serialized_props = [["block_name", "entry_block"], ["label", "EntryBlock"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 1], ["position", Vector2(54, 207)], ["block_format", "On [body: NODE_PATH] entered"], ["statement", "func _on_body_entered(_body: Node):
256+
var body: NodePath = _body.get_path()"], ["defaults", {}], ["param_input_strings", {
257+
"body": ""
258+
}], ["signal_name", "body_entered"]]
259+
260+
[sub_resource type="Resource" id="Resource_6ki5d"]
261+
script = ExtResource("4_qtggh")
262+
serialized_block = SubResource("Resource_uuy53")
263+
path_child_pairs = [[NodePath("VBoxContainer/TopMarginContainer/MarginContainer/HBoxContainer/ParameterInput0/SnapPoint"), SubResource("Resource_vne13")], [NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_up363")]]
264+
265+
[sub_resource type="Resource" id="Resource_7j4bl"]
266+
script = ExtResource("6_ppdc3")
267+
array = Array[ExtResource("4_qtggh")]([SubResource("Resource_pgiuj"), SubResource("Resource_6ki5d")])
268+
269+
[sub_resource type="Resource" id="Resource_gkcqr"]
270+
script = ExtResource("7_uuuue")
271+
script_inherits = "RigidBody2D"
272+
block_trees = SubResource("Resource_7j4bl")
273+
generated_script = "extends RigidBody2D
274+
275+
var VAR_DICT := {}
276+
277+
func _ready():
278+
VAR_DICT['paddle_hit'] = AudioStreamPlayer.new()
279+
VAR_DICT['paddle_hit'].name = 'paddle_hit'
280+
VAR_DICT['paddle_hit'].set_stream(load('res://pong_game/paddle_hit.ogg'))
281+
add_child(VAR_DICT['paddle_hit'])
282+
VAR_DICT['wall_hit'] = AudioStreamPlayer.new()
283+
VAR_DICT['wall_hit'].name = 'wall_hit'
284+
VAR_DICT['wall_hit'].set_stream(load('res://pong_game/wall_hit.ogg'))
285+
add_child(VAR_DICT['wall_hit'])
286+
287+
func _on_body_entered(_body: Node):
288+
var body: NodePath = _body.get_path()
289+
if get_node(body).is_in_group('paddles'):
290+
VAR_DICT['paddle_hit'].volume_db = 0.0
291+
VAR_DICT['paddle_hit'].pitch_scale = 1.0
292+
VAR_DICT['paddle_hit'].play()
293+
if get_node(body).is_in_group('walls'):
294+
VAR_DICT['wall_hit'].volume_db = 0.0
295+
VAR_DICT['wall_hit'].pitch_scale = 1.0
296+
VAR_DICT['wall_hit'].play()
297+
298+
func _init():
299+
body_entered.connect(_on_body_entered)
300+
"
301+
98302
[sub_resource type="Resource" id="Resource_ve47i"]
99303
script = ExtResource("5_wr38c")
100304
block_class = &"StatementBlock"
@@ -173,6 +377,10 @@ modulate = Color(0.511, 0.362, 0.972, 1)
173377
position = Vector2(960, 544)
174378
linear_velocity = Vector2(353.553, 353.553)
175379

380+
[node name="BlockCode" type="Node" parent="Ball"]
381+
script = ExtResource("3_6jaq8")
382+
block_script = SubResource("Resource_gkcqr")
383+
176384
[node name="BallSpawnArea" parent="." groups=["ball_spawn_area"] instance=ExtResource("10_5vs1t")]
177385
position = Vector2(960, 544)
178386

pong_game/space.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ patch_margin_bottom = 64
2525
axis_stretch_horizontal = 2
2626
axis_stretch_vertical = 2
2727

28-
[node name="Walls" type="RigidBody2D" parent="."]
28+
[node name="Walls" type="RigidBody2D" parent="." groups=["walls"]]
2929
collision_layer = 4
3030
collision_mask = 3
3131
collision_priority = 10.0

0 commit comments

Comments
 (0)