How to use Scripts to program game objects

Knowledgebase / How to use Scripts to program game objects

 

The Level Editor is where you arrange the game objects in your game to build a world, but we need to program the game objects to bring them to life.

Usually programming has to be typed out as instructions that the computer will understand, but Gamefroot’s Script Editor gets rid of all that hassle. 

code

With the Script Editor, all you need to do is drag and drop blocks of code together like lego pieces. When these blocks are snapped together, they create sets of instructions – so you can program without needing to type out code! To open the Script Editor, click Script Editor in the top-right corner.

These are the main areas of the Script Editor interface that you need to know:

scripts-2

All the script blocks are available from the sidebar on the left, and they are arranged in categories with other similar blocks. For example, all the blocks that can affect speed, gravity, and collisions are found under the Physics category.

The workspace is where you drag blocks into, and arrange them to run as a program. Blocks are snapped together to create sets of instructions that run from top to bottom. Open the different categories in the sidebar, and try dragging some blocks out into the workspace. Try snapping different blocks together to see how that works.

 scripts-3

You can delete blocks by dragging them into the sidebar or into the trash can.

scripts-4

The Script Block Categories

 

category_01
Events: These blocks are the triggers for other blocks. All scripts must have at least one event block, or else none of the blocks will know when to run.
category_02
Control Flow: These blocks control how other blocks work by deciding when those blocks are run according to certain conditions. There are also blocks under this category that can destroy game objects and change the level.
category_03
Physics: These blocks can alter the velocity, acceleration, and gravity. They can also detect collisions between game objects.
category_04
Motion: These blocks can alter and detect the position, rotation, and scale of game objects.
category_05
Animation: These blocks can start, stop, and change the animation sequences for a game object.
category_06
Looks: These blocks can change the visibility and transparency of a game object, as well as changing the camera controls and stage colour.
category_07
Sensing: These blocks sense the position of the mouse, position of fingers on a touch screen, the tags that a game object has, and a game object’s class, as well as sensing a bunch of other things.
category_08
Sound: These blocks can play and change game music and sounds.
category_09
Operators: These blocks allow you to compare numbers and boolean states (true or false), and perform mathematical calculations. This category also has blocks for creating and investigating lists and text strings, as well as using colours.
category_10
Draw: These blocks allow you to draw shapes and text on to the screen.
category_11
Variables: These blocks allow you to create and use variables for remembering numbers, text, coordinates, lists, whether something is true or false, etc.
category_12
Functions: These blocks allow you to create and run functions, which are repeatable stacks of blocks.

Applying scripts to game objects

Once you have created a script, you need to apply it to a game object to get the script to work in your game. To do this, click on the script from the Scripts sidebar, and then click on the game object to apply it.
apply-script
The game object will then display a coloured icon so you can see which scripts have been applied to which game objects.
Note: If the script does not get applied to the game object, make sure you have the right layer selected. You can’t apply scripts to game objects on layers that you don’t have selected.

Script Properties

 

script-properties

 

The Script Properties window is accessible by clicking on the 3 lines icon in the menu bar of the script editor. In the Script Properies window, you can name and describe your script, and set whether or not your variables will be public, and what those variables should have as default values.
Ticking public will make that variable available from the game object’s instance properties – this makes it really easy to have individually customized game objects.

Instance Properties

To edit the instance properties of a scripted game object, right-click on the game object and choose Edit Instance Properties.

 

 instance-properties1
From the instance properties window you can set values for those public variables. This makes it really easy for you to change how a scripted game object works without having to change the script for each object.
instance-properties2
In the example above, this character can have its own message and health without those being predefined in the script – this means the Character script can be reused on other characters, and those characters can have different messages and health.