Functionality using fun

Any information about the fun attribute is given here.

Moderator: KoalaGuy

User avatar
Lobby
Developer
Reactions:
Posts: 2318
Joined: Sun Oct 26, 2008 12:34

Functionality using fun

Post by Lobby » Wed Sep 13, 2017 12:26

Hi,

in this topic I'll try to give a step-by-step guide into fun functionality. Dependent on your current knowledge it might need some time getting used to it. However, fun is a real powerful tool to create buildings that react to their environment or to user interaction. By now (version 343) the system is for from completion. Nevertheless, functionality as it's described here should still work with future updates as new functionality will be added primarily.

1. Getting started
To keep it simple I'll just focus on the fun part of a sample plugin. For this reason I'll use frames which are built right into the game so you don't have to bother with them. However, you can make them as complex as usual for your projects. Also building size isn't limited by fun, but a size of width:1, height:1 makes it much easier.

Let this be our starting point of a simple plugin:

Code: Select all

[
  {
    "id":"$deco_fun_sample00",
    "type":"decoration",
    "width":1,
    "height":1,
    "frames":[{"x":288,"y":0,"w":32,"h":32,"count":2,"offset x":1024,"offset y":1024}]
  }
]
You don't have to worry about the frames definition, it just means that we load two frames of this image:
fun_frames.png
fun_frames.png (1.08 KiB) Viewed 465 times
So, now let's add some fun to it. To do so, we use the fun attribute:

Code: Select all

[
  {
    ... // Just as before
    "fun":[
      {
        "condition":{"type":"building","frame":0},
        "actions":[
          {"type":"frame","frame":1}
        ]
      }
    ]
  }
]
Try to guess what this code does.

Solution: If the building was placed with frame 0, it will eventually switch to frame 1.

Let's analyze "fun" a bit further. It takes an array of objects which we refer to as transitions (as they are similar to the transitions in a state machine). A transition has the form (simplified)

Code: Select all

if (condition) then do actions
The ability to provide multiple transitions allows us to perform different actions on different conditions. Details about when and which transition will be taken will follow later.

What does the condition

Code: Select all

