-
Notifications
You must be signed in to change notification settings - Fork 17
bozar42 edited this page Apr 17, 2020
·
2 revisions
By the end of Chapter 9, MainScene.gd has been filled with two expressions with different arguments:
NODE_A.connect(SIGNAL_NAME, NODE_B, FUNCTION_NAME)
NODE_A = NODE_B
Is it possible to separate arguments from expressions? More specifically, we'd like to put arguments into a data structure and store them in MainScene.gd. Let another script to evaluate expressions based on these arguments.
We define two arrays for signal connection and node reference respectively. The element of each array is another array composed of strings.
# Binding signals
[
[signal, target_func, source_node, target_node_1, target_node_2, ...],
[signal, target_func, source_node, target_node_1, target_node_2, ...],
...
]
# Setting references
[
[target_var_name, source_node, target_node],
[target_var_name, source_node, target_node],
...
]
Let MainScene.gd extends a script and feed it with these user-defined arrays through object initialization.
# MainScene.gd
extends "res://library/RootNodeTemplate.gd"
const SIGNAL_BIND: Array = [
[
"sprite_created", "_on_InitWorld_sprite_created",
INIT_WORLD,
PC_MOVE, NPC, SCHEDULE, DUNGEON,
],
]
const NODE_REF: Array = [
[
"_ref_DungeonBoard",
DUNGEON,
PC_MOVE, PC_ATTACK, REMOVE, INIT_WORLD,
],
]
func _init().(SIGNAL_BIND, NODE_REF) -> void:
pass
Add RootNodeTemplate.gd to library/ folder. There are three key functions. You should be able to fill the missing code yourself. Or you can refer to my script.
# RootNodeTemplate.gd
func _set_signal() -> void:
var __
for s in _signal_bind:
# [signal_name, func_name, source_node, target_node]
for i in range(3, len(s)):
__ = get_node(_get_path(s[2])).connect(s[0],
get_node(_get_path(s[i])), s[1])
func _set_node_ref() -> void:
for n in _node_ref:
# [target_var_name, source_node, target_node]
for i in range(2, len(n)):
get_node(_get_path(n[i]))[n[0]] = get_node(_get_path(n[1]))
func _get_path(path_to_node: String) -> String:
return "{0}/{1}".format([_path_to_self, path_to_node])