Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ XR Studio

Use XRS Script classes from XML Script file

  • Last UpdatedApr 24, 2024
  • 4 minute read

Only public methods can be accessed from XML Script files and they can be called using:

  • execute, and setting the parameter script with the fully qualified name of the method, following the syntax: namespaceName::className.methodName

  • route.

Call static XRS class methods from XML script

The following code shows an example of XRS static class methods definition.

namespace XR

{

class TestClass

{

static void testCommandCmd()

{

Runtime::Command.set(name:"testVar.value", value:"hello world", resolve:"true");

Runtime::Command.createnode(name:"CAM_PROVA", nodeType:"Camera");

Runtime::Environment.Print(Runtime::Scene.GetFieldValue("CAM_PROVA.type") + " " + Runtime::Scene.GetFieldValue("testVar.value"));

}

static void testFromCommand(int v)

{

Runtime::Environment.Print("testFromCommand! "+v);

}

static void testFromCommand2(string s, int a)

{

Runtime::Environment.Print("testFromCommand2! "+s+" "+a);

}

static int sumValues(int a, int b, int c)

{

int sum=a + b + c;

Runtime::Environment.Print(sum);

return sum;

}

}

}

The following code shows an example of how to call XRS static class methods from XML.

<Var name="testVar" type="sstring" value="" />

<Keysensor key="X" name="X" />

<route from="X.keyDown" to="test_cmd.execute"/>

<Command name="test_cmd">

<execute script="XR::FirstClass.testCommandCmd"/>

<execute script="XR::FirstClass.testFromCommand" v="12"/>

<execute script="XR::FirstClass.sumValues" a="3" b="5" c="1"/>

<execute script="XR::FirstClass.testFromCommand2" s="@hello@" a="13" resolve="false"/>

</Command>

//Output

//[INFO] [XRS] target hello world

//[INFO] [XRS] testFromCommand! 12

//[INFO] [XRS] 9

//[INFO] [XRS] testFromCommand2! @hello@ 13

<Keysensor key="L" name="L" />

<route from="L.keyDown" to="XR::FirstClass.testCommandCmd"/>

//Output

//[INFO] [XRS] target hello world

<Keysensor key="Z" name="Z" />

<route from="Z.keyDown" to="XR::FirstClass.testFromCommand" value="21"/>

//Output

//[INFO] [XRS] testFromCommand! 21

  • Execute statement allows you to specify a list of input parameters for the XRS method using the syntax inputParametername:”inputParameterValue”, whereas route supports only methods with 0 or 1 parameters.

  • Execute statement also supports “resolve”: if set to true, the values to be assigned to the method input parameters are resolved by the XML script string resolver before setting them; if set to false, the values are not resolved and always assigned as string literals. Resolve is true by default.

  • Async methods can be called from XML script using a route only.

  • Return value of an XRS class method is not currently accessible from XML script.

Call XRS object instance methods from XML script

Non-static class variables or methods are available to XML script only through the ScriptObject node and its fields.

The following code shows an example of XRS class with instance methods definition.

namespace XR

{

class TestClass

{

void test()

{

two();

three();

}

void two()

{

Runtime::Environment.Print("two");

}

private void three()

{

Runtime::Environment.Print("three");

}

}

}

The following code shows an example of how to instance an XRS class object and invoke its methods.

<ScriptObject name="testObject" class="XR::TestClass" />

<Keysensor key="X" name="X" />

<route from="X.keyDown" to="test_cmd.execute"/>

<Command name="test_cmd">

<execute script="testObject.test"/>

</Command>

//Output

//[INFO] [XRS] two

//[INFO] [XRS] three

Keysensor key="L" name="L" />

<route from="L.keyDown" to="testObject.test"/>

//Output

//[INFO] [XRS] two

//[INFO] [XRS] three

Defined classes

Any defined class in included XRS scripts corresponds to a ScriptClass node in the global context and can be displayed in the Debug Panel under Nodes > global > xrs > classes.

Methods and variables defined as static and public are displayed as fields of the ScriptClass node.

Methods correspond to fields of type sscriptmethod and variables to fields of type sscriptvariable.

Instanced nodes

Instanced ScriptObject nodes, which represent an XRS class object instance, are listed in the debug panel under the graphic context where instancing was declared.

Methods and variables defined as public that are not static are displayed as fields of the ScriptObject node.

Methods correspond to fields of type sscriptmethod and variables to fields of type sscriptvariable.

In both cases, such as ScriptClass and ScriptObject, when a sscriptmethod or sscriptvariable field is selected, additional information on the signature are displayed in the bottom panel: full name and method signature with the input parameter name and type for the SScriptMethods; full name and variable type for the SScriptVariables.

  • For SScriptMethods it is possible to insert a value in the input field and press Set button to call the methods. Only 0 or 1 input parameters are currently supported.

  • When an SScriptVariable is valorized from XML Script or Debug panel, it passes through a string conversion step, which does not happen when a variable is valorized directly from XRS Script side.

In This Topic
TitleResults for “How to create a CRG?”Also Available in