call - coldrockgames/doc-scriptor GitHub Wiki
With this command, you can call another script by its name.
Syntax
call "<script_name>"
Where script_name can be any expression, so you may dynamically build the name of the script to call, like:
call "monster_attacks_" + self.data.race
If you have placed your scripts in a folder structure inside your scripts/
folder, this structure is reflected in a scripts' name, when you want to call it.
Imagine this folder structure:
scripts
โโโโmonsters
โ attack_close.scriptor
โ attack_ranged.scriptor
โโโโnpcs
โ <...some more files...>
โโโโplayer
โ <...some more files...>
To call the "attack_close" script, you would simply use the full path name, with a slash /
as path separator:
call "monsters/attack_close"
Variables Are Shared
The call
command is way more powerful than you might think at first glance, because you also share variables between the scripts.
As scriptor, by design, does not offer functions or methods, you can work around this limitation with the call
command, as the invoked script will see the parent scripts' variables while it runs.
This example explains, what happens, if you call
another script.
// The called script (the "child")
// Just set a variable for demonstration
name = "Hello, I am the child"
// The executing script (the "parent")
ilog(name) // Will print nothing, as name does not exist
call child
ilog(name) // Will print "Hello, I am the child" now as the child has set it
Used right, you can satisfy both requirements of "functions" in a programming language:
- Pass in parameters/arguments to a function.
By simply setting your variables before youcall
the child script. - Receive return values from a function.
By simply accessing your variables after you havecall
ed the child script.
Let's repeat the example from above, but now with bidirectional variable usage:
// The called script (the "child")
// Just set a variable for demonstration
name = "Hello " + monster_name + ", I am the child"
// The executing script (the "parent")
monster_name = "Orc Warrior" // Set a script variable before calling the child
call child
ilog(name) // Will print "Hello Orc Warrior, I am the child" now as the child had also access to "monster_name"