Fun basics

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

Fun basics

#1

Post by Lobby »

Fun is really powerful but complex at the same time. It allows you to write plugins that reacts to user input, build things, destroy things, play sound and much more. See here for a basic fun example.

This thread summarizes attributes that are needed for fun.

Fun attribute
First of all, a fun array can only be defined within a context. That means you can only define and use it from within a building or road plugin. To define it, you there are different attributes which decide on when the fun is evaluated:
  • "fun" - Will be evaluated for each building/road that's placed on the map on a daily basis.
  • "on click fun" - Will be evaluated if the user taps on a building./road
  • "random fun" - Will be evaluated daily, regardless of whether there are real instanced of this building/road. The context (that means x,y) will be chosen randomly. Might be useful to spawn stuff from nowhere.
A fun array is an array of so called transitions. Example:

Code: Select all

"fun":[
  // Transitions
]
Transitions
A transition is an object that consists of a condition and actions that might be executed if the condition is true:

Code: Select all

"fun":[
  {  // A transition object
    "condition": ...,  // A condition that will be evaluated
    "actions":[...]  // The action list that might be executed if the condition is true
  },
  ...  // More transitions
]
Possible attributes of a transition object are:
  • "condition" - A condition object that can be evaluated to true or false. To learn more about conditions, see here. If this tag isn't defined the transition will act as there's a condition that is always true.
  • "actions" - An array of action objects that may be executed if the condition is true. To learn more about actions, see here.
  • "p" - A floating point number from 0 to 1. A probability to actually execute the actions if the condition is true. Is 1 by default.
  • "double check" - If true, the condition will be checked again before action execution. For performance reasons, condition evaluation is decoupled from action execution by default. So this attribute is false by default. Only change this if you know what you're doing.
Execution behavior
If there is only one transition for a fun object execution is trivial: If the condition is true, the action list will be executed with probability "p". However, if there are multiple transitions, things begin to become a bit complicated. First of all, only one action list can be executed at a time. So if multiple transitions have a condition that is true, the game will select one of them randomly (using the probability "p" of each transition) to execute it's action list. The sum of all probabilities determines if a actions list will be executed at all. If the sum is >=1 an action array will be executed in any case.

User avatar
22Alpha
Inhabitant of a Conurbation
Posts: 448
Joined: 23 Sep 2017, 14:29

Platform

Re: Fun basics

#2

Post by 22Alpha »

What if your building is an even tile(2x2), where does the 0 start?

User avatar
CommanderABab
AB
Posts: 11185
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Fun basics

#3

Post by CommanderABab »

22Alpha wrote:
28 Jan 2018, 06:44
What if your building is an even tile(2x2), where does the 0 start?
Screenshot_20180128-002003.jpg

User avatar
22Alpha
Inhabitant of a Conurbation
Posts: 448
Joined: 23 Sep 2017, 14:29

Platform

Re: Fun basics

#4

Post by 22Alpha »

Then, does that mean next to x:1 still x:2 and above x2 is x:2,y:2?

User avatar
CommanderABab
AB
Posts: 11185
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Fun basics

#5

Post by CommanderABab »

Screenshot_20180128-043738.jpg

User avatar
22Alpha
Inhabitant of a Conurbation
Posts: 448
Joined: 23 Sep 2017, 14:29

Platform

Re: Fun basics

#6

Post by 22Alpha »

Thanks! Big help.

User avatar
22Alpha
Inhabitant of a Conurbation
Posts: 448
Joined: 23 Sep 2017, 14:29

Platform

Re: Fun basics

#7

Post by 22Alpha »

What is "always":true and "ignore success":true? And do I need to put "double check":true on every set of condition and action sets?

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

Platform

Re: Fun basics

#8

Post by Lobby »

Code: Select all

"always":true
Normally, only one transition is executed at the same time, although the conditions of multiple transitions are true. The transaction that will finally be executed is determined by the given probabilities of each transition (1 by default). A transition that contains "always":true will always be executed once it's condition is true. You usually use it if you have multiple transitions and don't want any randomness in execution.

Code: Select all

"ignore":true
Typically, you provide a list of action objects that should be executed once the corresponding condition is true. But what happens if an action cannot be succeeded (e.g. if you try to build a regular building on water)? By default, execution of the action list is aborted once the execution of an action failed. By adding "ignore":true into an action object you tell the game that it shouldn't abort execution if this action fails. Whether you want this behavior depends on what you want to achieve.

Code: Select all

"double check":true
For performance reasons conditions are evaluated all at once. After that, all considered actions are executed at once. The issue is that the action of a plugin may violate the condition of another plugin which would then be executed although it's condition isn't true anymore. Sometimes this isn't an issue, but if it is, just add "double check":true to your transition to ensure that the condition is checked a second time right before action execution. An example where you need this are animals: You don't want them to vanish by running into each other. Without "double check":true they can run into each other although their condition checks for that case.

User avatar
Zilla_682
Settler
Posts: 3
Joined: 27 Apr 2020, 10:29
Plugins: Showcase Store

Re: Fun basics

#9

Post by Zilla_682 »

How do I create a plugin? How do I create the texture for my building? How?????

User avatar
1Code
Inhabitant of a Megacity
Posts: 302
Joined: 30 Jan 2020, 16:56
Location: https://bit.ly/3P5dhnT
Plugins: Showcase Store

Plugin Creator

Platform

Re: Fun basics

#10

Post by 1Code »

Zilla_682 wrote:
30 Apr 2020, 15:40
How do I create a plugin? How do I create the texture for my building? How?????
viewtopic.php?f=41&t=2965

Post Reply Previous topicNext topic

Return to “Fun attribute”