{type":"building","frame":0}
exactly to? It checks whether there's a building at the current position (that is the position of the building in which we define fun) which also has set frame 0 as current frame. If this is true, the condition evaluates to true so the actions can be performed.

We provide a single action

Code: Select all

{"type":"frame","frame":1}
which tries to set the frame of the building at the current position (so of this building) to 1.

2. Multiple transitions
For now, it's boring if our plugin just switched frame 0 to 1. We want to switch back from 1 to 0 eventually. To do so, we can use the ability to provide multiple transitions:

Code: Select all

[
  {
    ... // As before
    "fun":[
      {
        "condition":{"type":"building","frame":0},
        "actions":[
          {"type":"frame","frame":1}
        ]
      },
      {
        "condition":{"type":"building","frame":1},
        "actions":[
          {"type":"frame","frame":0}
        ]
      }
    ]
  }
]
Simple, isn't it? But wait, you might say what if the frame is 0 the first transition's condition will be true so the first action will be performed. But after that, the condition of the second transition becomes true so that the frame will be set back to 0 immediately.

True, but that's not how transitions and their actions are executed. Instead, all conditions of all transitions are evaluated at the same time. After that, only one transition whose condition was true is selected to perform it's actions. If no condition was true, no actions will be performed. How often does this happen? For "fun" this process is performed daily. So the building will switch it's frame each day except you pause the game.

3. Tap sensitivity
You might say that we can achieve the exact same behavior by using an animation. That's true, and for this functionality I would also recommend to use animations instead as it's much easier. But what if we want our building to change it's frame only if the user taps on it? We cannot achieve this with animations alone, but with fun it's fairly easy: We just have to replace "fun" with "on click fun". This fun will be performed only if the user taps on the building.

So our final code for this looks like that:

Code: Select all

[
  {
    "id":"$deco_fun_sample00",
    "type":"decoration",
    "width":1,
    "height":1,
    "frames":[{"x":288,"y":0,"w":32,"h":32,"count":2,"offset x":1024,"offset y":1024}],
    "on click fun":[
      {
        "condition":{"type":"building","frame":0},
        "actions":[
          {"type":"frame","frame":1}
        ]
      },
      {
        "condition":{"type":"building","frame":1},
        "actions":[
          {"type":"frame","frame":0}
        ]
      }
    ]
  }
]
Note that you can define "fun" and "on click fun" both in a single plugin. So you can do both, daily transitions as well as touch sensitive transitions. This is used for example in these sample plugins:
Wandering Animals :plugin
Game of Life :plugin
...Have a nice stay

User avatar
Brody Craft
Inhabitant of a Universe
Reactions:
Posts: 4343
Joined: Tue Jan 24, 2017 11:15
Location: Messier - 42069
Contact:

Re: Fun

Post by Brody Craft » Wed Sep 13, 2017 12:33

This is very mind bending O_o
Quote Of The Day: N/A
President Of R.O.V.
CEO Of LobbyNet

User avatar
Lobby
Developer
Reactions:
Posts: 2318
Joined: Sun Oct 26, 2008 12:34

Re: Fun

Post by Lobby » Wed Sep 13, 2017 12:42

Any questions? I know that it's not easy.
...Have a nice stay

User avatar
Brody Craft
Inhabitant of a Universe
Reactions:
Posts: 4343
Joined: Tue Jan 24, 2017 11:15
Location: Messier - 42069
Contact:

Re: Fun

Post by Brody Craft » Wed Sep 13, 2017 12:43

No questions...
Quote Of The Day: N/A
President Of R.O.V.
CEO Of LobbyNet

User avatar
Bearbear65
Inhabitant of a Supercluster
Reactions:
Posts: 3697
Joined: Fri Feb 10, 2017 14:53
Location: Japan

Re: Fun

Post by Bearbear65 » Wed Sep 13, 2017 13:16

Brody Craft wrote:
Wed Sep 13, 2017 12:43
No questions...
Trust me this is hard ;)
But it feels so good when you finally get it right :lol: 8-)
ImageJust my opinion

User avatar
Josh
Graphic designer
Reactions:
Posts: 1909
Joined: Sat Mar 11, 2017 19:20

Re: Functionality using fun

Post by Josh » Wed Sep 13, 2017 15:52

You can make whole new systems with this :)
Proud TheoTown player sinds update 1.1.50
My plugins and collections!
Creator of Aldorria, Covinton Empire, West Country, Sunnydale
Save nature, before it kills you🌲

User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 1236
Joined: Thu Jul 07, 2016 22:50
Location: 'Merica
Contact:

Re: Functionality using fun

Post by KINGTUT10101 » Sun Sep 24, 2017 19:47

Could you help me figure out how to make a plug-in that will replace nearby buildings. Basically a building that will spread if it's next to other buildings.

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Fun

Post by KoalaGuy » Thu Oct 05, 2017 10:22

Lobby wrote:
Wed Sep 13, 2017 12:42
Any questions? I know that it's not easy.
A little help needed here.
I need a building1 to build building2 on itself if building2 is next to building1.

