This guide explains how to integrate and control the Lanista Unity WebGL build through JavaScript API calls.
The WebGL build exposes JavaScript functions that allow external applications to control the game flow and provide match data:
Core Functions:
SetMode(mode)- Configure the operation modeSetMatchId(matchId)- Set the match ID for external API callsLoadJsonGameData(jsonData)- Load match data directly as JSON
Interactive Functions:
ThrowObjectToPlayer1(objectIndex)- Throw an object at Player 1ThrowObjectToPlayer2(objectIndex)- Throw an object at Player 2LoadPlayer1IconUrl(url)- Load Player 1's icon from a URLLoadPlayer2IconUrl(url)- Load Player 2's icon from a URL
Callback Functions:
OnSimulationReady()- Called by Unity when the game is ready to receive data
You must call SetMode() first to configure how the game will receive match data.
SetMode(mode);Parameters:
mode(number): The operation mode0- External API Call Mode1- iFrame Mode2- API Simulation Mode (for testing)
Example:
// Use External API Call Mode
SetMode(0);
// Or use iFrame Mode
SetMode(1);In this mode, the Unity game will directly call the backend API to fetch match data.
Requirements:
- Call
SetMode(0)to activate External API Call mode - Must call
SetMatchId(matchId)to specify which match to fetch
Example:
// Set to External API Call mode
SetMode(0);
// Provide the match ID
SetMatchId("0469a41b-60ae-408a-b8ad-0e0f701398db");The game will:
- Wait for the match ID to be set
- Poll the API endpoint every second:
https://backend-production-9598.up.railway.app/api/combat/status?matchId={matchId} - Automatically stop polling when match status is "finished"
In this mode, your frontend application controls when match data is sent to the Unity game.
Requirements:
- Call
SetMode(1)to activate iFrame mode - Call
LoadJsonGameData(jsonData)whenever new match state data arrives
Example:
// Set to iFrame mode
SetMode(1);
// When you receive new match data from your backend
const matchData = {
match: {
player_1: { name: "Gladiator A" },
player_2: { name: "Gladiator B" },
status: "ongoing"
},
logs: [...]
};
// Send it to Unity as a JSON string
LoadJsonGameData(JSON.stringify(matchData));Important:
- You must send the data as a JSON string, not as a JavaScript object
- Call this function each time new match state data is received
- The game will process and visualize the match data in real-time
This mode is for testing purposes. It uses pre-loaded dummy data and simulates the match progression.
SetMode(2);The game will automatically play through the loaded match data with 1-second intervals between events.
Configures the operation mode of the game.
Parameters:
mode(number): 0 = External API, 1 = iFrame, 2 = Simulation
Returns: None
Sets the match ID for External API Call mode.
Parameters:
matchId(string): The unique identifier of the match to fetch
Returns: None
Required for: Mode 0 (External API Call Mode) only
Loads match data directly into the game.
Parameters:
jsonData(string): Match data as a JSON string
Returns: None
Required for: Mode 1 (iFrame Mode) - called on each state update
Example JSON Structure:
{
"match": {
"player_1": {
"name": "Player Name 1"
},
"player_2": {
"name": "Player Name 2"
},
"status": "ongoing"
},
"logs": []
}Throws an object at Player 1 during the match.
Parameters:
objectIndex(number): The index of the object to throw (0-based). Put 0 to throw tomatoe. There is only tomatoe for now.
Returns: None
Example:
// Throw object at index 0 to Player 1
ThrowObjectToPlayer1(0);Throws an object at Player 2 during the match.
Parameters:
objectIndex(number): The index of the object to throw (0-based). Put 0 to throw tomatoe. There is only tomatoe for now.
Returns: None
Example:
// Throw object at index 0 to Player 2
ThrowObjectToPlayer2(0);Loads Player 1's icon/avatar from a URL.
Parameters:
url(string): The URL of the image to load for Player 1
Returns: None
Example:
// Load Player 1's icon
LoadPlayer1IconUrl("https://example.com/player1-avatar.png");Loads Player 2's icon/avatar from a URL.
Parameters:
url(string): The URL of the image to load for Player 2
Returns: None
Example:
// Load Player 2's icon
LoadPlayer2IconUrl("https://example.com/player2-avatar.png");Callback function called by Unity when the game has fully loaded and is ready to receive match data.
Parameters: None
Returns: None
Called by: Unity game (not called by your application)
Use case: Use this callback to know when it's safe to call SetMode(), SetMatchId(), or LoadJsonGameData().
Example:
// This function will be automatically called by Unity when ready
function OnSimulationReady() {
console.log("Game is ready!");
// Now you can safely configure the game
SetMode(1);
LoadJsonGameData(yourMatchData);
}Note: This function is already defined in the HTML template. Unity will call it automatically when the game initialization is complete.
If you encounter any issues:
- Open Browser Console (F12 in most browsers)
- Look for messages prefixed with
[UNITY] - Common debug messages:
[UNITY] External API Call mode selected...- Mode 0 activated[UNITY] iFrame mode selected...- Mode 1 activated[UNITY] Waiting for match ID to be set...- SetMatchId() not called yet[UNITY] Match ID set to: {matchId}- Match ID successfully set[UNITY] Match loaded: {player1} vs {player2}- Data loaded successfully[UNITY] Error parsing Game Data JSON:- Invalid JSON format
// Wait for Unity to load
window.addEventListener('load', function() {
// Set mode to External API Call
SetMode(0);
// Set the match ID
SetMatchId("your-match-id-here");
// Unity will now automatically fetch and play the match
});// Set mode to iFrame
SetMode(1);
// Your application polls your backend
setInterval(async () => {
const response = await fetch('your-api-endpoint');
const matchData = await response.json();
// Send to Unity
LoadJsonGameData(JSON.stringify(matchData));
// Stop if match is finished
if (matchData.match.status === 'finished') {
clearInterval(this);
}
}, 1000);- Always call
SetMode()before any other function - The game instance must be fully loaded before making API calls
- Check the browser console for detailed logs and error messages
- In External API Call mode, the Unity game handles all API communication
- In iFrame mode, your application has full control over data flow