Variables

Any information about the fun attribute is given here.

Moderator: Plugin Moderators

User avatar
Lobby
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Variables

#1

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
switch.png (1.25 KiB) Viewed 24619 times
Then there should be indicators that show the current state of the switch :img
light.png
light.png (1.18 KiB) Viewed 24619 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.
Attachments
vars.zip
(3.1 KiB) Downloaded 612 times
vars.json
(866 Bytes) Downloaded 548 times

User avatar
JustAnyone
Developer
Posts: 3534
Joined: 23 Jul 2017, 12:45
Location: Easter Island
Plugins: Showcase Store

Platform

Re: Variables

#2

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}
			}],

User avatar
Bearbear76
Former Bearbear65
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Variables

#3

Post by Bearbear76 »

When can it start doing math? :bt

User avatar
Lobby
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#4

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
Developer
Posts: 3534
Joined: 23 Jul 2017, 12:45
Location: Easter Island
Plugins: Showcase Store

Platform

Re: Variables

#5

Post by JustAnyone »

Lobby wrote:
18 Feb 2018, 02:47
@JustAnyone doesn't it work?
Sorry! I've already fixed that. Forgot to mention it.

User avatar
Lobby
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#6

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
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#7

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
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#8

Post by Lobby »

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

User avatar
KINGTUT10101
1,000,000 inhabitants
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: Variables

#9

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
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#10

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
Posts: 2226
Joined: 07 Jul 2016, 22:50
Location: 'Merica
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: Variables

#11

Post by KINGTUT10101 »

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

User avatar
Lobby
Developer
Posts: 3713
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Variables

#12

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

<=
>=
==
<
>

Post Reply Previous topicNext topic

Return to “Fun attribute”