This feature is "experimental". We may apply changes in the near future to the way it works when we gather some feedback.
Scripting WorkAdventure maps
Do you want to add a bit of intelligence to your map? Scripts allow you to create maps with special features.
You can for instance:
- Create FTUE (First Time User Experience) scenarios where a first-time user will be displayed a notification popup.
- Create NPC (non playing characters) and interact with those characters using the chat.
- Organize interactions between an iframe and your map (for instance, walking on a special zone might add a product in the cart of an eCommerce website...)
Please note that scripting in WorkAdventure is at an early stage of development and that more features might be added in the future. You can actually voice your opinion about useful features by adding an issue on Github.
Beware: Scripts are executed in the browser of the current user only. Generally speaking, scripts cannot be used to trigger a change that will be displayed on other users screen.
There are 2 ways you can use the scripting language:
scriptproperty of your map.
Adding a script in the map
script property in your map.
In Tiled, in order to access your map properties, you can click on "Map > Map properties".
script property (a "string"), and put the URL of your script.
You can put relative URLs. If your script file is next to your map, you can simply write the name of the script file here.
Start by testing this with a simple message sent to the chat.
WA.sendChatMessage('Hello world', 'Mr Robot');
WA objects contains a number of useful methods enabling you to interact with the WorkAdventure game. For instance,
WA.sendChatMessage opens the chat and adds a message in it.
In your browser console, when you open the map, the chat message should be displayed right away.
Adding a script in an iFrame
In WorkAdventure, you can easily open an iFrame using the
openWebsite property on a layer. However, by default, the iFrame is not allowed to communicate with WorkAdventure.
This is done to improve security. In order to be able to execute a script that communicates with WorkAdventure inside an iFrame, you have to explicitly allow the iFrame to use the "iFrame API".
In order to allow communication with WorkAdventure, you need to add an additional property:
openWebsiteAllowApi. This property must be boolean and you must set it to "true".
WA object that you can use to communicate with WorkAdventure.
The library is available at
Note: if you are using a self-hosted version of WorkAdventure, use
<!doctype html> <html lang="en"> <head> <script src="https://play.workadventu.re/iframe_api.js"></script> </head> <body> </body> </html>
You can now start by testing this with a simple message sent to the chat.
... <script> WA.sendChatMessage('Hello world', 'Mr Robot'); </script> ...
Let's now review the complete list of methods available in this
View the dedicated page about using Typescript with the scripting API.
Available features in the client API
The list of available functions and features is available in the API Reference page, with examples.