Like files and media files, functions are stored in the adaptor:ex data folder
games in the respective game folder.
.js ) in your game's folder
functions containing your functions.
Functions within the
functionsfolder are registered in a maximum of one level directory depth. That means you can, for example, use subfolders to clone the functions of a git repository, but all
.jsfiles that export functions you may want to use, must be at the top level of this subfolder.
The files are loaded into your game as nodejs modules . So you need to make your functions available to adaptor:ex using
If you copy these lines into a new file with the
.js extension and move them to the functions folder in your game, you can access two new functions "reverse" and "playerCount" in your game.
You can add as many functions as you want in one file or split the functions across multiple files. The file names are arbitrary and play no role.
You can use functions both in the independent Function action and in all form fields that allow variables as input.
The Function action
With the logic function action you can call functions, use their return value and trigger
next states based on their output. Here you can find out more about the action: Function
Functions as Variables
Specify the function name enclosed in two square brackets (
]] ), prepended by
functions, in an action setting that accepts variable input.
The function name is supplemented by an open and a closed bracket
(). Arguments can also be specified here, separated by a comma (
, ), to pass them to the function.
Example of a function as a variable:
Within the function, "hello" and "world" are passed as an array :
The return value of the function is inserted at the point at which the function is placed.
You can also find all the functions of your game in the VARIABLES TOOLBAR from where you can drag them into the respective action.
Use the "playerCount" function created above in a Switch action.
If the number of items in the players collection is greater than 10, the
next state"Party" continues. Otherwise it continues with "NetflixAndChill".
To add external libraries that are not included with adaptor:ex, install them globally or in the
functions folder of your game so that they are placed inside a
node_modules folder in the
All the parameters passed by the level session are included in the first argument.
The second argument always contains variables and functions from the adaptor:ex game and session context.
args contains the transfer parameters specified in the level. A number of parameters must be passed as an array .
Note that "args" is always an array when the function is called as variable. Therefore it is recommended to always treat "args" as an array. This way you can use the function in any context.
context is an Object that contains
context.session contains functions and variables related to the session that called the function.
context.game contains functions and variables related to the game in which the session that called the function is running.
You also have access to the global variable
adaptor that contains functions and variables related to the adaptor:ex server hosting the game with the corresponding session.
We will try to explain the various functions and variables here as soon as possible.