WorkAdventure Map Scripting Guide
WorkAdventure Map Scripting Guide
To overcome the scripting restrictions of sandboxing in an iFrame within WorkAdventure, a developer must configure appropriate headers such as Access-Control-Allow-Origin to permit resource sharing and interaction . Additionally, the developer should enable communication across WorkAdventure's API by setting the openWebsiteAllowApi property to 'true' and ensure that the necessary client API library is imported into the iFrame . These steps create a bridge enabling functionality while preserving security, allowing for interaction with WorkAdventure's ecosystem through methods provided by the WA object.
The WA.chat.sendChatMessage function allows developers to programmatically send messages to the chat in WorkAdventure. To use this function, scripts must be executed within an environment that has access to the WorkAdventure API—such as in an iFrame that has been set to allow API interactions . Developers need to manage the sandboxing and CORS restrictions by setting appropriate headers and ensuring communication between the iFrame and main application is established . Consequently, the function provides a way to enhance user communication and engagement by dynamically sending messages as part of event handling or interactions within the virtual environment.
Scripts in WorkAdventure can be implemented in two main ways: inside the map or in an iFrame. When implementing a script inside a map, the script is directly referenced by including a JavaScript file within the map's properties, which can use relative URLs if the script file is close to the map . On the other hand, implementing a script in an iFrame involves adding a script in a separate HTML page that allows importing the WorkAdventure client API library, enabling interactions facilitated by the library . The key difference is that map-based scripts are confined to the specific map context, whereas iFrame scripts can expand functionality through additional web interfaces.
The iFrame in WorkAdventure is sandboxed primarily for security reasons; it prevents potentially malicious scripts from affecting users outside the intended scope. This sandboxing means that scripts within the iFrame have restrictions on their execution environment, including a lack of origin, which prevents them from initiating actions that affect other users directly . Consequently, operations such as altering the global state or accessing external resources must explicitly relax these restrictions through headers like Access-Control-Allow-Origin . While this ensures safety and integrity, it also limits the ease with which different parts of the application can interact.
Scripts in WorkAdventure run only in the browser of the current user, meaning they cannot trigger changes that affect multiple users' screens at the same time . This limitation implies that shared experiences or synchronized actions across users must be managed differently, often requiring server-side orchestration or API-based communication to achieve uniformity. As a result, developers may face challenges in creating consistent multi-user experiences using only client-side scripting, possibly affecting user experience by causing discrepancies in behavior and interaction across different client sessions.
The Map Scripting API in WorkAdventure supports the creation of First Time User Experiences (FTUE) by allowing developers to script initial interactions that introduce new users to the environment through guided actions. This involves setting up scripts that trigger welcoming pop-ups or tutorial steps when a user enters the map for the first time. Such scripts can present crucial information or navigation tips, ensuring users engage effectively from the onset, reducing confusion and improving user retention . By using specific map properties and scripting intelligence, developers can craft personalized introductory flows that adapt to user actions.
To enable communication between an iFrame and WorkAdventure for sending a message to a chat using TypeScript, the developer must first set the openWebsiteAllowApi property to 'true' in the iFrame. This allows the iFrame to interact with WorkAdventure's API. The developer then needs to import the WorkAdventure client API JavaScript library from 'https://play.workadventu.re/iframe_api.js' into the iFrame . With these settings, the developer can now use the WA.chat.sendChatMessage method to send a message to the chat .
By allowing TypeScript in its scripting API, WorkAdventure enhances the development process by providing strong typing, which improves code quality and reduces runtime errors through compile-time checks. TypeScript also offers better tooling support, enabling features like autocompletion, refactoring, and clearer documentation for developers using the WorkAdventure scripting environment . This feature is particularly beneficial in complex projects, as it facilitates maintainability and scalability of code bases.
Special zones in map scripting with WorkAdventure are defined areas within a map that trigger specific interactions when users interact with them. These zones can enhance user interaction by providing interactive elements such as pop-up notifications, user engagements with non-playing characters (NPCs), or linking actions that influence other features like eCommerce transactions when users step into these areas . By using map properties and scripting, developers can create enriched scenarios offering dynamic content delivery and real-time engagement tailored to user behaviors.
Including non-playing characters (NPCs) through scripting in WorkAdventure can significantly enhance user engagement by providing interactive and dynamic content within the map environment. NPCs can serve as guides, narrators, or quest providers, offering users a sense of purpose and direction in exploring the environment . This feature can lead to more immersive and personalized experiences, encouraging prolonged participation and repeat visits. Moreover, NPCs can be scripted to interact contextually with users, responding to their actions or choices, thereby fostering an environment of exploration and discovery that captivates users and deepens their interaction with the virtual world.