-
Notifications
You must be signed in to change notification settings - Fork 16
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])
We have explained the naming convention for this demo at the end of Chapter 5. Here is a reminder.
Also note that we use
_ref
prefix to refer to node references, and_new
prefix to refer to script resources from thelibrary/
folder.
Define your own conventions so that you can tell these special variables from ordinary ones at a glance.
We have defined various constants instead of copying and pasting the same value everywhere to avoid mistakes. If the same constant appears in different scripts, you'd better to define it in a script and store the script in a specific place.
In the last part, I'd like to list something you can do beyond this tutorial.
Trivial features that appear in the demo but not covered here:
- Support controller inputs.
- Let PC wait one turn.
- Randomize the size of wall block.
Features that require more efforts:
- Let indicators move along with PC.
- Use object pool to store dead NPCs.
- Save and load the game.
Godot is easy to learn and suffices for me to create a 2D turn-based game, although it is not as powerful as commercial engines. However, no matter which tool you choose and what game you are making, good luck and have fun!