Code: Select all

        "condition":{"type":"and","x":1,"inner":[
          {"type":"id","$building2"}
But game shows an error because it excepts ":" after "$building2"
I also tried

Code: Select all

        "condition":{"type":"and","x":1,"inner":[
          {"id":"$building2"}
But there's an error because there's not a "type" definition.
Any ideas?
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
CommanderABab
Inhabitant of a Universe
Reactions:
Posts: 4204
Joined: Tue Jun 07, 2016 21:12

Re: Functionality using fun

Post by CommanderABab » Thu Oct 05, 2017 10:25

Build is in the actions.
378378Currently using beta 378. 378 What's new? Bus stop bug fixed! :)
ImageTelegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Thu Oct 05, 2017 10:27

CommanderABab wrote:
Thu Oct 05, 2017 10:25
Build is in the actions.
I know, I added it there but it needs to be activated when $building2 is next to it.

EDIT:

Code: Select all

        "condition":{"type":"and","x":1,"inner":[
          {"type":"id","$building2"}
        ]},
        "actions":[
          {"type":"remove"},
          {"type":"build","id":"$building2"}
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
Lobby
Developer
Reactions:
Posts: 2318
Joined: Sun Oct 26, 2008 12:34

Re: Functionality using fun

Post by Lobby » Thu Oct 05, 2017 10:36

The syntax isn't correct, in JSON objects consist of name:value-pairs while your "$building2" stands alone (how sad). To check for a building, use type:building and provide your id as value of id:value. See here:

Code: Select all

        "condition":{"type":"and","x":1,"inner":[
          {"type":"building","id":"$building2"}
        ]},
If you have only one condition you can simplify the code by removing the and-group:

Code: Select all

        "condition":{"type":"building","id":"$building2"},
...Have a nice stay

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Thu Oct 05, 2017 10:41

Ok, thanks! :)
It would be useful if you could provide some conditions and actions in this topic, since only changing frames is shown here :teach
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Fri Oct 06, 2017 15:00

@Lobby
What would be the best way to make a condition that would be fulfilled if there's no $building2 around $building1 4 tiles in any direction?
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
Lobby
Developer
Reactions:
Posts: 2318
Joined: Sun Oct 26, 2008 12:34

Re: Functionality using fun

Post by Lobby » Fri Oct 06, 2017 15:26

4 tiles into each direction? Do you mean like the red tiles here?:
4tiles.png
...Have a nice stay

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Fri Oct 06, 2017 15:32

Yes.
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
Lobby
Developer
Reactions:
Posts: 2318
Joined: Sun Oct 26, 2008 12:34

Re: Functionality using fun

Post by Lobby » Fri Oct 06, 2017 15:36

I would use "count" to do that. count evaluates all inner conditions and counts how many of them hold true. Let's say it found n true conditions. The count condition is then true iff min≤n≤max or n=z (depending on whether you provided min, max or z).

Code: Select all

"condition":{"type":"count","z":0,"inner":[
  {"type":"building","id":"$building2","x":1},
  {"type":"building","id":"$building2","x":2},
  {"type":"building","id":"$building2","x":3},
  {"type":"building","id":"$building2","x":4},
  {"type":"building","id":"$building2","y":1},
  {"type":"building","id":"$building2","y":2},
  {"type":"building","id":"$building2","y":3},
  {"type":"building","id":"$building2","y":4},
  {"type":"building","id":"$building2","x":-1},
  {"type":"building","id":"$building2","x":-2},
  {"type":"building","id":"$building2","x":-3},
  {"type":"building","id":"$building2","x":-4},
  {"type":"building","id":"$building2","y":-1},
  {"type":"building","id":"$building2","y":-2},
  {"type":"building","id":"$building2","y":-3},
  {"type":"building","id":"$building2","y":-4}
]},
This code isn't short by any means, but at least it works :space
...Have a nice stay

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Fri Oct 06, 2017 15:37

Thanks!
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
Bearbear65
Inhabitant of a Supercluster
Reactions:
Posts: 3697
Joined: Fri Feb 10, 2017 14:53
Location: Japan

Re: Functionality using fun

Post by Bearbear65 » Sun Oct 15, 2017 9:15

Is it possible to make a plug-in with functions like this
Also if it is tell me I need help
For example you have four buildings A¹ , A² , B¹ , B²
If you click A¹ you will get A² but you can only turn A² back into A¹
if B² is next to it also to prevent B² making A² into A¹ accidentally
There is 2 modes for the B building B¹ (off) B² (on)
If you click B¹ (off) you get B² (on) that can change A² back to A¹
so simply B building is like a switch and A building is like a light bulb
ImageJust my opinion

User avatar
CommanderABab
Inhabitant of a Universe
Reactions:
Posts: 4204
Joined: Tue Jun 07, 2016 21:12

Re: Functionality using fun

Post by CommanderABab » Sun Oct 15, 2017 9:26

Yes
378378Currently using beta 378. 378 What's new? Bus stop bug fixed! :)
ImageTelegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

User avatar
Bearbear65
Inhabitant of a Supercluster
Reactions:
Posts: 3697
Joined: Fri Feb 10, 2017 14:53
Location: Japan

Re: Functionality using fun

Post by Bearbear65 » Sun Oct 15, 2017 9:28

How?
ImageJust my opinion

User avatar
CommanderABab
Inhabitant of a Universe
Reactions:
Posts: 4204
Joined: Tue Jun 07, 2016 21:12

Re: Functionality using fun

Post by CommanderABab » Sun Oct 15, 2017 10:39

temp.json
(1.56 KiB) Downloaded 1 time

Code: Select all

//Is it possible to make a plug-in with functions like this
// Also if it is tell me I need help
//For example you have four buildings
// A¹ , A² , B¹ , B²
//If you click A¹ you will get A² 
//but you can only turn A² back into A¹
//if B² is next to it also to prevent B²
// making A² into A¹ accidentally
//There is 2 modes for the B building
// B¹ (off) B² (on)
//If you click B¹ (off) you get B² (on) 
//that can change A² back to A¹
//so simply B building is like a switch and A 
//building is like a light bulb
{"id":"a1",
 "build time":0,
 "on click fun":[
      { "condition":{
           {"type":"building","id":"a1"},  
        },       
        "actions":[
          {"type":"remove"},
          {"type":"build","id":"a2"}
        ],
        "p":1
      }
    ]  
},
{"id":"a2",
 "build time":0,
 "on click fun":[
      { "condition":{
           {"type":"building nearby","id":"b2","min":1,"max":8},  
        },       
        "actions":[
          {"type":"remove"},
          {"type":"build","id":"a1"}
        ],
        "p":1
      }
    ]  
},
{"id":"b1",
 "build time":0,
 "on click fun":[
      { "condition":{
           {"type":"building","id":"b1"},  
        },       
        "actions":[
          {"type":"remove"},
          {"type":"build","id":"b2"}
        ],
        "p":1
      }
    ]  
},
{"id":"b2",
 "build time":0,
 "on click fun":[
      { "condition":{
           {"type":"building","id":"b2"},  
        },       
        "actions":[
          {"type":"remove"},
          {"type":"build","id":"b1"}
        ],
        "p":1
      }
    ]  
},
:teach :json
378378Currently using beta 378. 378 What's new? Bus stop bug fixed! :)
ImageTelegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Tue Oct 24, 2017 13:13

So how exactly does "random fun" work? I haven't found any documentation for it
I need random action from lots of given actions to be done if condition is true.
PS: I'm working with notifications here
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
CommanderABab
Inhabitant of a Universe
Reactions:
Posts: 4204
Joined: Tue Jun 07, 2016 21:12

Re: Functionality using fun

Post by CommanderABab » Tue Oct 24, 2017 13:29

Lobby :

Code: Select all

"random fun":[] 
can be used to create new stuff randomly

Me:
Lol

Lobby :
it's transitions are called for random x,y on the map

Me:
That could be disastrous. Cause pipe leaks and so forth. ;)

Lobby :
Yes, might be used for something like that😁

:teach
378378Currently using beta 378. 378 What's new? Bus stop bug fixed! :)
ImageTelegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

User avatar
KoalaGuy
TheoTown Veteran
Reactions:
Posts: 5066
Joined: Mon Mar 06, 2017 21:45
Location: Pitcairn Islands
Contact:

Re: Functionality using fun

Post by KoalaGuy » Tue Oct 24, 2017 13:57

Any ideas how to make the game choose a random notification then?
"Meow meow meow meow. Meow. Meow meow, meow meow meow!
TheoTown Translator (Serbian)
Becoming Best Plugin Creator Again
(My plugins)
Contributor
Once top poster
Most reactions
Made some graphics for TheoTown
Made some smilies

User avatar
CommanderABab
Inhabitant of a Universe
Reactions:
Posts: 4204
Joined: Tue Jun 07, 2016 21:12

Re: Functionality using fun

Post by CommanderABab » Tue Oct 24, 2017 14:12

378378Currently using beta 378. 378 What's new? Bus stop bug fixed! :)
ImageTelegram - t.me/
🆎️🆎️🆎️🆎️Commander🆎️🆎️

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests