Page 1 of 1

Fun basics

Posted: 17 Nov 2017, 18:04
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.

Re: Fun basics

Posted: 28 Jan 2018, 06:44
by 22Alpha
What if your building is an even tile(2x2), where does the 0 start?

Re: Fun basics

Posted: 28 Jan 2018, 07:49
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

Re: Fun basics

Posted: 28 Jan 2018, 08:44
by 22Alpha
Then, does that mean next to x:1 still x:2 and above x2 is x:2,y:2?

Re: Fun basics

Posted: 28 Jan 2018, 12:07
by CommanderABab
Screenshot_20180128-043738.jpg

Re: Fun basics

Posted: 28 Jan 2018, 12:34
by 22Alpha
Thanks! Big help.

Re: Fun basics

Posted: 21 Feb 2018, 14:52
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?

Re: Fun basics

Posted: 21 Feb 2018, 23:34
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.

Re: Fun basics

Posted: 30 Apr 2020, 15:40
by Zilla_682
How do I create a plugin? How do I create the texture for my building? How?????

Re: Fun basics

Posted: 07 Feb 2021, 15:56
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