Javascript Functions
Javascript functions extend the possibilities of an adaptor:ex game by the complete range of the script programming language Javascript. Some functions are already included in the adaptor:ex server installation. It is also possible to create and integrate your own functions.
Add Functions
Like files and media files, functions are stored in the adaptor:ex data folder games
in the respective game folder.
Create one or more javascript files ( .js
) in your game's folder functions
containing your functions.
data
├── games
| ├── Playground
│ | ├── files
│ | ├── functions
│ | | ├── myfunctions.js
├── log
├── nedb
├── config.json
Functions within the
functions
folder 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.js
files 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 module.exports
async function playerCount(args, {session, game}) {
all_player = await game.db.players.find({})
return all_player.length
}
function reverse(args, {session, game}) {
let reversed = args[0].split("").reverse().join("")
return reversed
}
module.exports = {
playerCount:playerCount,
reverse:reverse
}
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.
Call Functions
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 ( [[
and ]]
), 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:
[[functions.test("hello","world")]]
Within the function, "hello" and "world" are passed as an array :
function test(args, {session, game}) {
session.log("1. " + args[0]) // 1. hello
session.log("2. " args[1]) // 2. world
}
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.
example
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".
Write functions
This chapter is far from complete. Contact us if you have any questions at tech@machinaex.de or on the machina commons discord server
You can use all functions of the Nodejs Javascript Runtime.
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 functions
directory.
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.
async function playerCount(args, context) {
all_player = await context.game.db.players.find({})
return all_player.length
}
function reverse(args, {session, game}) {
let reversed = args[0].split("").reverse().join("")
return reversed
}
module.exports = {
playerCount:playerCount,
reverse:reverse
}
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 session
and game
.
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.