How to make this javascript code work?

I can’t figure out. I replaced let by var and const, tried different formulae but it returns nothing.

However if I replace line 20 with line 21 it works.

I don’t know if this is the only issue but you’re get the variable “string” but not it’s value. You can get the value with a 2nd variable or add getAsString to the line with the function call.

// for testing, the first example adds an x to the value, the 2nd an "o"

let myVar = runtimeScene.getVariables().get("myVarText");
let v = myVar.getAsString()
myVar.setString(v + "x");

let myVar2 = runtimeScene.getVariables().get("myVarText");
myVar.setString(myVar2.getAsString() + "o");

FYI, you can’t add .getAsString to the first line because you still need that to set the variable. That’s why you need either a 2nd variable or add .getAsString

Here’s a good post about variables. It’s an old post. From what I’ve read let is better than var

1 Like

thanks again. It works.
Yes I read this old post but nowhere is mentioned you can’t use getAsString on the first line. I would have never found this by myself. I really wonder how others figure these things out.

1 Like

A lot of trial and error. Some extensions are written in JavaScript. Studying them helps. Also, these links help.