Sonus Interactive

Section 4 – Farm Game
Global Variables
Co-ordinates
Collisions
Conditional Collisions
Waiting
Applying actions to collision objects
TTS Variables
Playing Audio
Object boundaries
Global variable categories


For our next project we will make a small farming game. Where the player can walk around a small 5 by 5 grid and plant crops that will reward the player with points after they expire. The challenges we will need to overcome for this project are ensuring the player cannot plant crops where crops have already been planted, ensuring crops expire and increase the players score but also free the area for another crop to be planted and keeping the player within a border and not allowing them to move away from the payable zone.

First lets create a new project and create our global variable to keep track of our score and also a few other bits of information we will need to store for the game. use the alt button to access the top menu of the G.D.E program and use the right arrow key to select "edit". from here press down to select "Global variables" and hit enter.

In the global variable editor we will create the score variable but we will also create 2 more variables to keep a record of the players co-ordinates so we can place a crop in the same co-ordinates as the player when one is planted. We will store the players X and Y properties in a couple of variables and then when a crop is created we will set their X and Y properties to those variable values.

When using various variables to do different things its better to keep them stored in categories for better organisation. Lets tab to the "New Category" button and hit enter. A new window will pop up to create our new category and the only thing we need to enter is a name. Lets call it locations, and store any location related variable in it, after typing it in tab to submit and hit enter. Lets also create a category to put the score variable in, do the same again but this time lets call it "goals" to store anything goal related.

Tab to the new variable button and hit enter. The first selected text field will be the name of our new variable. Input "Score" and then tab to the value text field and input a 0. Next tab to the category combo box which will have our categories listed in it, select the "goals" item in the list and then tab to the submit button and hit enter. This will take us back to the variable editor window.
Create another variable but this time give it the name "Player X" and give it a value of 4, since this will be the starting X co-ordinate of our player object. For this one select the "locations" item in the category list and then tab to submit and hit enter. do the exact same thing again but call the next variable "Player Y" and give it a value of 3.

When in the Global Variable Editor we can tab to the category combo box which determines what variables are displayed in the list. By default it is set to all, but you can select one of the categories you have made and only they will be listed.

tab to "close" and hit enter to return back to the main G D E window.

Before we create our player object that will move around planting crops, lets create the walls that will keep the player contained. Use shift + F10 to bring up the Game Object tree menu when the tree is focused on and select "Add Game Object".

First input the Game Objects name as "Left Wall". The X property should be set to 1 and Y set to 3. This means if it was on a grid it would be one across to the right and 3 up. Set the BY to 2. This is the Y Boundary property , this property essentially tells us how tall we want the object to be. if the Y property is set to 3 and the boundary for Y is set to 2, then the object will be positioned 3 co-ordinates up and will stretch for 2 more co-ordinates up and down which means it will take up Y co-ordinates from 1 to 5, creating a wall on the left side or our imaginary grid.
On the walls collision event add a condition begin action. Tab to the condition settings and select the "Col" option in the drop down box for the variable. This option checks if the object is colliding with another specified object. Tab and input the name "Player" which is an object we haven't created yet but will shortly. The operator should stay as ==, if we select any other operator it will check if the object isn't colliding with Player as opposed to checking if it is. Tab to submit and hit enter.

Next add a play audio action however this time do not select "Self" in the "apply to" section, instead select the "collision" option. This option forces the game object that the wall is colliding with to perform the action rather than performing it itself, the colliding object will be the player walking into it. Input the sound name Thud and the volume as 1. The reason we want to apply this to the player is because the player will play a walking sound when the user presses the directional buttons. Telling the player object to play another sound will cancel the walking audio and instead we will hear the thud audio.

Next add an "Add X" action with a value of 1 below the play audio action and again apply it to the collision object. This action needs to be below the audio action because the play audio action will be looking for a collision object but if we change the X co-ordinate of the collision object before we apply our play audio action to it,then it will no longer be colliding and the play audio action will not be able to find it.

