how to set variable scope,
Variable scopes are discussed extensively in our documentation here.
Setting a specific variable can be done with the SetVariable RPC. This RPC shows you need to refer to a specific elementInstanceKey and specify whether or not to use the local scope or have the variable propagated to the first parent scope that has the variable already defined (or else the root scope).
To make sure a variable exists at a certain scope can be achieved through variable input mappings.
Please let me know whether this helps and whether you have any further questions.
thank you very much