When you’re designing blocks, it’s a good strategy to make them as reusable as possible. Don’t over-optimize a block for that one particular actor. Say, for instance, you need to make a slime that turns blue for 5 seconds when the player hits it. If the “blue” and “5 seconds” are hard-coded into the block’s code, what happens when you need a slime that turns red for 4 seconds instead?
That’s what properties are for.
This block, for example, could declare two properties: the color to use, and the duration, and then implement its logic in terms of these properties:
export const PROPS = [ propColor("Color"), propNumber("Duration", 3) ]; export function onInit() { // Start green. setTint(0, 1, 0); } export function onCollision() { // Bump! Change to the requested color. setTintColor(props.Color); // Remind ourselves to change back to green. sendToSelfDelayed(props.Duration, "RestoreColor"); } export function onRestoreColor() { setTint(0, 1, 0); }
Users of the block (you!) will now be able to assign its properties:
The more flexible you make your blocks, the more you will be able to reuse them in different actors and contexts!