OK, i decided to learn Python to use with Vox, i am more comfortable with Javascript and i really love it. Somethings in python let me crazy sometimes, so i decided to share here what i learn, when i get sometime free of course
Today i will try to explain how python treat Local and Global variables.
Dont worry about the previous explanations, the example will cover everything that you need.
- What is a Local Variable :
Well, like the name suggest, "local variables" can only be used inside the function that was created.
- What is a Global Variables:
You can call this variables from any place in the code.
Let see the examples and learn how we can change Global Variables inside a function.
#Global Variables
variable1 = "test1"
variable2 = "test2"
def changeVariables():
variable1 = "change1"
variable2 = "change2"
#Local Variable
variable3 = variable1+variable2
def changeVariablesGlobal():
global variable1
global variable2
variable1 = "change1"
variable2 = "change2"
Copy the code above and save in your "PY" folder, give the name TestVariables.py.
Now, see in the code that we have three variables. Two of them are declared outside the functions (variable1 and variable2), when we do that we are creating Global Variables and we can use this variables in any place of the code, see that i am using this variables in the function "changeVariables" to set the value of another variable.
The "variable3" otherwise its a Local Variable, was declared inside the function "changeVariables", so we can only use it inside this function, if you try to use this variable in the function "changeVariablesGlobal" you will get a error.
So far so good, but Python its a bad snake, we have to understand how to change the Global Variables now.
Lets put Vox to teach us. Copy the command bellow and paste in your VoxCommando, i think that you already now how to do that and i believe that you already put your script the folder PY, if you need change the path.
<?xml version="1.0" encoding="utf-16"?>
<command id="1791" name="variables" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
<action>
<cmdType>PY.ExecFile</cmdType>
<cmdString>PY\TestVariables.py</cmdString>
<cmdRepeat>1</cmdRepeat>
</action>
<action>
<cmdType>PY.ExecString</cmdType>
<cmdString>changeVariables()</cmdString>
<cmdRepeat>0</cmdRepeat>
</action>
<action>
<cmdType>PY.ExecString</cmdType>
<cmdString>changeVariablesGlobal()</cmdString>
<cmdRepeat>1</cmdRepeat>
</action>
<action>
<cmdType>PY.ExecString</cmdType>
<cmdString>result = variable1 + " " + variable2</cmdString>
<cmdRepeat>1</cmdRepeat>
</action>
<action>
<cmdType>OSD.ShowText</cmdType>
<cmdString>{LastResult}&&3000</cmdString>
<cmdRepeat>1</cmdRepeat>
</action>
</command>
So, now that you have the command we need to edit it to make some tests. Set your command like the picture bellow:
Ok, let understand the actions.
We will block the actions 2 and 3 (see the picture above) for this test.
Now, when we run the command, the action 1 will run the script file, everything that is outside a function will run, in our case we have only this outside the function:
variable1 = "test1"
variable2 = "test2"
So when Vox run the script this will set our global variables.
In the action 4 we will set our result to be equal to the variable1 and variable2, in the action 5 we show the result, so Vox will show this to you:
Nice...now we know how to set and use Global Variables in Vox.
To next test we will set the command like this:
Ok...see that now we enable the action 2. When we ask Vox to run the "PY.ExecString" we are saying to Vox to only run a small peace of code, in our case this means that we will run the function "changeVariables()", see:
def changeVariables():
variable1 = "change1"
variable2 = "change2"
#Local Variable
variable3 = "test3"
When we do that, the script will not run anything more, this means that the Global Variables will not be set again in our case.
What will be the result when we run the command in Vox ? Will be the same, the snake bite us
In the function changeVariables() we are setting the values of the variable1 and the variable2, but when we do that we are not setting the value of the Global Variable, because Python will understand this like a Local Variable.
So, How we can change the value of a Global Variable ? Before change the value of any Global Variable (if we are inside the function) we have to tell Python that it is a Global Variable. See the function changeVariablesGlobal()
def changeVariablesGlobal():
global variable1
global variable2
variable1 = "change1"
variable2 = "change2"
See ? We have to use the "global variable1", this will tell for Python that we will change the value of the Global Variable.
Now set your command in Vox like the picture bellow:
Run the command and now we will have a new result:
Ok...we can now use Global Variables in all our scripts, once we set the value of a Global Variable we can get this value from any place in our code, even in a different script.
Hope that help
Clayton