Skip to content

Commit 6d017d0

Browse files
committed
Change Simple node scripts to instantiate themselves
To support users adding SimpleCharacter and SimpleScoring as nodes, instead of as child scenes, add an _init function for each which loads the appropriate scene file and instantiates it. https://phabricator.endlessm.com/T35545
1 parent 2516bd1 commit 6d017d0

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@tool
2+
extends SimpleCharacter
3+
4+
5+
func _init():
6+
# Disable the SimpleCharacter _init function. We expect this script is
7+
# already bound to _simple_character.tscn.
8+
pass
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[gd_scene load_steps=3 format=3 uid="uid://ddx1cd5q6t61o"]
22

3-
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_character/simple_character.gd" id="1_oqwef"]
3+
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_character/_simple_character.gd" id="1_idjqv"]
44

5-
[sub_resource type="RectangleShape2D" id="RectangleShape2D_104qs"]
6-
size = Vector2(16, 16)
5+
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ffh0f"]
6+
size = Vector2(100, 100)
77

88
[node name="SimpleCharacter" type="CharacterBody2D"]
9-
script = ExtResource("1_oqwef")
9+
script = ExtResource("1_idjqv")
1010

1111
[node name="Sprite2D" type="Sprite2D" parent="."]
1212

1313
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
14-
shape = SubResource("RectangleShape2D_104qs")
14+
shape = SubResource("RectangleShape2D_ffh0f")

addons/block_code/simple_nodes/simple_character/simple_character.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func on_gravity_changed(new_gravity):
5252
gravity = new_gravity
5353

5454

55+
func _init():
56+
if self.get_parent():
57+
return
58+
59+
var node = preload("res://addons/block_code/simple_nodes/simple_character/_simple_character.tscn").instantiate() as Node
60+
node.replace_by(self, true)
61+
node.queue_free()
62+
scene_file_path = ""
63+
64+
5565
func _ready():
5666
add_to_group("affected_by_gravity")
5767
simple_setup()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@tool
2+
extends SimpleScoring
3+
4+
5+
func _init():
6+
# Disable the SimpleScoring _init function. We expect this script is
7+
# already bound to _simple_scoring.tscn.
8+
pass

addons/block_code/simple_nodes/simple_scoring/simple_scoring.tscn renamed to addons/block_code/simple_nodes/simple_scoring/_simple_scoring.tscn

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

3-
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd" id="1_yyb01"]
3+
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_scoring/_simple_scoring.gd" id="1_lc5bg"]
44

55
[node name="SimpleScoring" type="CanvasLayer" groups=["hud"]]
6-
script = ExtResource("1_yyb01")
6+
script = ExtResource("1_lc5bg")
77

88
[node name="PlayerLeftScore" type="Label" parent="."]
99
unique_name_in_owner = true

addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const _POSITIONS_FOR_PLAYER = {
1919
}
2020

2121

22+
func _init():
23+
if self.get_parent():
24+
return
25+
26+
var node = preload("res://addons/block_code/simple_nodes/simple_scoring/_simple_scoring.tscn").instantiate() as Node
27+
node.replace_by(self, true)
28+
node.queue_free()
29+
print_tree_pretty()
30+
scene_file_path = ""
31+
32+
2233
func get_custom_class():
2334
return "SimpleScoring"
2435

0 commit comments

Comments
 (0)