Parameters in custom abilities and custom rules
In this article, we explore how to use parameters in your code.
Table of Contents
- Introduction
- 1. Figure out what should be a parameter
- 2. Update the code inside your custom ability or rule
- 3. Use the custom ability or rule in code, and set the parameters
Introduction
Custom abilities and custom rules allow you to reuse code. This saves you from having to drag out the same blocks again, but you might want to make the code work with inputs in different cases.
For example, you might have an ability that draws a rectangle. Using the custom ability saves you from having to repeat the code each time you want to draw this rectangle.
However, it would use the exact same code each time, so it would draw the rectangle with the same size every time.
What would be useful is if you could adjust it for different widths and heights. Then you could reuse the ability to draw different-sized rectangles and make a picture.
What are parameters?
Parameters allow you to do this. A parameter is a variable that exists for a custom block and can be set to a different value each time the custom block is used. You could replace the numbers in the code inside the custom block. Then, each time the custom block is used, different values could be used.
After creating a base custom ability or rule, which contains patterns of code that you'd like to reuse each time, here is one process that you can follow, to use parameters for it.
- Figure out the numbers or text you'd like to be able to replace each time
- Make the code inside your custom ability or rule use the parameter
- Use the custom ability or rule in code and set the parameter values
1. Figure out what should be a parameter
If you notice yourself reusing bits of code with changes each time, like a different number for a certain part, a parameter could be used in place of that number.
For the case of the "Draw rectangle" custom ability, I would like the rectangle to be able to draw with different widths and heights each time — not just 160 and 100.
So these are the bubbles that I'd like to be able to change, each time the ability is used.
Then decide on names for the parameters that will be used in these bubbles, and create the parameters in the custom ability or rule.
Here are some tips on editing custom blocks to add parameters.
How to edit an existing custom ability or rule
When you're creating a new custom ability or rule, you can add parameters. But you can also update an existing custom ability or custom rule, by following these steps:
-
Press and hold on the custom ability or rule in the block's keyboard.
-
Tap the "Edit" option.
How to add a parameter
-
When editing a custom ability or rule, you can tap the plus button to add a new parameter.
-
Then you can type the parameter name, and enter a default value. To start with, you could use the number that you had previously been using.
-
When you're ready, you can tap "Save" in the top right corner, to save your changes.
How to delete a parameter
You can delete a parameter by clearing the name and then pressing backspace.
How to duplicate an existing custom ability or rule
You can also duplicate the custom block. This will create a new custom block with the same parameters. It will not affect the previous custom block and where it has been used.
-
Press and hold on the custom ability or rule in the block's keyboard.
-
Tap the "Copy" option.
2. Update the code inside your custom ability or rule
Now that we have added parameters to our custom ability or rule, let's update the code inside to use the parameters.
How to find parameters
The parameters that exist for the custom block will show up in the Local variables section on the Variables keyboard. Like other local variables, they will not appear outside the scope where they exist — in this case, they won't show up outside the custom ability or rule where they exist.
Replace the numbers or text with the new parameters.
3. Use the custom ability or rule in code, and set the parameters
When you use the block, you can type a number or text in the bubble next to the parameter name, which will be used as the value of the parameter.
Now you can use the block wherever you like in your code, with different parameter values each time.
For example, here is a T-shape, made with different-sized rectangles from the "Draw rectangle" custom ability.
- Sample project for "Draw rectangle" custom ability with parameters: https://c.gethopscotch.com/p/129pdgp6av