add call time resolution of port values

This commit is contained in:
Lera Elvoé 2023-06-13 18:23:10 +03:00
parent 83d5fae1bd
commit 46169147a4
No known key found for this signature in database
6 changed files with 124 additions and 3 deletions

View file

@ -21,6 +21,8 @@ static var type_assoc: Dictionary = {
Types.DICTIONARY: DeckType.DeckTypeDictionary, Types.DICTIONARY: DeckType.DeckTypeDictionary,
} }
var variable_stack: Dictionary = {}
func add_node(node: GDScript, meta: Dictionary = {}) -> DeckNode: func add_node(node: GDScript, meta: Dictionary = {}) -> DeckNode:
# TODO: accept instances of DeckNode instead of instancing here? # TODO: accept instances of DeckNode instead of instancing here?

View file

@ -4,6 +4,7 @@ var name: String
var input_ports: Array[Port] var input_ports: Array[Port]
var output_ports: Array[Port] var output_ports: Array[Port]
var outgoing_connections: Dictionary var outgoing_connections: Dictionary
var incoming_connections: Dictionary
var ports: Array[Port] var ports: Array[Port]
@ -49,7 +50,7 @@ func send(from_port: int, data: DeckType, extra_data: Array = []) -> void:
# key is node uuid # key is node uuid
# value is input port on destination node # value is input port on destination node
for node in connection: for node in connection:
_belonging_to.get_node(node)._receive(connection[node], data, extra_data) get_node(node)._receive(connection[node], data, extra_data)
func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void: func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
@ -68,6 +69,26 @@ func add_outgoing_connection(from_port: int, to_node: String, to_port: int) -> v
var port_connections: Array = outgoing_connections.get(from_port, []) var port_connections: Array = outgoing_connections.get(from_port, [])
port_connections.append({to_node: to_port}) port_connections.append({to_node: to_port})
outgoing_connections[from_port] = port_connections outgoing_connections[from_port] = port_connections
get_node(to_node).add_incoming_connection(to_port, _id, from_port)
func add_incoming_connection(to_port: int, from_node: String, from_port: int) -> void:
var connection := {from_node: from_port}
incoming_connections[to_port] = connection
func request_value(on_port: int) -> Variant:
if !incoming_connections.has(on_port):
return null
var connection: Dictionary = incoming_connections[on_port]
var node := get_node(connection.keys()[0])
return node._value_request(connection.values()[0])
# override this
func _value_request(from_port: int) -> Variant:
return null
func remove_outgoing_connection(from_port: int, connection_hash: int) -> void: func remove_outgoing_connection(from_port: int, connection_hash: int) -> void:

View file

@ -0,0 +1,16 @@
extends DeckNode
func _init() -> void:
add_output_port(
Deck.Types.STRING,
"Variable",
"field"
)
name = "Get Deck Var"
func _value_request(from_port: int) -> Variant:
var key = ports[0].value_callback.call()
return _belonging_to.variable_stack[key]

View file

@ -27,7 +27,14 @@ func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
if to_port != 1: if to_port != 1:
return return
var data_to_print = ports[0].value_callback.call() if ports[0].value_callback.call() != "" else data.get_value() var data_to_print
if request_value(0) != null:
data_to_print = request_value(0)
elif ports[0].value_callback.call() != "":
data_to_print = ports[0].value_callback.call()
else:
data_to_print = data.get_value()
# var data_to_print = input_ports[0].value_callback.call() # var data_to_print = input_ports[0].value_callback.call()
print(data_to_print) print(data_to_print)
print("extra data: ", extra_data) print("extra data: ", extra_data)

View file

@ -0,0 +1,52 @@
extends DeckNode
func _init() -> void:
add_input_port(
Deck.Types.STRING,
"Variable Name",
"field"
)
add_input_port(
Deck.Types.STRING,
"Value",
"field"
)
add_input_port(
Deck.Types.BOOL,
"Set",
"button"
)
add_output_port(
Deck.Types.STRING,
"Value",
"label"
)
name = "Set Deck Var"
func _receive(to_port: int, data: DeckType, extra_data: Array = []) -> void:
if to_port != 2:
return
var var_name: String = get_value_for_port(0, data)
# String for now, until i figure out an Any type
var var_value: String = get_value_for_port(1, data)
_belonging_to.variable_stack[var_name] = var_value
send(3, DeckType.DeckTypeString.new(var_value))
# this can probably go into DeckNode with a different name that makes it clear
# that it prioritizes call-time resolution
func get_value_for_port(port: int, data: DeckType) -> Variant:
if request_value(port) != null:
return request_value(port)
elif ports[port].value_callback.call() != "":
return ports[port].value_callback.call()
else:
return data.get_value()

View file

@ -5,15 +5,22 @@ const NODE_SCENE := preload("res://graph_node_renderer/deck_node_renderer_graph_
var deck: Deck = Deck.new() var deck: Deck = Deck.new()
var button_node = preload("res://classes/deck/nodes/button.gd") var button_node = preload("res://classes/deck/nodes/button.gd")
var print_node = preload("res://classes/deck/nodes/print.gd") var print_node = preload("res://classes/deck/nodes/print.gd")
var get_var_node = preload("res://classes/deck/nodes/get_deck_var.gd")
var set_var_node = preload("res://classes/deck/nodes/set_deck_var.gd")
func _ready() -> void: func _ready() -> void:
var add_button := Button.new() var add_button := Button.new()
add_button.text = "Button" add_button.text = "Button"
var add_print := Button.new() var add_print := Button.new()
add_print.text = "Print" add_print.text = "Print"
var get_var := Button.new()
get_var.text = "Get Var"
var set_var := Button.new()
set_var.text = "Set Var"
get_zoom_hbox().add_child(add_button) get_zoom_hbox().add_child(add_button)
get_zoom_hbox().add_child(add_print) get_zoom_hbox().add_child(add_print)
get_zoom_hbox().add_child(get_var)
get_zoom_hbox().add_child(set_var)
add_button.pressed.connect( add_button.pressed.connect(
func(): func():
@ -31,6 +38,22 @@ func _ready() -> void:
add_child(node_renderer) add_child(node_renderer)
) )
get_var.pressed.connect(
func():
var node := deck.add_node(get_var_node)
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
node_renderer.node = node
add_child(node_renderer)
)
set_var.pressed.connect(
func():
var node := deck.add_node(set_var_node)
var node_renderer: DeckNodeRendererGraphNode = NODE_SCENE.instantiate()
node_renderer.node = node
add_child(node_renderer)
)
connection_request.connect(attempt_connection) connection_request.connect(attempt_connection)
disconnection_request.connect(attempt_disconnect) disconnection_request.connect(attempt_disconnect)