script - Gamefroot

Code an entire game with Scripts
Your Gamefroot game can be converted from a generic platform game
into any kind of game you can imagine. All you need to do is add new
game graphics, and a few scripted items that change fundamental game
mechanics, e.g. the position of the game camera, hiding the player
character, introducing mouse controls, using large items in the place of
terrain, etc.
Now that you know your way around the Script Editor, we can explore
more powerful ways of controlling our games. We are going to build an
example game that removes all platform game elements (such as
characters and terrain), and relies entirely on scripted items.
This example game will feature a group of friendly fish that you must
steer clear from a dangerous shark by guiding them with your mouse.
The fish will follow the mouse around at varying speeds, and the shark
will swim across the screen at increasing speeds. The goal of the game
is to keep all your fish alive for the longest time possible to get a high
score.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
The scripted items we need to make for this game are:
- A camera item that locks the game screen to the centre of our level.
- A mouse pointer for the fish to follow
- 6 follower fish which trail after the mouse pointer at various speeds
- A shark which appears from alternate sides of the screen at random
positions. The shark’s speed increases with each successive
appearance, and kills fish when they come into contact with it’s mouth
Let’s start with a new level. First we need to
get some new game graphics from an asset
pack on the Marketplace. The asset pack that
we want to grab is named “underwater”.
Open the Marketplace in the sidebar, then
click on the pack to your collection of assets
(it’s free).
Then open My Resources and make sure the
pack is added to your game so the assets
become available in the sidebar.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Now let’s set up the level as shown below:
Open the Items tab from the sidebar, and place down 6 fish next to
each other. Then place a shark next to them, put a mushroom in the
middle, and a star near the shark (the mushroom and star will be
invisible when we’re finished). Finally draw a row of terrain tiles along
the bottom and place the player character on top of them.
Each of these items will have a script attached. The first one
will turn the mushroom into an invisible camera. Right-click on
the mushroom, and select edit script and this item.
These are the blocks and tokens that will be used to make this script.
You can find the “Go invisible” and “Snap camera to” blocks in the
Animation tab, and “Become not solid” in the Motion tab. “Change
score”, “If ( ) then”, and “Player’s Health” are all in the Control tab, the
“My location” token is in Sensing, the light blue “( ) < ( )” token is in the
Operators tab, and the rest of the blocks are in the Events tab.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
If we read the blocks in sequence then we can understand exactly
what will happen when our new script runs in the game.
When the game starts, it will make the item it is attached to go invisible,
and then it will snap the camera to it’s location. It will also constantly be
increasing the score by 1, and it will be constantly checking if the
player’s health is lower than 10, in which case it will make the game go
to the Game Over screen with the text “Try again?”
Once you’ve finished making this script, save the script and close the
script editor to return to level edit mode.
Since we selected the
mushroom item by
right-clicking on it to begin
with, the script will already
be applied to it. This is
indicated by the coloured
script icon in the top-right
corner of the item.
The next script to build is the mouse pointer, and this will be
attached to the star. Right-click on the star item and select
edit script and this item. Here is what we want to create:
As you can see, this script is pretty similar to the previous one. By
reading the blocks we can see that this scripted item will stop being
solid and go invisible when the game starts, while constantly
teleporting itself to the location of the mouse.
Once you’ve finished building it, save it and go back to level edit mode.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Now let’s make a fish! Right-click on the first fish on the left,
and click edit script, this item.
First, let’s make it follow the mouse around. We want to create a loop
that keeps moving the fish towards to the mouse pointer. The loop
needs to get triggered initially, then continue to trigger itself. To do this,
we want to start with the “When first created” and “When I receive a
message” blocks from the Events tab. Here is what we want to build:
If we read only the yellow and the black blocks while ignoring the blue
blocks for now, we can see that when this scripted item is first
created (that’s when the game starts) it will send itself a message of
“follow”. This initial message is then received by the lower set of
blocks because of the message receiver block (When I receive a
message of “follow”), which activates the blue blocks, and then finally
sends itself another message of “follow” in 200 milliseconds. Then
the whole sequence will repeat every 0.2 seconds and this will
continue forever. In computer programming, this is called a loop.
Now you know how the loop works, find the blocks required and start
building the whole sequence. The the blocks and tokens required are
listed on the following page...
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
When you’ve arranged the blocks as shown on the previous page,
you’ll see the green “Location of instance...” tokens have empty slots:
Those red “instance...” slots need to specify a target. Click on the red
slot to open the instance selector, and then choose the mouse pointer
script attached to the star.
Then click Select instance.
Now we can see that the
blue blocks are setting the
velocity of our fish to the
difference between the
location of the mouse
pointer and the location
of the fish, and then
multiplying that
difference by 2. So the
further away the mouse is,
the faster the fish will
travel towards the mouse!
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Once you’ve built the whole sequence of blocks for that loop in the fish
script, save the script and test out your work by previewing the game.
As you can see, the fish follows your
mouse pointer around, while the
other fish and the shark don’t do
anything since we haven’t scripted
them yet. Also, our fish doesn’t rotate
or change the direction it’s facing,
and the game is set in a jungle when
it should be underwater!
At this stage you should change
the background of the game to an
underwater one. Do this from File,
Level Properties. The underwater
asset pack has an ocean themed
background that you can use.
After that we can set up the shark
script, and finish off our fish script
by making it rotate based on where
the mouse is.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Right-click on the shark item, and click edit script, this item.
Once you’ve recreated the script as shown above, you can understand
how the blocks work together. The shark starts off traveling left, and
then when it leaves the screen it tells itself to go to a random Y position
(vertical), then face the other direction and start moving that way after a
certain amount of time. That amount of time will get smaller as the
score increases, and the shark’s X (horizontal) speed will keep
increasing. The player is being used as the shark’s “mouth”, so the fish
know that when they hit the player they are being eaten. This is why the
player is constantly teleported to the shark’s location.
Save the shark script, return to level editor
mode and then find the invisible character in
the Characters tab in the sidebar. Hover your
mouse over it, and click the Set as Player
button. This will make it so that the shark
doesn’t appear to have a character attached
to it as it swims around. Also remove the gun
from the player through the level properties
window (from the File menu).
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Open the Behaviours tab in the sidebar, find the Fish script, and click
edit to open it. Add the new blocks as shown inside the dotted line:
These new blocks will make the fish constantly rotate and face in the
direction of the mouse pointer, and also make the fish get eaten
(disappear) when the player touches it (remembering that the invisible
player is always teleporting to the shark’s mouth).
Save the fish script and return to level edit mode.
Now we are going to make the rest of the fish follow the leader fish in a
line. To do this, we are going to need to make copies of our original fish
script by grabbing the source code from one script and pasting it into
another script.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
To view a script’s source code, hold shift and click the Gamefroot
logo inside the script editor. Do this in the fish script you created earlier.
Then the View Source
button will appear.
Click View Source to
reveal the source code
for this script.
Right-click on the code and choose Select All, then right-click on the
selected code and choose Copy.
Now you are ready to paste this code into a new script. Close the script
editor without saving any changes.
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.
Right-click on the fish to the right of your original fish, and select edit
script, this item to create a new script for this fish.
Inside the script editor, click on the View Source button. Delete all the
source code, and then paste the source code from the previous fish
script. Then click Update Workspace. Now this new fish script should
look exactly like the original one.
This new fish script needs to be
slightly different. Instead of
following the mouse, this fish
needs to follow the leader fish.
Click on all the red instance
tokens (
) and replace
them with Fish instead of
mouse pointer.
Now this fish will follow the first fish around. Click Script Properties and
save this script as Fish 2. Then close the script editor, and repeat this
same process with fish 3, fish 4, fish 5, and fish 6. Each fish must have
it’s own script, and each fish needs to be made to follow the previous
one; Fish 2 follows fish 1, fish 3 follows fish 2, fish 4 follows fish 3, fish 5
follows fish 4, and fish 6 follows fish 5.
And that’s it! Save your game then play around with it. Try adding new
things to make this game better, or use the ideas you’ve learned to
make something brand new. Congratulations fellow game developer,
and welcome to the wonderful world of Game Development!
Copyright © 2014 Instinct Entertainment Ltd. All Rights Reserved.