Fun basics

Any information about the fun attribute is given here.

Moderator: Plugin Moderators

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

Fun basics

Post by Lobby » Fri Nov 17, 2017 18:04

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

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

Re: Fun basics

Post by 22Alpha » Sun Jan 28, 2018 6:44

What if your building is an even tile(2x2), where does the 0 start?
United we stand
Divided we fall

User avatar
CommanderABab
Inhabitant of a Multiverse
Reactions:
Posts: 6210
Joined: Tue Jun 07, 2016 21:12
Plugins: Show
Version: Beta

Re: Fun basics

Post by CommanderABab » Sun Jan 28, 2018 7:49

22Alpha wrote:
Sun Jan 28, 2018 6:44
What if your building is an even tile(2x2), where does the 0 start?
Screenshot_20180128-002003.jpg
512512Currently using 512. 512 What's new? :)
Nice homes!
:)

Telegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

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

Re: Fun basics

Post by 22Alpha » Sun Jan 28, 2018 8:44

Then, does that mean next to x:1 still x:2 and above x2 is x:2,y:2?
United we stand
Divided we fall

User avatar
CommanderABab
Inhabitant of a Multiverse
Reactions:
Posts: 6210
Joined: Tue Jun 07, 2016 21:12
Plugins: Show
Version: Beta

Re: Fun basics

Post by CommanderABab » Sun Jan 28, 2018 12:07

Screenshot_20180128-043738.jpg
512512Currently using 512. 512 What's new? :)
Nice homes!
:)

Telegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

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

Re: Fun basics

Post by 22Alpha » Sun Jan 28, 2018 12:34

Thanks! Big help.
United we stand
Divided we fall

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

Re: Fun basics

Post by 22Alpha » Wed Feb 21, 2018 14:52

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?
United we stand
Divided we fall

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

Re: Fun basics

Post by Lobby » Wed Feb 21, 2018 23:34

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.
...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