next add a condition end action afterwards. This means we now have a wall object that will trigger actions when the player walks in to it, not only that but it will check it is colliding with the player and if so it will change the players co-ordinate and increase its X co-ordinate by one which will push the player further right by one space. This means the player cannot pass the left wall.

We now need to create 3 more objects like this one, to be the Top, Right and Bottom borders of our farming area. Create a new object and give it the name "Top Wall" and give it a X property of 4 and a Y property of 6. This time give the BX property a value of 2 instead of the BY property. This means this wall object will be further to the right and further up, one space above the left wall object and will stretch in either direction by 2 co-ordinates, occupying from X2 to X6. Add the same condition as the left wall object but instead of increasing the players X co-ordinate by 1, decrease its Y co-ordinate by 1 , which means when the player hits the top wall the wall will reduce the Players Y co-ordinate by 1, making it move slightly down. Submit this object.

Our next wall will be the Right Wall. This wall will be at position X7 and Y3, with the BY property set to 2. The collision event should be identical to the others except it will subtract the player object X co-ordinate by one, meaning it pushes it to the left.

Our last wall will be the Bottom Wall. This wall will be at position X4 and Y0, with the BX property set to 2. The collision event should be identical to the others except it will add the players Y co-ordinate by one, meaning it pushes them further up.

Now we have objects that will keep our player object from leaving the area, but before creating the player object we keep referring to we will create the "Crop" game object first that the player will plant. These objects will be created when the player plants them and exist for a small amount of time before destroying themselves, alerting the player and updating the score.

create a new object and call it "Crop". We want the crop to exist for a little while and then destroy itself but update the score before it does. Firstly we need to make the crop get placed where the player currently is, and we do that by placing it at the stored Player X and Player Y variables we made earlier. We will make it so every time the player object moves these variable are updated. Make sure the creation event is selected in the event combo box. Our first actions will be "set X" and "set Y" and instead of inputting a number, input the variables names. So set X will have a value of "Player X" and the set Y action will have a value of "Player Y". Next add a wait action to the creation event, the only parameter you will need to fill in will be the seconds the action is required to wait, lets enter 20. Ensure the wait action is below the set X and Set Y actions. After the wait action we will add an "Add Global Variable" action to increase the score variable we made at the beginning of this tutorial. Enter the variable name "Score" and the value 10. Although we have updated the score, we haven't yet updated the Text to speech channel to hold the score in so it can be spoken to the player. Text to speech channels are stored memory where we input data to be played later, it can be text we input to be read or it can be a variable name so the variables value will be said. In this circumstance we want to cache the score variable value since the variable has changed. Add a Set TTS Channel action. Input the name "Score" into the Speech text field, and input a 0 into the channel text field. Meaning we will load the score value into channel 0, to be played later.

Lastly we will add a play audio action, although we haven't loaded any audio to be played yet, we will when we make the Init Object and it saves us coming back just to add it afterwards. Add a play audio action and input the name "Sprout" and the volume value as 1.

The last action we need to add is a "Destroy self" action, but we need to add this onto the audio finished event. So in conclusion the Crop object will be created and wait for 20 seconds, then it will increase the global variable score by 10, then it will update the TTS channel 0 to the new variable score value, then it will play the spout audio to inform the user and then it will destroy itself when the sprout audio finishes.

Now we have our crop and wall objects we can now make our player object. Create a new Game Object and call it "Player" and sets its X property to 4 and its Y property to 3 which will place it in the middle of the square space we have boxed in with our wall objects.

