Variables

Any information about the fun attribute is given here.

Moderator: Plugin Moderators

User avatar
Lobby
Developer
Reactions:
Posts: 3171
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta
Phone model: S7 edge

Variables

Post by Lobby » Sat Jan 13, 2018 16:30



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 1135 times
Then there should be indicators that show the current state of the switch :img
light.png
light.png (1.18 KiB) Viewed 1135 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 32 times
vars.json
(866 Bytes) Downloaded 16 times
...Have a nice stay

User avatar
JustAnyone
Feature manager
Reactions:
Posts: 2427
Joined: Sun Jul 23, 2017 12:45
Location: Pickle command center
Plugins: Show
Version: Beta
Phone model: Picklephone S5

Re: Variables

Post by JustAnyone » Sun Jan 28, 2018 11:58

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
Bearbear65
Inhabitant of a Universe
Reactions:
Posts: 4565
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta
Phone model: Xperia™ XZ1 SO-01K

Re: Variables

Post by Bearbear65 » Mon Feb 12, 2018 4:18

When can it start doing math? :bt
ImageJust my opinion
Theotown forum authoritarian

User avatar
Lobby
Developer
Reactions:
Posts: 3171
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta
Phone model: S7 edge

Re: Variables

Post by Lobby » Sun Feb 18, 2018 2:47

@JustAnyone doesn't it work?
@Bearbear65 actually, there's a bunch of integer operations supported within code. It's just bad documented...
...Have a nice stay

User avatar
JustAnyone
Feature manager
Reactions:
Posts: 2427
Joined: Sun Jul 23, 2017 12:45
Location: Pickle command center
Plugins: Show
Version: Beta
Phone model: Picklephone S5

Re: Variables

Post by JustAnyone » Sun Feb 18, 2018 3:07

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

User avatar
Lobby
Developer
Reactions:
Posts: 3171
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta
Phone model: S7 edge

Re: Variables

Post by Lobby » Sun Feb 18, 2018 9:51

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
]
...Have a nice stay

User avatar
Lobby
Developer
Reactions:
Posts: 3171
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta
Phone model: S7 edge

Re: Variables

Post by Lobby » Sun Feb 18, 2018 10:08

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
...Have a nice stay

User avatar
Lobby
Developer
Reactions:
Posts: 3171
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta
Phone model: S7 edge

Re: Variables

Post by Lobby » Sun Feb 18, 2018 10:20

Try to replace . by _
I think valid variable names must not contain .
...Have a nice stay

Post Reply Previous topicNext topic

Return to “Fun attribute”

Who is online

Users browsing this forum: No registered users and 1 guest