Learn how to use BLOCKS CODE metadata to interact with your BLOCKS using dynamic parameters.
Launch the BLOCKS CODE application and open the script called DynamicParameters.littlefoot
. You can find the script in the littlefoot/scripts/Example Scripts
folder. If you don't have the BLOCKS CODE IDE installed on your system, please refer to the section Getting started with BLOCKS CODE for help.
Let's start by adding parameters to the side panel of BLOCKS CODE that will appear under the Parameters tab.
These are created using an XML format enclosed between multiline comment blocks and <metadata>
tags at the top of the file. Variables are created between <variables>
tags and they can also be grouped using <groups>
tags as follows:
Within the variable declaration, the string given to the "name" attribute becomes the variable name in the subsequent littlefoot code.
Let's now implement the repaint() function of the littlefoot program. This function is called periodically at approximately 25Hz on the device and is used to control the LEDs on a Lightpad.
In the following piece of code, we first call the clearDisplay() function which will reset the screen to a blank state so that any LEDs that were turned on in the previous iteration of the repaint() function are turned off.
Then we use the fillRect() function to draw a rectangle on the screen using the variables defined before, only if the draw button is checked. The fillRect() function takes as argument the colour retrieved by the makeARGB() helper function and the position and size of the rectangle we wish to draw.
In this example, we learnt how to add parameters that can be tweaked at runtime to control the behaviour of a program in realtime.