Skip to content

Commit 9e3e997

Browse files
committed
Build Simple node scenes in code
The method to instantiate a scene internally in 6d017d0 is clever, but the real classes and internal classes create a circular dependency that cause import errors. Since there are only a couple child nodes that are significantly updated by the exported variables, just create the whole scene in code. There should be no functional changes besides dropping the internal scenes. The only change is that the simple nodes can only be added as a node type and not by the scene file. I don't think that will be missed. https://phabricator.endlessm.com/T35494
1 parent def3586 commit 9e3e997

File tree

7 files changed

+49
-101
lines changed

7 files changed

+49
-101
lines changed

addons/block_code/examples/pong_game/pong_game.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ position = Vector2(1984, 544)
841841
script = ExtResource("3_6jaq8")
842842
block_script = SubResource("Resource_6drva")
843843

844-
[node name="SimpleScoring" type="CanvasLayer" parent="." groups=["hud"]]
844+
[node name="SimpleScoring" type="CanvasLayer" parent="."]
845845
follow_viewport_enabled = true
846846
script = ExtResource("13_tg3yk")
847847

addons/block_code/simple_nodes/simple_character/_simple_character.gd

Lines changed: 0 additions & 8 deletions
This file was deleted.

addons/block_code/simple_nodes/simple_character/_simple_character.tscn

Lines changed: 0 additions & 14 deletions
This file was deleted.

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const Types = preload("res://addons/block_code/types/types.gd")
88
@export var texture: Texture2D:
99
set = _set_texture
1010

11-
@export var speed: Vector2 = Vector2(300, 300):
12-
set = _set_speed
11+
@export var speed: Vector2 = Vector2(300, 300)
1312

1413
const PLAYER_KEYS = {
1514
"player_1":
@@ -28,6 +27,9 @@ const PLAYER_KEYS = {
2827
}
2928
}
3029

30+
var sprite: Sprite2D
31+
var collision: CollisionShape2D
32+
3133
# Get the gravity from the project settings to be synced with RigidBody nodes.
3234
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
3335

@@ -36,18 +38,12 @@ var _jumping = false
3638

3739
func _set_texture(new_texture):
3840
texture = new_texture
41+
_texture_updated()
3942

40-
if not is_node_ready():
41-
return
42-
43-
$Sprite2D.texture = texture
44-
var shape = RectangleShape2D.new()
45-
shape.size = Vector2(100, 100) if texture == null else texture.get_size()
46-
$CollisionShape2D.shape = shape
4743

48-
49-
func _set_speed(new_speed):
50-
speed = new_speed
44+
func _texture_updated():
45+
sprite.texture = texture
46+
collision.shape.size = Vector2(100, 100) if texture == null else texture.get_size()
5147

5248

5349
## Nodes in the "affected_by_gravity" group will receive gravity changes:
@@ -56,23 +52,16 @@ func on_gravity_changed(new_gravity):
5652

5753

5854
func _init():
59-
if self.get_parent():
60-
return
61-
62-
var node = preload("res://addons/block_code/simple_nodes/simple_character/_simple_character.tscn").instantiate() as Node
63-
node.replace_by(self, true)
64-
node.queue_free()
65-
scene_file_path = ""
66-
67-
68-
func _ready():
6955
add_to_group("affected_by_gravity")
70-
simple_setup()
7156

57+
sprite = Sprite2D.new()
58+
add_child(sprite)
59+
60+
collision = CollisionShape2D.new()
61+
collision.shape = RectangleShape2D.new()
62+
add_child(collision)
7263

73-
func simple_setup():
74-
_set_texture(texture)
75-
_set_speed(speed)
64+
_texture_updated()
7665

7766

7867
func get_custom_class():

addons/block_code/simple_nodes/simple_scoring/_simple_scoring.gd

Lines changed: 0 additions & 8 deletions
This file was deleted.

addons/block_code/simple_nodes/simple_scoring/_simple_scoring.tscn

Lines changed: 0 additions & 30 deletions
This file was deleted.

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,48 @@ const Types = preload("res://addons/block_code/types/types.gd")
1111
@export var score_right: int:
1212
set = _set_score_right
1313

14-
@onready var _score_labels = {
15-
"left": %PlayerLeftScore,
16-
"right": %PlayerRightScore,
17-
}
14+
var _score_labels: Dictionary
1815

1916
const _POSITIONS_FOR_PLAYER = {
2017
"1": "left",
2118
"2": "right",
2219
}
2320

2421

22+
func _create_score_label(player: String) -> Label:
23+
var label := Label.new()
24+
25+
var x_pos: int
26+
match player:
27+
"left":
28+
x_pos = 240
29+
"right":
30+
x_pos = 1200
31+
_:
32+
push_error('Unrecognized SimpleScoring player "%s"' % player)
33+
34+
label.set_size(Vector2(477, 1080))
35+
label.set_position(Vector2(x_pos, 0))
36+
label.pivot_offset = Vector2(240, 176)
37+
label.size_flags_horizontal = Control.SizeFlags.SIZE_EXPAND_FILL
38+
label.size_flags_vertical = Control.SizeFlags.SIZE_FILL
39+
label.add_theme_font_size_override("font_size", 200)
40+
label.text = "0"
41+
label.horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER
42+
43+
return label
44+
45+
2546
func _init():
26-
if self.get_parent():
27-
return
47+
add_to_group("hud")
48+
49+
var left_player_score := _create_score_label("left")
50+
_score_labels["left"] = left_player_score
51+
add_child(left_player_score)
2852

29-
var node = preload("res://addons/block_code/simple_nodes/simple_scoring/_simple_scoring.tscn").instantiate() as Node
30-
node.replace_by(self, true)
31-
node.queue_free()
32-
scene_file_path = ""
53+
var right_player_score := _create_score_label("right")
54+
_score_labels["right"] = right_player_score
55+
add_child(right_player_score)
3356

3457

3558
func get_custom_class():
@@ -38,16 +61,12 @@ func get_custom_class():
3861

3962
func _set_score_left(new_score_left):
4063
score_left = new_score_left
41-
if not is_node_ready():
42-
await ready
4364
if score_left:
4465
_update_label("left", score_left)
4566

4667

4768
func _set_score_right(new_score_right):
4869
score_right = new_score_right
49-
if not is_node_ready():
50-
await ready
5170
if score_right:
5271
_update_label("right", score_right)
5372

0 commit comments

Comments
 (0)