## Variables

Any information about the fun attribute is given here.

Moderator: Plugin Moderators

Lobby
Developer
Reactions:
Posts: 3650
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

### Variables

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"} ] This code updates the value of the variable$vars_switch_myvar to 1-$vars_switch_myvar. So calling this assignment multiple times would lead to a value sequence of 0,1,0,1,0,1,... (value of not assigned variables is 0). 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 } Sample I'll use variable$vars_switch_myvar to implement the following: I want to have a switch that can be switched by the user
switch.png (1.25 KiB) Viewed 9523 times
Then there should be indicators that show the current state of the switch
light.png (1.18 KiB) Viewed 9523 times
The code for it would then be

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 } ] } ] Variable naming 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. Attachments vars.zip (3.1 KiB) Downloaded 138 times vars.json (866 Bytes) Downloaded 122 times =^._.^= ∫ Dominykas Developer Reactions: Posts: 3179 Joined: Sun Jul 23, 2017 12:45 Location: Easter Island Plugins: Show #### Platform ### Re: Variables This should work in theory? Code: Select all "random fun": [{ "condition": { "type":"upgrade", "id":"dsahqupAR" }, "actions": [{ "type":"set", "id":"dsavar", "code":"1" }] }], "requirements": [{ "type":"CONDITION", "text":"Upgrade Required", "condition":{"type":"value","code":"dsavar","z":1} }], Bearbear76 Former Bearbear65 Reactions: Posts: 5453 Joined: Fri Feb 10, 2017 14:53 Plugins: Show Version: Beta #### Platform #### Plugin Creator ### Re: Variables When can it start doing math? Just my opinion I'm probably recompiling my kernel... :o Lobby Developer Reactions: Posts: 3650 Joined: Sun Oct 26, 2008 12:34 Plugins: Show Version: Beta #### Platform ### Re: Variables @JustAnyone doesn't it work? @Bearbear65 actually, there's a bunch of integer operations supported within code. It's just bad documented... =^._.^= ∫ Dominykas Developer Reactions: Posts: 3179 Joined: Sun Jul 23, 2017 12:45 Location: Easter Island Plugins: Show #### Platform ### Re: Variables Lobby wrote: Sun Feb 18, 2018 2:47 @JustAnyone doesn't it work? Sorry! I've already fixed that. Forgot to mention it. Lobby Developer Reactions: Posts: 3650 Joined: Sun Oct 26, 2008 12:34 Plugins: Show Version: Beta #### Platform ### Re: Variables So the math here is incrementing a counter? The example above contains something similar Code: Select all "actions":[ {"type":"set","id":"$vars_switch_myvar","code":"1-$vars_switch_myvar"} ] To just increment you could write Code: Select all "actions":[ {"type":"set","id":"$other_var","code":"1+$other_var"} //$other_var := 1 + \$other_var
]
=^._.^= ∫

Lobby
Developer
Reactions:
Posts: 3650
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

### Re: Variables

Supported operators are right now:

Code: Select all

a+b  // Sum of a and b
a-b  // Subtracts b from a
a*b  // Multiplies a by b
a/b  // Divides a by b
a%b  // Modulus operator on a and b (see https://en.wikipedia.org/wiki/Modular_arithmetic)
a^b  //Binary XOR operation on a and b (see https://en.wikipedia.org/wiki/Exclusive_or)
Special variables that are accessible in code:

Code: Select all

x  // Contains x value of the current context
y  // Contains y value of the current context
You can combine these operations, but you have to pay attention on their order as they'll always be executed from left to right (like simple calculators do). Here a condition that is only true for tiles that lie on a checkerboard like pattern

Code: Select all

{"type":"value","code":"x+y%2","z":0}  // (x+y)%2 can only be 0 or 1
=^._.^= ∫

Lobby
Developer
Reactions:
Posts: 3650
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

### Re: Variables

Try to replace . by _
I think valid variable names must not contain .
=^._.^= ∫

KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2128
Joined: Thu Jul 07, 2016 22:50
Location: 'Merica
Plugins: Show
Version: Beta
Contact:

### Re: Variables

Is it possible to have a variable which can only be read and changed by one building?

Like if I had a building like "theo.kt101" and I wanted to store the value "happiness" in each one, but each building placed will have their own "happiness" value that isn't global.

Lobby
Developer
Reactions:
Posts: 3650
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

### Re: Variables

The problem with fun is that it has quite simple functionality and adding special cases for each potential use case doesn't look like a real solution.

That's why I added support for Lua
The function to access the storage of a building is Tile.getBuildingStorage
If you need help with that please don't hesitate to send me a pm or make an own topic for it so that others can learn from it, too.
=^._.^= ∫

KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2128
Joined: Thu Jul 07, 2016 22:50
Location: 'Merica
Plugins: Show
Version: Beta
Contact:

### Re: Variables

Are we able to use less than and more than with variables? (< >)

Lobby
Developer
Reactions:
Posts: 3650
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

### Re: Variables

Yes, you can just write something like

Code: Select all

"condition":"x>0"
or even

Code: Select all

"condition":"y/2<=x+1"

Supported comparison operators are

Code: Select all

<=
>=
==
<
>
=^._.^= ∫