Page 1 of 1

Variables

Posted: Sat Jan 13, 2018 16:30
by Lobby


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 :img
switch.png
switch.png (1.25 KiB) Viewed 2688 times
Then there should be indicators that show the current state of the switch :img
light.png
light.png (1.18 KiB) Viewed 2688 times
The code for it would then be :json

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.

Re: Variables

Posted: Sun Jan 28, 2018 11:58
by JustAnyone
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}
			}],

Re: Variables

Posted: Mon Feb 12, 2018 4:18
by Bearbear65
When can it start doing math? :bt

Re: Variables

Posted: Sun Feb 18, 2018 2:47
by Lobby
@JustAnyone doesn't it work?
@Bearbear65 actually, there's a bunch of integer operations supported within code. It's just bad documented...

Re: Variables

Posted: Sun Feb 18, 2018 3:07
by JustAnyone
Lobby wrote:
Sun Feb 18, 2018 2:47
@JustAnyone doesn't it work?
Sorry! I've already fixed that. Forgot to mention it.

Re: Variables

Posted: Sun Feb 18, 2018 9:51
by Lobby
So the math here is incrementing a counter? The example above contains something similar :json

Code: Select all

"actions":[
  {"type":"set","id":"$vars_switch_myvar","code":"1-$vars_switch_myvar"}
]
To just increment you could write :json

Code: Select all

"actions":[
  {"type":"set","id":"$other_var","code":"1+$other_var"} // $other_var := 1 + $other_var
]

Re: Variables

Posted: Sun Feb 18, 2018 10:08
by Lobby
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 :json

Code: Select all

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

Re: Variables

Posted: Sun Feb 18, 2018 10:20
by Lobby
Try to replace . by _
I think valid variable names must not contain .

Re: Variables

Posted: Wed Apr 03, 2019 14:14
by KINGTUT10101
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.

Re: Variables

Posted: Wed Apr 03, 2019 16:07
by Lobby
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 :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.

Re: Variables

Posted: Sat Jun 29, 2019 17:21
by KINGTUT10101
Are we able to use less than and more than with variables? (< >)

Re: Variables

Posted: Sat Jun 29, 2019 22:07
by Lobby
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

<=
>=
==
<
>