diff --git a/addons/block_code/block_code_node/block_code.gd b/addons/block_code/block_code_node/block_code.gd index 12be8825..9fdf4fe0 100644 --- a/addons/block_code/block_code_node/block_code.gd +++ b/addons/block_code/block_code_node/block_code.gd @@ -45,9 +45,21 @@ func _update_parent_script(): var script := GDScript.new() script.set_source_code(block_script.generated_script) script.reload() + + # Persist export script variables (like SimpleCharacter exported texture) + var persist_properties = {} + var old_property_list = parent.get_property_list() + for property in old_property_list: + if property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE: + persist_properties[property.name] = parent.get(property.name) + parent.set_script(script) parent.set_process(true) + # Set persisted script variables in new script + for property_name in persist_properties: + parent.set(property_name, persist_properties.get(property_name)) + func _get_configuration_warnings(): var warnings = [] diff --git a/addons/block_code/examples/pong_game/paddle.gd b/addons/block_code/examples/pong_game/paddle.gd deleted file mode 100644 index 033c26c5..00000000 --- a/addons/block_code/examples/pong_game/paddle.gd +++ /dev/null @@ -1,33 +0,0 @@ -@tool -class_name Paddle -extends CharacterBody2D - - -func get_custom_class(): - return "Paddle" - - -static func get_exposed_properties() -> Array[String]: - return ["position"] - - -static func get_custom_blocks() -> Array[Block]: - var b: Block - var block_list: Array[Block] = [] - - # Movement - b = CategoryFactory.BLOCKS["statement_block"].instantiate() - b.block_type = Types.BlockType.EXECUTE - b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}" - b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()" - b.category = "Movement" - block_list.append(b) - - b = CategoryFactory.BLOCKS["statement_block"].instantiate() - b.block_type = Types.BlockType.EXECUTE - b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}" - b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()" - b.category = "Movement" - block_list.append(b) - - return block_list diff --git a/addons/block_code/examples/pong_game/paddle.tscn b/addons/block_code/examples/pong_game/paddle.tscn deleted file mode 100644 index e55a355a..00000000 --- a/addons/block_code/examples/pong_game/paddle.tscn +++ /dev/null @@ -1,19 +0,0 @@ -[gd_scene load_steps=4 format=3 uid="uid://s7enbp56f256"] - -[ext_resource type="Script" path="res://addons/block_code/examples/pong_game/paddle.gd" id="1_74lee"] -[ext_resource type="Texture2D" uid="uid://tplpgtnfeda0" path="res://addons/block_code/examples/pong_game/assets/paddle.png" id="1_eucti"] - -[sub_resource type="RectangleShape2D" id="RectangleShape2D_bbbjr"] -size = Vector2(128, 384) - -[node name="Paddle" type="CharacterBody2D" groups=["paddles"]] -collision_mask = 68 -motion_mode = 1 -script = ExtResource("1_74lee") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_bbbjr") - -[node name="Sprite2D" type="Sprite2D" parent="."] -unique_name_in_owner = true -texture = ExtResource("1_eucti") diff --git a/addons/block_code/examples/pong_game/pong_game.tscn b/addons/block_code/examples/pong_game/pong_game.tscn index 8dfa3838..bf47255d 100644 --- a/addons/block_code/examples/pong_game/pong_game.tscn +++ b/addons/block_code/examples/pong_game/pong_game.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=165 format=3 uid="uid://tf7b8c64ecc0"] +[gd_scene load_steps=166 format=3 uid="uid://tf7b8c64ecc0"] -[ext_resource type="PackedScene" uid="uid://s7enbp56f256" path="res://addons/block_code/examples/pong_game/paddle.tscn" id="1_1k5k2"] [ext_resource type="Script" path="res://addons/block_code/examples/pong_game/pong_game.gd" id="1_bjkc8"] [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/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"] @@ -13,84 +14,108 @@ [ext_resource type="PackedScene" uid="uid://1ownc3rikn2k" path="res://addons/block_code/examples/pong_game/ball_spawn_area.tscn" id="10_5vs1t"] [ext_resource type="PackedScene" uid="uid://fhoapg3anjsu" path="res://addons/block_code/examples/pong_game/goal_area.tscn" id="12_nqmxu"] -[sub_resource type="Resource" id="Resource_2ownr"] +[sub_resource type="Resource" id="Resource_02fc8"] script = ExtResource("5_wr38c") block_class = &"StatementBlock" -serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.290196, 0.52549, 0.835294, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 2 buttons, speed {speed: VECTOR2}"], ["statement", "velocity = Input.get_vector(\"player_2_left\", \"player_2_right\", \"player_2_up\", \"player_2_down\")*{speed} +serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 1 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2() +dir.x += float(Input.is_key_pressed(KEY_D)) +dir.x -= float(Input.is_key_pressed(KEY_A)) +dir.y += float(Input.is_key_pressed(KEY_S)) +dir.y -= float(Input.is_key_pressed(KEY_W)) +dir = dir.normalized() +velocity = dir*{speed} move_and_slide()"], ["defaults", {}], ["param_input_strings", { -"speed": "0, 1000" +"speed": "0,1000" }]] -[sub_resource type="Resource" id="Resource_lt7l7"] +[sub_resource type="Resource" id="Resource_hofsq"] script = ExtResource("4_qtggh") -serialized_block = SubResource("Resource_2ownr") +serialized_block = SubResource("Resource_02fc8") path_child_pairs = [] -[sub_resource type="Resource" id="Resource_lp80u"] +[sub_resource type="Resource" id="Resource_ijy7o"] script = ExtResource("5_wr38c") block_class = &"EntryBlock" -serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(36, 43)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]] +serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(82, 24)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]] -[sub_resource type="Resource" id="Resource_0j1nn"] +[sub_resource type="Resource" id="Resource_wlov0"] script = ExtResource("4_qtggh") -serialized_block = SubResource("Resource_lp80u") -path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_lt7l7")]] +serialized_block = SubResource("Resource_ijy7o") +path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_hofsq")]] -[sub_resource type="Resource" id="Resource_t1ec3"] +[sub_resource type="Resource" id="Resource_frqvp"] script = ExtResource("6_ppdc3") -array = Array[ExtResource("4_qtggh")]([SubResource("Resource_0j1nn")]) +array = Array[ExtResource("4_qtggh")]([SubResource("Resource_wlov0")]) -[sub_resource type="Resource" id="Resource_t7nl4"] +[sub_resource type="Resource" id="Resource_qmak3"] script = ExtResource("7_uuuue") -script_inherits = "Paddle" -block_trees = SubResource("Resource_t1ec3") -generated_script = "extends Paddle +script_inherits = "SimpleCharacter" +block_trees = SubResource("Resource_frqvp") +generated_script = "extends SimpleCharacter var VAR_DICT := {} func _process(delta): - velocity = Input.get_vector(\"player_2_left\", \"player_2_right\", \"player_2_up\", \"player_2_down\")*Vector2(0, 1000) + var dir = Vector2() + dir.x += float(Input.is_key_pressed(KEY_D)) + dir.x -= float(Input.is_key_pressed(KEY_A)) + dir.y += float(Input.is_key_pressed(KEY_S)) + dir.y -= float(Input.is_key_pressed(KEY_W)) + dir = dir.normalized() + velocity = dir*Vector2(0,1000) move_and_slide() " -[sub_resource type="Resource" id="Resource_lf6va"] +[sub_resource type="Resource" id="Resource_wvsmi"] script = ExtResource("5_wr38c") block_class = &"StatementBlock" -serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.290196, 0.52549, 0.835294, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 1 buttons, speed {speed: VECTOR2}"], ["statement", "velocity = Input.get_vector(\"ui_left\", \"ui_right\", \"ui_up\", \"ui_down\")*{speed} +serialized_props = [["block_name", "statement_block"], ["label", "StatementBlock"], ["color", Color(0.439216, 0.501961, 0.564706, 1)], ["block_type", 2], ["position", Vector2(0, 0)], ["block_format", "Move with player 2 buttons, speed {speed: VECTOR2}"], ["statement", "var dir = Vector2() +dir.x += float(Input.is_key_pressed(KEY_RIGHT)) +dir.x -= float(Input.is_key_pressed(KEY_LEFT)) +dir.y += float(Input.is_key_pressed(KEY_DOWN)) +dir.y -= float(Input.is_key_pressed(KEY_UP)) +dir = dir.normalized() +velocity = dir*{speed} move_and_slide()"], ["defaults", {}], ["param_input_strings", { -"speed": "0, 1000" +"speed": "0,1000" }]] -[sub_resource type="Resource" id="Resource_5obqu"] +[sub_resource type="Resource" id="Resource_v80v0"] script = ExtResource("4_qtggh") -serialized_block = SubResource("Resource_lf6va") +serialized_block = SubResource("Resource_wvsmi") path_child_pairs = [] -[sub_resource type="Resource" id="Resource_hhevk"] +[sub_resource type="Resource" id="Resource_3qglq"] script = ExtResource("5_wr38c") block_class = &"EntryBlock" -serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(34, 50)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]] +serialized_props = [["block_name", "process_block"], ["label", "EntryBlock"], ["color", Color(0.980392, 0.34902, 0.337255, 1)], ["block_type", 1], ["position", Vector2(57, 27)], ["block_format", "On Process"], ["statement", "func _process(delta):"], ["defaults", {}], ["param_input_strings", {}], ["signal_name", ""]] -[sub_resource type="Resource" id="Resource_ipx1e"] +[sub_resource type="Resource" id="Resource_gnrty"] script = ExtResource("4_qtggh") -serialized_block = SubResource("Resource_hhevk") -path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_5obqu")]] +serialized_block = SubResource("Resource_3qglq") +path_child_pairs = [[NodePath("VBoxContainer/SnapPoint"), SubResource("Resource_v80v0")]] -[sub_resource type="Resource" id="Resource_iqrk3"] +[sub_resource type="Resource" id="Resource_2qfet"] script = ExtResource("6_ppdc3") -array = Array[ExtResource("4_qtggh")]([SubResource("Resource_ipx1e")]) +array = Array[ExtResource("4_qtggh")]([SubResource("Resource_gnrty")]) -[sub_resource type="Resource" id="Resource_52r02"] +[sub_resource type="Resource" id="Resource_lxj2y"] script = ExtResource("7_uuuue") -script_inherits = "Paddle" -block_trees = SubResource("Resource_iqrk3") -generated_script = "extends Paddle +script_inherits = "SimpleCharacter" +block_trees = SubResource("Resource_2qfet") +generated_script = "extends SimpleCharacter var VAR_DICT := {} func _process(delta): - velocity = Input.get_vector(\"ui_left\", \"ui_right\", \"ui_up\", \"ui_down\")*Vector2(0, 1000) + var dir = Vector2() + dir.x += float(Input.is_key_pressed(KEY_RIGHT)) + dir.x -= float(Input.is_key_pressed(KEY_LEFT)) + dir.y += float(Input.is_key_pressed(KEY_DOWN)) + dir.y -= float(Input.is_key_pressed(KEY_UP)) + dir = dir.normalized() + velocity = dir*Vector2(0,1000) move_and_slide() " @@ -1020,21 +1045,25 @@ script = ExtResource("1_bjkc8") [node name="Space" parent="." instance=ExtResource("1_y56ac")] -[node name="PaddleLeft" parent="." instance=ExtResource("1_1k5k2")] -modulate = Color(0.511, 0.362, 0.972, 1) +[node name="PaddleLeft" parent="." instance=ExtResource("3_hjpbs")] +modulate = Color(0.509804, 0.360784, 0.972549, 1) position = Vector2(64, 544) +collision_mask = 5 +texture = ExtResource("4_ra7bh") [node name="BlockCode" type="Node" parent="PaddleLeft"] script = ExtResource("3_6jaq8") -block_script = SubResource("Resource_t7nl4") +block_script = SubResource("Resource_qmak3") -[node name="PaddleRight" parent="." instance=ExtResource("1_1k5k2")] -modulate = Color(0.511, 0.362, 0.972, 1) +[node name="PaddleRight" parent="." instance=ExtResource("3_hjpbs")] +modulate = Color(0.509804, 0.360784, 0.972549, 1) position = Vector2(1856, 544) +collision_mask = 5 +texture = ExtResource("4_ra7bh") [node name="BlockCode" type="Node" parent="PaddleRight"] script = ExtResource("3_6jaq8") -block_script = SubResource("Resource_52r02") +block_script = SubResource("Resource_lxj2y") [node name="Ball" parent="." instance=ExtResource("9_xrqll")] modulate = Color(0.511, 0.362, 0.972, 1) diff --git a/addons/block_code/simple_nodes/simple_character/simple_character.gd b/addons/block_code/simple_nodes/simple_character/simple_character.gd index 4f989e13..fbecbcff 100644 --- a/addons/block_code/simple_nodes/simple_character/simple_character.gd +++ b/addons/block_code/simple_nodes/simple_character/simple_character.gd @@ -2,21 +2,30 @@ class_name SimpleCharacter extends CharacterBody2D -var sprite_texture: Texture2D = preload("res://icon.svg") +@export var texture: Texture2D: + set = _set_texture + + +func _set_texture(new_texture): + texture = new_texture + + if not is_node_ready(): + return + + $Sprite2D.texture = texture + var shape = RectangleShape2D.new() + shape.size = Vector2(100, 100) if texture == null else texture.get_size() + $CollisionShape2D.shape = shape func _ready(): - $Sprite2D.texture = sprite_texture + _set_texture(texture) func get_custom_class(): return "SimpleCharacter" -static func get_exposed_properties() -> Array[String]: - return ["position"] - - static func get_custom_blocks() -> Array[Block]: var b: Block var block_list: Array[Block] = [] @@ -24,15 +33,33 @@ static func get_custom_blocks() -> Array[Block]: # Movement b = CategoryFactory.BLOCKS["statement_block"].instantiate() b.block_type = Types.BlockType.EXECUTE - b.block_format = "Move with player 1 buttons, speed {speed: INT}" - b.statement = 'velocity = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")*{speed}\n' + "move_and_slide()" + b.block_format = "Move with player 1 buttons, speed {speed: VECTOR2}" + b.statement = ( + "var dir = Vector2()\n" + + "dir.x += float(Input.is_key_pressed(KEY_D))\n" + + "dir.x -= float(Input.is_key_pressed(KEY_A))\n" + + "dir.y += float(Input.is_key_pressed(KEY_S))\n" + + "dir.y -= float(Input.is_key_pressed(KEY_W))\n" + + "dir = dir.normalized()\n" + + "velocity = dir*{speed}\n" + + "move_and_slide()" + ) b.category = "Input" block_list.append(b) b = CategoryFactory.BLOCKS["statement_block"].instantiate() b.block_type = Types.BlockType.EXECUTE - b.block_format = "Move with player 2 buttons, speed {speed: INT}" - b.statement = 'velocity = Input.get_vector("player_2_left", "player_2_right", "player_2_up", "player_2_down")*{speed}\n' + "move_and_slide()" + b.block_format = "Move with player 2 buttons, speed {speed: VECTOR2}" + b.statement = ( + "var dir = Vector2()\n" + + "dir.x += float(Input.is_key_pressed(KEY_RIGHT))\n" + + "dir.x -= float(Input.is_key_pressed(KEY_LEFT))\n" + + "dir.y += float(Input.is_key_pressed(KEY_DOWN))\n" + + "dir.y -= float(Input.is_key_pressed(KEY_UP))\n" + + "dir = dir.normalized()\n" + + "velocity = dir*{speed}\n" + + "move_and_slide()" + ) b.category = "Input" block_list.append(b) diff --git a/addons/block_code/simple_nodes/simple_character/simple_character.tscn b/addons/block_code/simple_nodes/simple_character/simple_character.tscn index 28193764..692a2cff 100644 --- a/addons/block_code/simple_nodes/simple_character/simple_character.tscn +++ b/addons/block_code/simple_nodes/simple_character/simple_character.tscn @@ -1,16 +1,14 @@ -[gd_scene load_steps=4 format=3 uid="uid://ddx1cd5q6t61o"] +[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="Texture2D" uid="uid://dr8e0tvfxjy1f" path="res://icon.svg" id="2_vn8k5"] -[sub_resource type="CircleShape2D" id="CircleShape2D_dfyyg"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_104qs"] +size = Vector2(16, 16) [node name="SimpleCharacter" type="CharacterBody2D"] script = ExtResource("1_oqwef") [node name="Sprite2D" type="Sprite2D" parent="."] -scale = Vector2(0.16, 0.16) -texture = ExtResource("2_vn8k5") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("CircleShape2D_dfyyg") +shape = SubResource("RectangleShape2D_104qs") diff --git a/addons/block_code/ui/picker/categories/category_factory.gd b/addons/block_code/ui/picker/categories/category_factory.gd index ac92f35b..22c35043 100644 --- a/addons/block_code/ui/picker/categories/category_factory.gd +++ b/addons/block_code/ui/picker/categories/category_factory.gd @@ -478,6 +478,29 @@ static func get_built_in_blocks(_class_name: String) -> Array[Block]: b.category = "Signal" block_list.append(b) + "CharacterBody2D": + var b = BLOCKS["statement_block"].instantiate() + b.block_type = Types.BlockType.EXECUTE + b.block_format = "Move with keys {up: STRING} {down: STRING} {left: STRING} {right: STRING} with speed {speed: VECTOR2}" + b.statement = ( + "var dir = Vector2()\n" + + "dir.x += float(Input.is_key_pressed(OS.find_keycode_from_string({right})))\n" + + "dir.x -= float(Input.is_key_pressed(OS.find_keycode_from_string({left})))\n" + + "dir.y += float(Input.is_key_pressed(OS.find_keycode_from_string({down})))\n" + + "dir.y -= float(Input.is_key_pressed(OS.find_keycode_from_string({up})))\n" + + "dir = dir.normalized()\n" + + "velocity = dir*{speed}\n" + + "move_and_slide()" + ) + b.defaults = { + "up": "W", + "down": "S", + "left": "A", + "right": "D", + } + b.category = "Input" + block_list.append(b) + var prop_list = ClassDB.class_get_property_list(_class_name, true) block_list.append_array(blocks_from_property_list(prop_list, props)) diff --git a/project.godot b/project.godot index 4bdb9c12..c267cf01 100644 --- a/project.godot +++ b/project.godot @@ -23,28 +23,9 @@ SignalManager="*res://addons/block_code/block_code_node/utilities/signal_manager enabled=PackedStringArray("res://addons/block_code/plugin.cfg", "res://addons/gut/plugin.cfg", "res://addons/plugin_refresher/plugin.cfg") -[input] - -player_2_up={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null) -] -} -player_2_down={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) -] -} -player_2_left={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null) -] -} -player_2_right={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null) -] -} +[physics] + +2d/default_gravity=0.0 [rendering]