Skip to content

Change Simple node scripts to instantiate themselves #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions addons/block_code/examples/pong_game/pong_game.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[ext_resource type="PackedScene" uid="uid://cg8ibi18um3vg" path="res://addons/block_code/examples/pong_game/space.tscn" id="1_y56ac"]
[ext_resource type="Script" path="res://addons/block_code/block_code_node/block_code.gd" id="3_6jaq8"]
[ext_resource type="PackedScene" uid="uid://ddx1cd5q6t61o" path="res://addons/block_code/simple_nodes/simple_character/simple_character.tscn" id="3_hjpbs"]
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_character/simple_character.gd" id="3_mdrcv"]
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block_tree_node.gd" id="4_qtggh"]
[ext_resource type="Texture2D" uid="uid://tplpgtnfeda0" path="res://addons/block_code/examples/pong_game/assets/paddle.png" id="4_ra7bh"]
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block.gd" id="5_wr38c"]
[ext_resource type="Script" path="res://addons/block_code/ui/block_canvas/serialized_block_tree_node_array.gd" id="6_ppdc3"]
[ext_resource type="Script" path="res://addons/block_code/block_script_data/block_script_data.gd" id="7_uuuue"]
[ext_resource type="PackedScene" uid="uid://bis7afjjuwypq" path="res://addons/block_code/simple_nodes/simple_scoring/simple_scoring.tscn" id="8_yg457"]
[ext_resource type="PackedScene" uid="uid://c7l70grmkauij" path="res://addons/block_code/examples/pong_game/ball.tscn" id="9_xrqll"]
[ext_resource type="PackedScene" uid="uid://fhoapg3anjsu" path="res://addons/block_code/examples/pong_game/goal_area.tscn" id="12_nqmxu"]
[ext_resource type="Script" path="res://addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd" id="13_tg3yk"]

[sub_resource type="Resource" id="Resource_k6jw1"]
script = ExtResource("5_wr38c")
Expand Down Expand Up @@ -796,20 +796,22 @@ version = 0

[node name="Space" parent="." instance=ExtResource("1_y56ac")]

[node name="PaddleLeft" parent="." groups=["paddles"] instance=ExtResource("3_hjpbs")]
[node name="PaddleLeft" type="CharacterBody2D" parent="." groups=["paddles"]]
modulate = Color(0.509804, 0.360784, 0.972549, 1)
position = Vector2(64, 544)
collision_mask = 5
script = ExtResource("3_mdrcv")
texture = ExtResource("4_ra7bh")

[node name="BlockCode" type="Node" parent="PaddleLeft"]
script = ExtResource("3_6jaq8")
block_script = SubResource("Resource_qmak3")

[node name="PaddleRight" parent="." groups=["paddles"] instance=ExtResource("3_hjpbs")]
[node name="PaddleRight" type="CharacterBody2D" parent="." groups=["paddles"]]
modulate = Color(0.509804, 0.360784, 0.972549, 1)
position = Vector2(1856, 544)
collision_mask = 5
script = ExtResource("3_mdrcv")
texture = ExtResource("4_ra7bh")

[node name="BlockCode" type="Node" parent="PaddleRight"]
Expand Down Expand Up @@ -839,8 +841,9 @@ position = Vector2(1984, 544)
script = ExtResource("3_6jaq8")
block_script = SubResource("Resource_6drva")

[node name="SimpleScoring" parent="." instance=ExtResource("8_yg457")]
[node name="SimpleScoring" type="CanvasLayer" parent="." groups=["hud"]]
follow_viewport_enabled = true
script = ExtResource("13_tg3yk")

[node name="BlockCode" type="Node" parent="SimpleScoring"]
script = ExtResource("3_6jaq8")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@tool
extends SimpleCharacter


func _init():
# Disable the SimpleCharacter _init function. We expect this script is
# already bound to _simple_character.tscn.
pass
Comment on lines +5 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://ddx1cd5q6t61o"]

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

[sub_resource type="RectangleShape2D" id="RectangleShape2D_104qs"]
size = Vector2(16, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ffh0f"]
size = Vector2(100, 100)

[node name="SimpleCharacter" type="CharacterBody2D"]
script = ExtResource("1_oqwef")
script = ExtResource("1_idjqv")

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

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_104qs")
shape = SubResource("RectangleShape2D_ffh0f")
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func on_gravity_changed(new_gravity):
gravity = new_gravity


func _init():
if self.get_parent():
return

var node = preload("res://addons/block_code/simple_nodes/simple_character/_simple_character.tscn").instantiate() as Node
node.replace_by(self, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I play the Pong game, it shows error:

E 0:00:00:0445   simple_character.gd:57 @ _init(): Condition "p_node->data.parent" is true.
  <C++ Source>   scene/main/node.cpp:2796 @ replace_by()
  <Stack Trace>  simple_character.gd:57 @ _init()
                 block_code.gd:56 @ _update_parent_script()
                 block_code.gd:14 @ _ready()

node.queue_free()
scene_file_path = ""


func _ready():
add_to_group("affected_by_gravity")
simple_setup()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@tool
extends SimpleScoring


func _init():
# Disable the SimpleScoring _init function. We expect this script is
# already bound to _simple_scoring.tscn.
pass
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://bis7afjjuwypq"]

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

[node name="SimpleScoring" type="CanvasLayer" groups=["hud"]]
script = ExtResource("1_yyb01")
script = ExtResource("1_lc5bg")

[node name="PlayerLeftScore" type="Label" parent="."]
unique_name_in_owner = true
Expand Down
11 changes: 11 additions & 0 deletions addons/block_code/simple_nodes/simple_scoring/simple_scoring.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const _POSITIONS_FOR_PLAYER = {
}


func _init():
if self.get_parent():
return

var node = preload("res://addons/block_code/simple_nodes/simple_scoring/_simple_scoring.tscn").instantiate() as Node
node.replace_by(self, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I play the Pong game, it shows error:

E 0:00:00:0452   simple_scoring.gd:24 @ _init(): Condition "p_node->data.parent" is true.
  <C++ Source>   scene/main/node.cpp:2796 @ replace_by()
  <Stack Trace>  simple_scoring.gd:24 @ _init()
                 block_code.gd:56 @ _update_parent_script()
                 block_code.gd:14 @ _ready()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, I see how that happened. That's fixed in the newest set of commits.

node.queue_free()
print_tree_pretty()
scene_file_path = ""


func get_custom_class():
return "SimpleScoring"

Expand Down