Problem with the extension variable

Hello, I’m facing an issue when creating an extension in GDevelop.
I’m making an extension that detects the user’s operating system.
This is the code I’m using:

let userAgent = navigator.userAgent || navigator.vendor || window.opera;
let osName = “Unknown”;

if (/android/i.test(userAgent)) {
osName = “Android”;
} else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
osName = “iOS”;
} else if (/Win/.test(userAgent)) {
osName = “Windows”;
} else if (/Mac/.test(userAgent)) {
osName = “MacOS”;
} else if (/Linux/.test(userAgent)) {
osName = “Linux”;
}

runtimeScene.getVariables().get(“OSName”).setString(osName);

The problem is with this line:

runtimeScene.getVariables().get(“OSName”).setString(osName);

Here, I want the value of osName to be stored in a variable defined by the extension itself.
In other words, I want the osName value to be stored in an extension-specific variable called OSName.

Open the extension’s tab at the top. In the functions tab (left side) there are two buttons you can use to set extension variables. “Extension Scene Variables” and “Extension Global Variables”

Hope this helps!

It would be easier and safer (as less risk that your extension no longer works with future GDevelop releases) to:

  • make an expression that returns the value
  • store this value in a variable with an event (instead of using JS)
1 Like