miggor-StreamGraph/classes/deck/nodes/general/expression_node.gd
Lera Elvoé c07d810bcf be more lenient with using triggers and value requests on the same node on general and obs nodes (#78)
addresses part of #59

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/78
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
2024-02-26 05:34:00 +00:00

54 lines
1.3 KiB
GDScript

# (c) 2023-present Eroax
# (c) 2023-present Yagich
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
extends DeckNode
var expr = Expression.new()
func _init():
name = "Expression"
node_type = name.to_snake_case()
description = "A node returning the result of a mathematical expression."
props_to_serialize = []
# TODO: order of ports is changed
# due to https://github.com/godotengine/godot/issues/85558
# when it's fixed, switch it back
add_input_port(
DeckType.Types.DICTIONARY,
"Expression Input",
)
add_output_port(
DeckType.Types.ANY,
"Expression Text",
"codeblock",
)
func parse(input: Dictionary) -> Variant:
var text = get_output_ports()[0].value_callback.call()
var err = expr.parse(text, ["deck_var", "input"])
if err != OK:
DeckHolder.logger.log_node("Expression parse failed: %s" % err, Logger.LogType.ERROR)
printerr(err)
return null
var res = expr.execute([_belonging_to.variable_stack, input])
if expr.has_execute_failed():
DeckHolder.logger.log_node("Expression Execution Failed: %s" % text, Logger.LogType.ERROR)
return null
return res
func _value_request(_from_port : int) -> Variant:
var input = await request_value_async(0)
return parse(input)
func _receive(_on_input_port: int, data: Variant) -> void:
send(0, parse(data))