InteropCall
Overview
Section titled “Overview”Calls a JavaScript function through the JS Interop API, providing good performance for C++ to JavaScript communication. This method is optimized for high-frequency calls and accepts a string argument to pass data to the JavaScript function.
Syntax
Section titled “Syntax”void InteropCall( PrismaView view, const char* functionName, const char* argument);Parameters
Section titled “Parameters”view (required)
Section titled “view (required)”- Type:
PrismaView - Description: The
PrismaViewinstance in which the function will be called
functionName (required)
Section titled “functionName (required)”- Type:
const char* - Description: The name of the JavaScript function to call
- Note: The function must be globally accessible (e.g., attached to
window)
argument (required)
Section titled “argument (required)”- Type:
const char* - Description: A string argument to pass to the JavaScript function
- Note: Only accepts string data - use JSON for complex data structures
Return Value
Section titled “Return Value”This method returns void. Unlike Invoke, InteropCall does not support return value callbacks for maximum performance.
Usage Examples
Section titled “Usage Examples”// Simple function call with string argumentPrismaUI->InteropCall(view, "updatePlayerName", "Dragonborn");// JavaScript function to receive the callwindow.updatePlayerName = (playerName) => { document.getElementById('player-name').textContent = playerName;};// Performance-critical updates (e.g., health bar)void UpdateHealthBar() { int currentHealth = GetPlayerHealth(); std::string healthStr = std::to_string(currentHealth);
// Fast update without callback overhead PrismaUI->InteropCall(view, "setHealthValue", healthStr.c_str());}
// Call this frequently in game loopvoid GameLoop() { UpdateHealthBar(); // Called every frame}// JavaScript - optimized for frequent updateswindow.setHealthValue = (health) => { // Direct DOM manipulation for good performance const healthBar = document.getElementById('health-bar'); healthBar.style.width = health + '%';};#include <nlohmann/json.hpp>using JSON = nlohmann::json;
// Send complex data as JSON stringvoid SendPlayerStats() { JSON playerStats = { {"health", GetPlayerHealth()}, {"magicka", GetPlayerMagicka()}, {"stamina", GetPlayerStamina()}, {"level", GetPlayerLevel()} };
// Convert to string and send via InteropCall std::string jsonStr = playerStats.dump(); PrismaUI->InteropCall(view, "updatePlayerStats", jsonStr.c_str());}// JavaScript - parse JSON datawindow.updatePlayerStats = (jsonData) => { const stats = JSON.parse(jsonData);
// Update UI elements document.getElementById('health').textContent = stats.health; document.getElementById('magicka').textContent = stats.magicka; document.getElementById('stamina').textContent = stats.stamina; document.getElementById('level').textContent = stats.level;};Performance Comparison
Section titled “Performance Comparison”| Method | Performance | Return Values | Use Case |
|---|---|---|---|
InteropCall | Highest | No | Frequent updates, real-time data |
Invoke | Standard | Yes | Complex operations, one-time calls |
Best Practices
Section titled “Best Practices”- Performance Critical: Use for high-frequency calls and real-time updates
- String Arguments: Always pass data as strings - use JSON for complex structures
- No Return Values: Don’t expect return values - use
RegisterJSListenerfor bidirectional communication - Function Availability: Ensure JavaScript functions are globally accessible
Common Issues
Section titled “Common Issues”Migration from Invoke
Section titled “Migration from Invoke”If you’re converting from Invoke to InteropCall for performance:
// Before (using Invoke)PrismaUI->Invoke(view, "updateHealth(100)");
// After (using InteropCall)PrismaUI->InteropCall(view, "updateHealth", "100");// JavaScript function needs to accept argument// Beforewindow.updateHealth = (healthValue) => { // Function was called with embedded argument};
// Afterwindow.updateHealth = (healthValue) => { // InteropCall sends only string argument const transformedValue = Number(healthValue);
// Function receives argument as parameter};