Scripting, Code & Interaction: Difference between revisions

added a section about moving code to separate scripts. Potentially dangerous as I am relatively new to this matter. Please review.
mNo edit summary
(added a section about moving code to separate scripts. Potentially dangerous as I am relatively new to this matter. Please review.)
Line 284: Line 284:


And just in case you're confused about the '[' and ']' in there, that just means that it's something you don't have to include. Your functions don't have to have any parameters, and they can have up to 9.
And just in case you're confused about the '[' and ']' in there, that just means that it's something you don't have to include. Your functions don't have to have any parameters, and they can have up to 9.
==Moving functions to separate script files==
As a game becomes more complex, the code in GlobalScript becomes harder and harder to maintain. So it makes sense to place certain functions into separate script files.
A script consists of two parts: A header file (ending on .ash) and the script code file (ending on .asc). When you add a new script via the Script section of the project tree (right-click -> new script), a pair of these files will automatically be generated.
As an example, let us try move a function doStuff() from GlobalScript to a new script MyScript.
  function doStuff() {
  //doing stuff here
  }
When moving the code of the function from GlobalScript.asc to MyScript.asc, you will most likely get an error when you try to run it. This is due to the fact that the function has not yet been made public to the project. In order to do so, you have to import it in the header file MyScript.ash:
  import function doStuff();
 
If you have some experience in other programming languages, this may be a bit confusing, since the import does not work like ''#include'' in C/C++ or ''import'' in python. It does not make code visible to your script, but rather makes the function from the script visible to the whole project. Every function not imported that way will only be usable within the script where it is defined.
Note by the way that unlike global variables, functions do not need to be exported (using the ''export'' command) in order to work.
''So now my function can be accessed from anywhere?''
Well, almost. In the project tree, all scripts are organized as a list. Each script can only see functions defined in scripts above them in that list (and of course its own functions). GlobalScript is usually at the bottom of the list, so it can access all other functions, but no other script can access functions in GlobalScript. Room scripts are an exception, as they do not appear in the Scripts list but have access to all other scripts, including GlobalScript.
In our example, doStuff() can now be accessed from GlobalScript, any room script and any script below MyScript in the Scripts list.
''So what to do if my script has to call a function defined in GlobalScript?''
It cannot call a function in GlobalScript directly. Therefore it would be advisable to move that function to MyScript as well, or place it in a new script file that lies above MyScript.
''I tried that, and it works, but when I move a character action to my own script it stops working.''
Certain built-in functions (e.g. ''repeatedly_execute()'') can be used in custom scripts and will be called before their counterpart in GlobalScript is called.
Most other built-in functions can only be called from GlobalScript, and that includes any character or item-related actions.
If you still want to move these functions to separate scripts, you have to use helper functions.
As an example, let us try to create a working character interact function for a bartender character in MyScript:
# Create a character interact action cBartender_Interact from the character's action menu in the project tree. The corresponding function will be generated in GlobalScript.asc.
# Write a new function bartenderInteractHandler() in MyScript.asc to contain the actual action code
# Import the function in MyScript.ash to make it visible to the project
# Call bartenderInteractHandler() from the cBartender_Interact() function defined in GlobalScript.asc


==Defining custom hotkeys and shortcuts==
==Defining custom hotkeys and shortcuts==
2

edits