How do you get a value back from Javascript code?

I tried both examples given:
var string = variables.get(“string”).getAsString();
setString(string,“test”)
and:
var string = variables.get(“string”).getAsString();
string.setString(“test”)

none works, it doesn’t change the value of the variable string

https://wiki.gdevelop.io/gdevelop5/events/js-code/#documentation

const myVar = runtimeScene.getVariables().get("MyVar"); 
const myVar2=runtimeScene.getVariables().get("MyVar2"); 
const currentValue = myVar.getAsNumber();
myVar.setNumber(currentValue + 1);
myVar2.setString("Hello, world");

No, your syntax seems to be quite different.
Const are for variables that can’t be changed. Var and Let are for ones that can. Let seems to be now preferable instead of var.

I’m really trying to understand here
what’s the difference between line 2 and line 3, when to use them?
what was wrong in my tries was just the declaration??

ha sorry I read your last message now.
So I just tried
let string = runtimeScene.getVariables().get(“string”);
string.setString(“success”);
and it works
so indeed it was the var/let
or maybe it was the get thing? i still don’t understand the difference between line 2 and line 3 of yours

In the example from the link, the 2nd line gets a variable while the 3rd gets the value.

1 Like