new way to instance nodes

This commit is contained in:
Lera Elvoé 2023-07-21 08:30:12 +03:00
parent 074edb1e1f
commit f9069db8be
No known key found for this signature in database
8 changed files with 47 additions and 25 deletions

View file

@ -12,6 +12,9 @@ var _belonging_to: Deck
var _id: String var _id: String
var node_type: String var node_type: String
var description: String
var aliases: Array[String]
var props_to_serialize: Array[StringName] var props_to_serialize: Array[StringName]
enum PortType{ enum PortType{

View file

@ -1,6 +1,8 @@
extends Node extends Node
const BASE_NODE_PATH := "res://classes/deck/nodes/" const BASE_NODE_PATH := "res://classes/deck/nodes/"
# Dictionary[node_type, NodeDescriptor]
var nodes: Dictionary = {} var nodes: Dictionary = {}
@ -11,12 +13,11 @@ func _init() -> void:
while current_file != "": while current_file != "":
print(current_file) print(current_file)
if current_file.ends_with(".gd"): if current_file.ends_with(".gd"):
var f := FileAccess.open(BASE_NODE_PATH.path_join(current_file), FileAccess.READ) # var f := FileAccess.open(BASE_NODE_PATH.path_join(current_file), FileAccess.READ)
var line := f.get_line() var script_path := BASE_NODE_PATH.path_join(current_file)
if !line.begins_with("###type="): var node: DeckNode = load(script_path).new() as DeckNode
current_file = dir.get_next() var descriptor := NodeDescriptor.new(script_path, node.name, node.description, node.aliases)
continue nodes[node.node_type] = descriptor
nodes[line.trim_prefix("###type=")] = BASE_NODE_PATH.path_join(current_file)
current_file = dir.get_next() current_file = dir.get_next()
@ -24,4 +25,23 @@ func instance_node(type: String) -> DeckNode:
if !nodes.has(type): if !nodes.has(type):
return null return null
return load(nodes[type]).new() return load(nodes[type]["script_path"]).new()
class NodeDescriptor:
var name: String
var description: String
var aliases: String
var script_path: String
func _init(p_script_path: String, p_name: String, p_description: String, p_aliases: Array[String]) -> void:
script_path = p_script_path
name = p_name
description = p_description
aliases = p_aliases.reduce(
func(accum, el) -> void:
accum += el
, "")

View file

@ -1,13 +1,13 @@
###type=button
extends DeckNode extends DeckNode
func _init() -> void: func _init() -> void:
name = "Button"
node_type = "button"
description = "a button"
add_output_port( add_output_port(
Deck.Types.BOOL, Deck.Types.BOOL,
"Press me", "Press me",
"button" "button"
) )
name = "Button"
node_type = name.to_snake_case()

View file

@ -1,17 +1,17 @@
###type=get_deck_var
extends DeckNode extends DeckNode
func _init() -> void: func _init() -> void:
name = "Get Deck Var"
node_type = name.to_snake_case()
description = "retrieve a deck variable"
add_output_port( add_output_port(
Deck.Types.STRING, Deck.Types.STRING,
"Variable", "Variable",
"field" "field"
) )
name = "Get Deck Var"
node_type = name.to_snake_case()
func _value_request(from_port: int) -> Variant: func _value_request(from_port: int) -> Variant:
var key = ports[0].value_callback.call() var key = ports[0].value_callback.call()

View file

@ -1 +0,0 @@
extends Node

View file

@ -1,8 +1,11 @@
###type=print
extends DeckNode extends DeckNode
func _init() -> void: func _init() -> void:
name = "Print"
node_type = name.to_snake_case()
description = "print a value"
add_input_port( add_input_port(
Deck.Types.STRING, Deck.Types.STRING,
"Text to print", "Text to print",
@ -21,9 +24,6 @@ func _init() -> void:
"label" "label"
) )
name = "Print"
node_type = name.to_snake_case()
func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
if to_port != 1: if to_port != 1:

View file

@ -1,8 +1,11 @@
###type=set_deck_var
extends DeckNode extends DeckNode
func _init() -> void: func _init() -> void:
name = "Set Deck Var"
node_type = name.to_snake_case()
description = "set deck variable"
add_input_port( add_input_port(
Deck.Types.STRING, Deck.Types.STRING,
"Variable Name", "Variable Name",
@ -27,9 +30,6 @@ func _init() -> void:
"label" "label"
) )
name = "Set Deck Var"
node_type = name.to_snake_case()
func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
if to_port != 2: if to_port != 2:

View file

@ -62,7 +62,7 @@ func _ready() -> void:
set_var.pressed.connect( set_var.pressed.connect(
func(): func():
var node := NodeDB.instance_node("set_deck_varbutton") var node := NodeDB.instance_node("set_deck_var")
deck.add_node_inst(node) deck.add_node_inst(node)
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate() var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
node_renderer.node = node node_renderer.node = node