Creating a Custom Behavior

Our built-in behaviors do almost everything we need, but there’s one thing missing: when our player’s health reaches zero, we’d like them to be transitioned to our Game Over scene.

Since there’s not a built-in behavior that makes that happen for us, we’ll need to create one ourselves!

Making Our Custom Behavior

Return to the freestyle editor’s dashboard and select “Behaviors” from the left-hand sidebar.

Click the New Behavior button and name our behavior “Game Over When Health Reaches Zero”.

Now find our new behavior in our game’s behavior list and click Edit Behavior.

Defining our customer behavior’s parameters

Parameters let us pass settings to our behaviors when we configure them. They help freestyle’s game engine know what elements and objects in the game that we’re referring to when we build our behaviors, and they help us reuse our behaviors in multiple scenes (and even multiple games) without needing to create a new behavior each time a setting changes. You can think of them like options or settings for the behavior that let you build behaviors that can work in multiple scenarios.

Our Game Over behavior will have three options:

  • Health Variable: The global variable that stores our health, which we’ll check to see if the game is over.
  • Game Over Scene: The scene that we’ll transition to when the game is over.
  • Game Over Sound: The sound that will play when the game is over, before we transition our scene.

Within our behavior, we’ll wait for our Health Variable to change. When it changes, we’ll check if its equal to zero now – meaning the player has run out of health. If they have, we’ll keep the player from interacting with our game for a moment (so they can’t click around after they’re dead), play the Game Over Sound, and then re-enable the user’s input (so they can click on the next scene) and transition them to the Game Over Scene.

Let’s set up those parameters now. Click the Edit Parameters button at the top of our behavior.

In the sidebar that opens, click “Add Parameter.”

Name your parameter “Health Variable”, and set the type to “Global Variable.” Input a brief description to help you remember what the parameter does–I used “The global variable to check to determine if the game is over.”

Now, click Add Parameter again. Name the parameter “Game Over Scene”, set the type to “Scene”, and input a description.

Finally, click Add Parameter one last time. Name the parameter “Game Over Sound”, set the type to “Sound”, and input a description.

Types of Parameters

Before we move on, let’s spend a little more time on the parameter system itself. When you were creating your parameters for this behavior, you may have noticed that freestyle supports almost a dozen different parameter types.

Each parameter type represents an object in the game or information from the game that your behavior might want to interact with. In general, if your behavior is interacting with something in the game – whether it be moving an element, switching to a new variant on the element, or using a number to set the speed of an element, for example – you should add that data as a parameter and use that parameter’s value when you build your behavior.

Now that you’ve created these three parameters, click the X in the upper right-hand corner of the Parameter sidebar to close it.

Adding an event

Behaviors are built on top of events. Events represent something that happens in our game, like two elements colliding or the player clicking on something.

We want our behavior to run every time a global variable changes. We’ll check if the global variable that changed is our health variable, then we’ll check to see if the health variable equals 0. If it does, we’ll end the game.

Click the “Add Event” button in the lower left.

In the popup that opens, select “Global Variable Value Changed”. Then, click on the new event in the sidebar. You’ll see the behavior editor open for that event, with the list of our available actions in the sidebar on the left.

From the sidebar on the right, drag the “If Equals” condition into the event. When you’re over the event, the inside of it will turn blue, indicating that you can drop that action there.

Configuring our “If Equals” condition

From the “Select a Value” dropdown on the left, select “Updated Global Variable”. From the “Select a Value” dropdown on the right, select “Health Variable.” This compares the global variable that was updated (returned by our event) with the Health Variable parameter that we set.

Most freestyle events, including this one – Global Variable Value Changed – are triggered whenever that event happens. If our game has multiple global variables, the event might be triggered by a Global Variable that we don’t care about in this behavior. We start our behavior by checking to make sure that the Global Variable we’re interested in in this behavior – the player’s health – was the Global Variable that was updated. 

Checking if the Score is Zero

Now, drag another “If Equals” from the right sidebar. This time, place it inside the If Equals check that we just added. 

From the “Select a Value” dropdown on our new “If Equals” statement, select “Use Internal Information”, then click “Open Internal Information Editor.” Note that we renamed a previous featured, “Calculated Value”, to “Internal Information” since this tutorial was originally written. You’ll see some references to the old name in the screenshots below.

Most of our actions do something to the game based on information that we dictate, like setting an element’s speed to a numerical value we provide. Sometimes, though, we need to read information from our game – as it’s being played – to figure out what to do. That’s where Internal Information comes in. Internal Information is all data that’s coming from our game live as it’s being played, like the value of a global variable – the player’s health, in this behavior – or the speed of an element.

In this behavior, we’re testing to see if the player’s health equals zero, so we use Internal Information – the value of the Player Health Global Variable – to do that.

In the “Select a Value” dropdown, choose “Get Global Variable Value”. Select the “Health Variable” parameter to read a value from. You’ll notice that there’s dozens of pieces of internal information that we can read, so take a moment to skim through them to get a sense of the information that you can use in your own games.

Your calculated value should look like this:

Then click “Save.”

In the rightmost “Select a Value” dropdown, select “Enter a Custom Value”, then enter 0. This makes our conditional check to see if the value of our global variable, set by the Health Variable parameter on our new Behavior, is equal to 0. Custom values let us input numbers directly instead of adding them as a parameter to our behavior. For this behavior, we always want the game to end when the player’s health is zero, so we don’t create a parameter to read from – since zero is zero, no matter what game we’re playing.

Playing a Sound And Disabling Input When the Game Is Over

When our game is over, we want to keep the player from moving the Player element. We also aim to play a sound that designates that the game is over. We can add two actions to our newest If Equals condition to do that.

Find the “Disable Input” action and drag it into our innermost If Equals statement.

Now, find the “Play Sound” action and drag it into the innermost If Equals statement, too. Then, select the “Game Over Sound” parameter to set the sound we’ll play when the game is over.

Finally, we need to transition to our game over scene. Drag the “Switch Scene” action into our innermost If Equals statement. Select the “Game Over Scene” parameter as the scene to switch to.

Adding Our Custom Behavior to Our Level Scene

Now that we’ve created our behavior, we need to attach it to our scene. Return to the freestyle editor dashboard, head to the Scenes tab, and open your Level 1 Scene. Click on “Behaviors” to access the behavior list, and select the “This game’s behaviors” tab to view all the behaviors that are in our game – including our new custom behavior.

Click the “Add to scene” button next to our new custom “Game Over When Health Reaches Zero” behavior.

Then switch to the “Active on this scene” tab and scroll down to our “Game Over When Health Reaches Zero” behavior.

Set these properties for the behavior:

  • Health Variable: Health
  • Game Over Scene: Game Over
  • Game Over Sound: Die