Creating games with formulas

Written by Maaike on 16/08/2016 18:28:25

In each game that you make in the GameCreator [link homepage], you will probably need some formulas to get things working. To achieve certain points, opening doors on commando, the disappearing of items or giving the enemy strength; formulas are part of it. In this article we will explain how a formula is put together and give a few examples of formulas in your future games.
There are many formulas possible and you are free in creating them, but when you start using them for the first time it can be a little difficult to find your way in the formula forest. Though it is not as hard as it might seem, just keep in mind who needs to do what and when? Basically that is the ground rule of making games work in the GameCreator.
The formulas are an extension of selectors to allow you to do simple and complex math equations. Formulas are allowed in many element fields which require numbers (position, value, target, elements). For creating formulas with the math element you can use the same formulas that are available in JavaScript.

The ingredients of formulas

The three parts of a formula:
1) Start the formula with an equal sign.
2) Choose the element by using their name: #name
3) Tell them what to do.

There is a huge variety of formulas that you can use. Once you’ve written down the equal sign, you're free to write any equation. Depending on the field there are selectors, constants and methods available to support you.

Using selectors

Formulas can have either generic or constant selectors. A generic selector refers to an exact name, which is very useful when you want to address specific objects. This can be a single object, but also a group with different objects but with a partial identical name [link how to formulas and names]: BluePlane, PurplePlane, RedPlane.

You also have constant selectors, these are general variables that have always the same meaning. So !sender is always the sender of an action regardless of the context.

Generic selectors

Selector   Description 
!<id> The exclamation mark (followed by a number) searches for an element with the given id. Each element has its own id.
#<name> The hash followed by a name searches for all elements with that name.
.<name>  The point followed by a name searches a collection of elements for the first one with the given property name and returns the value, not the elements.
#<name>? The hash followed by a name and a question mark behind it, searched for all the elements that starts with the name. Placing the question mark in front of the name, then it finds the elements ending with that name.

Constant selectors

 Selector Description 
 !self Always points to the current element.
!parent Points to the parent element of the current element.
 !sender Points to the element in which the event occurred. This is only available through connections.
 !receiver Points to the element to which the action is sent. This is only available through connections.
 !start Points to the element that started the event action chain. This is only available through connections.

Other constants

Constant x
The x constant points to a number, but it depends on the context what it means. For connections x stands for the index of the connection. If three connections are fired, the third connection would have an x of value 2.


Since there are so many possibilities in using formulas in your games, we worked out a few examples to help you out.

Creating a scoreboard

Imagine you want an increasing score [link startersguide] in your game for clicking on the right objects. You’ve already got a text field for the score and the object that should be clicked.

Add a math element [link how to score counter] and make an action from the object to the math element. Set action on OnClick, event on Add and choose your value. For example 1 extra point per click.

Set the value of your score at 0 (math element settings) to start with zero points. We named the math element Scorecount, so in order to get your rising score it needs to be connected to the text field of the score. Add the formula in Text in the element settings of the score field:

Creating a scoreboard

Changing speed

You are making a platformer game where one of the obstacles is jumping on moving plateaus. The player has the power to change the speed of those moving bricks by clicking on some buttons.

The floor pieces already follow a path [link how to paths] that makes them go from left to right automatically. Add two images that are the buttons for in- and decreasing the speed and a math element (we used the name PathSpeed). Make an action from one button to the math element, telling it to decrease once clicked. Do the same for the other button, but choose for increasing. Choose your values.

In the element settings of the path and each key frame you set a formula in the playtime:

Changing speed

Keeping a tidy backpack

Your player can collect different items in a backpack and those items must be neatly presented. For example in a nice rows of four, with the same space between them. Give the objects a name that’s partial identical [link how to formulas and names], so they all respond to the same formula: ItemFlower, ItemBird, ItemBottle.
Make an extra camera [link how to switching cameras] to show the inside of the backpack and create an action from this camera to one of the objects. First you need to set the receiver on the name of the objects. For example =#Item?. We set event on OnActivated and action on MoveTo, because when we activate the backpack, the items need to sort themselves.
For this setup we need two formulas, which you can fill in at Value. The first row is the X-as for columns and the second is the y-as for rows.
X-as/columns: = x % 4 * 70
Y-as/rows: = ceiling (x / 4) * 70
The X in these formulas redirect to the receiver and gives them a number, so it counts the object to set them in four columns. The 4 refers to the amount of columns or rows you want and the last part * 70 is the size of object. If your object is 60 px width and you want some space between each object, you can use 70 px to create the white space.
Instead of ceiling you can also use floor or round to create neatly lined out rows.
Keeping a tidy backpack

Eliminating activated objects outside your game

An endless stream of enemies [link tutorial random enemies] is approaching your player. Each one that the player avoids flies out of the game, but is still active. If the player is incredibly good in flying and keeps on going for a long time, the game might get slow because of all those active enemies.

To turn them off once they left the game, we need to create some kind of border [link tutorial collide with ease]. Add an image in the form of a beam that is situated outside the game at the point where the enemies fly through and add a collision behind the beam.

In the settings of the collision you set material on passthrough so they don’t crash on the beam. Next you need to the name the element where the objects go through and who those objects are.

Elements: =#Beam
Target: =#Enemies

You probably already have a cloner [link tutorial automatically appearing objects] to create the endless stream of enemies. Create an action from the collision to the enemy cloner.  Set event on OnTouchStart, action on Remove and the value will be =!start. So when the enemy hits the collision they will be removed.
Eliminating activated objects outside your game

Follow the player

Your player is about to start his run through the desert, which is quite a long road. It would be best if the camera followed the player, so you’re in no need of difficult paths and other structures to make the world move forward.
First create your world, set the background somewhere above or below your level and add your player at the start point. Set in the x-as position of the camera: =#Player.translation.x Make sure your camera is enabled and active.
Also add a camera for your background. Place the same formula in the x-as but add /2 (or any other number), it makes that the background is moving slower than your running player:
If your background stops showing, because you reached its end, you just add some extra backgrounds next to it. Make sure there is no white line between them.
Follow the player

Extra help

Need some extra help with the formulas? Here you can find many tutorials where formulas are used or place your question in our community.
Did you find formulas that shouldn’t be missed in this list of examples, please let us know!