Fermat
Search
K

Creating your first tool

Learn by building a simple counter

Hello artisan!

Let's start building some tools together ✍️ 🎨 🔭
We'll start with the basics by learning how to respond to click events and change the properties of our blocks. These two simple but powerful actions will allow you to create interactive apps with only a few lines of code.
To do that, we'll define the behavior of a simple app that counts how many times we clicked a button.
Click counter
Start creating a button and use "Right-click" to open block Context Menu. You'll see the option "Edit behavior". This opens the Behavior Editor where you can write your own Arbor code and execute it using the Run button.
For example, you can change the Text value of the button using set:
set me.value to "Hello"
Change the value of the text when running the code
However, you would probably want your app to react to actions within Fermat rather than clicking the Run button each time. There are multiple actions and events that help you do that. In this tutorial, we'll use the when click event:
when click me:
-- do something
end
Defining a simple Click event
You can also define new properties in your block. For example, we can store the number of performed clicks and later use this to change the Button value:
set me.totalClicks to 0
when click me:
set me.totalClicks to me.totalClicks+1
set me.value to me.totalClicks
end
Finally, let's try to change properties of other blocks. You have noticed that we have constantly used me.property to change the current block properties. Instead of using me we'll use another block's id to access its properties. You'll easily find it and copy it using the Inspector and save it as a variable and change its properties as you want
set anotherBlock to "yourBlockID"
set me.totalClicks to 0
when click me:
set me.totalClicks to me.totalClicks+1
set anotherBlock.value to me.totalClicks
end
Find other Block's ID and use it to change to block value