Next we need to make the player object move but also keep track of its location by updating the Player X and Player Y variables. Select the left pressed event an add a subtract X action and input a value of 1. Also input a play audio action and give it a sound name of “Left Walk” and a volume value of 1. This will now move our player left when the left button is pressed. Since we have altered the X co-ordinate we should also add a "set global variable" action and set "Player X" to X, which will change the Player X variable to the value of this objects X value.
Now we will make the player move right. Select the right pressed event an add an add X action and input a value of 1. Also input a play audio action and give it a sound name of “Right Walk” and a volume value of 1. This will now move our player right when the right button is pressed. Also add an action to set the global variable “Player X” to X.
Now we will make the player move forwards. Select the up pressed event an add a add Y action and input a value of 1. Also input a play audio action and give it a sound name of “Forward Walk” and a volume value of 1. This will now move our player up when the up button is pressed. Add a set global variable action and set “Player Y” to Y.
Now we will make the player move backwards. Select the down pressed event an add a subtract Y action and input a value of 1. Also input a play audio action and give it a sound name of “Backwards Walk” and a volume value of 1. This will now move our player down when the down button is pressed. Also add an action to change to global variable “Player Y” to Y.

None the audio has yet been loaded so none of the play audio actions will actually work yet, once we have made the player object we will make an object who's only purpose is to create all the other games objects and load the games audio before destroying itself, and this object will be the games initial object.

The next function we must add to the player is the ability to plant crops using the 1 button. Go to the button 1 pressed event. We must ensure the player is not standing on an already existing crop before we allow them to plant another. We can do this by using a condition begin. Add a condition begin to the button 1 pressed event and tab to the condition settings. The variable combo box needs to be set to "Col" which is an abbreviation of collision. This means it is not checking for a local or global variable but instead checking for the collision of an object. Tab to input text into the text field and enter the name of the object we are checking for. In this case we are checking if the player object is colliding with a crop object, so input the name "Crop". The operator needs to be set to "NOT ==" which means it will check that the objects are not colliding. If it was set to "==" it means it would only trigger if the player was colliding with a crop. Any operator other than "==" will change the condition to check there is not a collision rather than checking if there is. Tab to submit and hit enter.

Next add a create object action below the condition. Only one text field will be available to specify what game object to create. Input the name "Crop" and submit the action.
After the create object action add a play audio action. Input the sound as “Plant” and the volume as 1.
After that add a condition end action below the create object action.

Now whenever the player presses 1, it will check that the player is not colliding with a Crop object and then create one if it isn't.

Next go to the Player objects Button 2 press event and add a Play TTS Channel action. Input the channel 0 and submit the action. Now the player can press button 2 to hear their score. After this we can submit the Player game object.

The last object we will create will be our Init Object. Its only purpose will be to create the player and the wall objects and also load the audio we need for our project. Since this project is only small it is fine to load all the audio at once, but in larger games the Sonus may not be able to store all the audio in memory at once. It would be better to unload audio that inst required at certain parts of the game or the Sonus could fail to run the game properly.

Lets create a new object and call it Loader. In its creation event add 5 create object actions to create the Left Wall,Right Wall, Top Wall, Bottom Wall and the Player object.
Next we will make it load the audio for the game. Add 7 load audio actions. The audio you need can be downloaded at the bottom of the page. There a 7 files. For the first load audio action input the New Sound text field as Sprout, we reference this already in the crop object. Next tab to the browse button to open the file browser and find the audio file "Sprout.ogg" and tab to the open button and hit enter. Then submit our new action. Do this again for the other audio files.
Load the walk left.ogg file with the name Left Walk.
Load the walk right.ogg file with the name Right Walk.
Load the walk forward.ogg file with the name Forward Walk.
Load the walk back.og file with the name Backwards Walk.
Load the thud.ogg file with the name Thud.
and lastly load the plant.ogg file with the name Plant.

Some of the sound reference names are the same as the ogg file names for simplicity. Now we have created our objects and loaded our sounds we have no more need for this object. Add a destroy self action so the object is erased from the Sonus memory and frees up space for future objects but ensure the destroy action is the last action in the list so it performs it last.

Submit the new Loader object so it is added to the game object tree. tab to it and use shift + F10 to bring up its menu and select "Set As Init Object" so it will be the first object to load during runtime and will proceed to load the others in its creation event.

Downloads:
Plant   Sprout   Thud   Walk Back  Walk Forward  Walk Left   Walk Right