Skip to content
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

Add Get Property from Node expression block #34

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions addons/block_code/types/types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ static func dijkstra(source: Variant.Type):


static func can_cast(type: Variant.Type, parent_type: Variant.Type) -> bool:
print("CAN CAST ", type, " -> ", parent_type)
if type == parent_type:
return true

if type == TYPE_NIL or parent_type == TYPE_NIL:
return true

if cast_graph.has(type) and cast_graph.has(parent_type):
dijkstra(type)
return dist[parent_type] < INT_MAX
Expand All @@ -116,6 +120,10 @@ static func cast(val: String, type: Variant.Type, parent_type: Variant.Type):
if type == parent_type:
return val

if type == TYPE_NIL or parent_type == TYPE_NIL:
print("CAST NIL", val)
return val

if cast_graph.has(type) and cast_graph.has(parent_type):
dijkstra(type)
if dist[parent_type] < INT_MAX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/parameter_block/parameter_block.gd" id="1_0hajy"]
[ext_resource type="PackedScene" uid="uid://c7puyxpqcq6xo" path="res://addons/block_code/ui/blocks/utilities/drag_drop_area/drag_drop_area.tscn" id="2_gy5co"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_dbera"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_8gcat"]
bg_color = Color(1, 1, 1, 1)
border_width_left = 3
border_width_top = 3
Expand All @@ -19,15 +19,14 @@ offset_right = 16.0
offset_bottom = 8.0
size_flags_horizontal = 0
script = ExtResource("1_0hajy")
defaults = null
block_name = "parameter_block"
label = "Param"
block_type = 3

[node name="Panel" type="Panel" parent="."]
unique_name_in_owner = true
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_dbera")
theme_override_styles/panel = SubResource("StyleBoxFlat_8gcat")

[node name="DragDropArea" parent="." instance=ExtResource("2_gy5co")]
layout_mode = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ func get_string() -> String:

if option:
return _option_input.get_item_text(_option_input.selected).to_snake_case()

print("GET STRING ", input, " with ", variant_type)

match variant_type:
TYPE_STRING:
TYPE_STRING, TYPE_NODE_PATH:
return "'%s'" % input.replace("\\", "\\\\").replace("'", "\\'")
TYPE_VECTOR2:
return "Vector2(%s)" % input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ size_flags_horizontal = 3

[node name="BoolInput" type="MarginContainer" parent="InputSwitcher"]
unique_name_in_owner = true
visible = false
layout_mode = 2
theme_override_constants/margin_left = 8

Expand Down
65 changes: 63 additions & 2 deletions addons/block_code/ui/picker/categories/category_factory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,17 @@ static func get_general_categories() -> Array[BlockCategory]:
variable_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_STRING
b.block_format = "Get String {var: STRING}"
b.statement = "VAR_DICT[{var}]"
variable_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_STRING
b.block_format = "Get String {var: STRING} from {node: NODE_PATH}"
b.statement = "get_node({node}).VAR_DICT[{var}]"
variable_list.append(b)

b = BLOCKS["statement_block"].instantiate()
b.block_format = "Set Int {var: STRING} {value: INT}"
b.statement = "VAR_DICT[{var}] = {value}"
Expand All @@ -160,12 +167,65 @@ static func get_general_categories() -> Array[BlockCategory]:
variable_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.block_format = "To String {int: INT}"
b.statement = "str({int})"
b.variant_type = TYPE_STRING
b.block_format = "To String {value: NIL}"
b.statement = "str({value})"
variable_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_INT
b.block_format = "Get Int {var: INT} from {node: NODE_PATH}"
b.statement = "get_node({node}).VAR_DICT[{var}]"
variable_list.append(b)

var variable_category: BlockCategory = BlockCategory.new("Variables", variable_list, Color("4f975d"))

# Nodes & Types
var type_list: Array[Block] = []

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_NODE_PATH
b.block_format = "This node"
b.statement = "get_path()"
type_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_NODE_PATH
b.block_format = "%{name: NIL}"
b.statement = "%{name}.get_path()"
type_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_INT
b.block_format = "As int {value: NIL}"
b.statement = "int({value})"
type_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_FLOAT
b.block_format = "As float {value: NIL}"
b.statement = "float({value})"
type_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_BOOL
b.block_format = "As boolean {value: NIL}"
b.statement = "bool({value})"
type_list.append(b)

b = BLOCKS["parameter_block"].instantiate()
b.variant_type = TYPE_NIL
b.block_format = "Get property {key: STRING} from {node: NODE_PATH}"
b.statement = "get_node({node}).get({key})"
type_list.append(b)

b = BLOCKS["statement_block"].instantiate()
b.block_format = "Set property {key: STRING} in {node: NODE_PATH} to {value: NIL}"
b.statement = "get_node({node}).set({key}, {value})"
type_list.append(b)

var type_category: BlockCategory = BlockCategory.new("Nodes & Types", type_list, Color("c12f8e"))

# Math
var math_list: Array[Block] = []

Expand Down Expand Up @@ -256,6 +316,7 @@ static func get_general_categories() -> Array[BlockCategory]:
math_category,
logic_category,
variable_category,
type_category,
input_category,
sound_category,
]
Expand Down
Loading
Loading