Wouldn't it be useful to have some sort of state you can access from all of your plugins? That's what variables are for. They're stored globally for the whole city and can by accessed by any plugin. For now, variables can only hold integers. To access a variable you have to give it a name like $vars_switch_myvar. A great thing about variables is that they can be accessed from anywhere. Also, variables will be saved if the player leaves a city.
Set variable
Setting a variable is an action of type "set". The name of the variable is provided as "id". The value you want to assign can be provided with the "code"-tag which allows similar code as the "value" condition:
Code: Select all
"actions":[
{"type":"set","id":"$vars_switch_myvar","code":"1-$vars_switch_myvar"}
]
Read variable
The sample code for setting a variable already uses code to read it, too (by using it in the "code"-tag). However, variables can also accessed by using a condition of type "value". In the "code" string you can provide a simple code that may contain variable names:
Code: Select all
"condition":{ // This condition is only true if $vars_switch_myvar==1
"type":"value",
"code":"$vars_switch_myvar",
"z":1
}
I'll use variable $vars_switch_myvar to implement the following: I want to have a switch that can be switched by the user
![Image :img](./images/smilies/bear_img.png)
![Image :img](./images/smilies/bear_img.png)
![Json :json](./images/smilies/bear_json.png)
Code: Select all
[
{
"id":"$vars_switch00",
"type":"decoration",
"width":1,
"height":1,
"frames":[{"bmp":"switch.png","w":32,"h":16,"count":2}],
"selectable frames":false,
"random frame":false,
"on click fun":[
{
"actions":[
{"type":"set","id":"$vars_switch_myvar","code":"1-$vars_switch_myvar"}, // Switch state of variable between 0 and 1
{"type":"frame","code":"$vars_switch_myvar"} // Set frame accordingly
]
}
],
"fun":[
{
"actions":[{"type":"frame","code":"$vars_switch_myvar"}] // Update frame on a daily basis (necessary if multiple switches exist)
}
]
},
{
"id":"$vars_light00",
"type":"decoration",
"width":1,
"height":1,
"frames":[{"bmp":"light.png","w":32,"h":16,"count":2}],
"selectable frames":false,
"random frame":false,
"fun":[
{
"actions":[{"type":"frame","code":"$vars_switch_myvar"}] // Update frame
}
]
}
]
Although you're pretty free in how you name your variables I recommend that it always starts with $ (variables without $ are supposed to be virtual). It also should contain name identifiers for you/your plugin to make name collisions with other plugins more unlikely.