Variables

Any information about the fun attribute is given here.
Previous topicNext topic
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Variables

Post 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
Then there should be indicators that show the current state of the switch :img
light.png
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.
You do not have the required permissions to view the files attached to this post.
User avatar
JustAnyone
Honorary Contributor
Reactions:
Posts: 3685
Joined: 23 Jul 2017, 10:45
Location: Easter Island
Plugins: Showcase Store

Platform

Re: Variables

Post 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}
			}],
:pickle:
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5711
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Variables

Post by Bearbear76 »

When can it start doing math? :bt
Just my opinion
retired
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post by Lobby »

@JustAnyone doesn't it work?
@Bearbear65 actually, there's a bunch of integer operations supported within code. It's just bad documented...
User avatar
JustAnyone
Honorary Contributor
Reactions:
Posts: 3685
Joined: 23 Jul 2017, 10:45
Location: Easter Island
Plugins: Showcase Store

Platform

Re: Variables

Post by JustAnyone »

Lobby wrote: 18 Feb 2018, 00:47 @JustAnyone doesn't it work?
Sorry! I've already fixed that. Forgot to mention it.
:pickle:
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post 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
]
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post 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
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post by Lobby »

Try to replace . by _
I think valid variable names must not contain .
User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2228
Joined: 07 Jul 2016, 20:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Variables

Post 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.
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post 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.
User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2228
Joined: 07 Jul 2016, 20:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Variables

Post by KINGTUT10101 »

Are we able to use less than and more than with variables? (< >)
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post 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

<=
>=
==
<
>
User avatar
yinx
Small-town resident
Reactions:
Posts: 31
Joined: 11 May 2026, 01:58
Location: Ha Noi
Plugins: Showcase Store

Plugin Creator

Platform

Re: Variables

Post by yinx »

If I want to get the value of the variable daystofire and pass it into frames of type = date, how can I do that? I mean, I want the frames of the date to be set according to the daytofire variable
YinDreamX from Vietnam :fire
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3771
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Variables

Post by Lobby »

iirc you cannot do this with fun
User avatar
yinx
Small-town resident
Reactions:
Posts: 31
Joined: 11 May 2026, 01:58
Location: Ha Noi
Plugins: Showcase Store

Plugin Creator

Platform

Re: Variables

Post by yinx »

Thanks! I actually found a workaround by using merge to inject the JSON. The only downside is that the game won't update those variables on the fly—it requires a restart to take effect, which might be a bit annoying for players
YinDreamX from Vietnam :fire
Previous topicNext topic