Section 3 - Punch Up
Covers:
Global variables
Loading and
playing audio
Conditions
Creating and destroying objects
Our next project is something closer to what can be considered a game
than the last project. We will make a small game where the user can
press a button to punch an opponent, but must use another button to
block when they hear an attack coming. The audio for this tutorial can
be downloaded at the bottom of this page.
If we already have a
project open then we can use the "alt" key to access the file menu and
press down to select new project and save it in a location of your
choosing. this will clear all your current data
so make sure your project is saved if you want to keep it before
starting a new one.
With a new project open, we will make a global
variable to keep track of if the player is blocking or not and we will
create two objects which will be the player and the opponent.
First lets setup the global variable we need, use the "alt" key to get
to the file menu strip at the top of the program if you aren't there
already. Use the right arrow key to navigate to the "Edit" menu and
press down to scroll through its contents. "Global Variables" should be
the first option in the list. hit enter and it will open up a global
variable editor window. The first selected control will be the list of
global variables which at the moment will be empty. Keep tabbing until
you get to the "New Variable" button. You will notice along the way
there is a drop down box for categories and buttons to create and delete
categories. Categories are where we can store global variables for
better organisation but we wont get into that in this tutorial, for now
just tab past them. When you press the "New Variable" button a window
will pop up where you can enter a name and a starting value. I will call
my Variable "Is Blocking" and the value will be 0, it will change to a 1
when the player presses the block button and return to 0 when they let
it go. we will achieve this in the “Player” game object events we
haven't created yet. Leave the category option as None and tab to submit
and hit enter. We have now created a new global variable. Global
variables can never be destroyed and will exist as long as the game is
running. Now we are back to the variable editor window, tab to the close
button so we return to the main window of the program and lets create
our player and opponent next.
Tab to the game object tree and use
shift + F10 to bring up the tree options, then use the arrow keys to
scroll down to "Add Game Object". The name will be the first text field
selected. I'm going to call mine "Player". The next options we tab
through are the X,Y,Z,BX,BY,BZ,Delay and Loop Audio options, all of
these can be left as they are. We also don't want to set any variables
for this object either so the variable button can be tabbed past. Tab to
event combo box list and select Creation, which should already be
selected as its the top of the list. Then select the add action button
and hit enter. We are going to load in the audio we need for the game in
the players creation event. Audio used in the Sonus must be in the .ogg
format and must be loaded into the consoles memory before it can be
played. The actions related to audio are, Load Audio to load in a file
into memory and give it a name that we can reference, Play Audio which
plays the file using its reference name,Stop audio which stops any audio
the object is playing, pause and resume audio, unload all audio which
will clear the memory of all loaded audio and increase,decrease
and set audio volume. As well as the play audio action there is also a play
positional audio action, which plays audio like the play audio action but it plays it
relative to a listeners position, meaning 3D environments can be made, which we
will cover in later tutorials. Game objects can only play one audio file at a
time and up to 16 game objects can play individual audio files
simultaneously.
Tab to the add action button and hit enter. The
new action window will open with the"Apply To" section as the first
thing you are focused on, this is where you can select "Self","Other" or
"Loop Object". Ensure "Self" is selected to make this the object that
loads the audio and then tab to the action drop down list and find the
"Load Audio" action. There will be a "New Sound" text field and a
"Directory" text field that you can then tab to. The "New Sound" Text
field will be the name we will give the loaded audio object. I will call
mine "Player Punch". I can then tab to the browse button which will
allow me to navigate my computer to find the actual .ogg audio file.
When you click this option you will open a browsing window and the
filename text field will be highlighted, tab until you reach the tree
view which will list all the folders in your computer.
Please
ensure that all audio loaded is within the same folder as your project
or in a subfolder, the G D E will give you an error if you try to
load an audio file that is not it the project files folder. This means
if you desire to share a project you can simply copy the whole folder
for somebody else to open on their computer.
find the folder
that contains the audio you downloaded from this page and press enter,
you can then use tab again to select the list of files inside the
folder. Scroll through the folder using the arrow keys until you find
the Player Punch.ogg file. Press enter and it will bring you back to the
add action window with the directory filled in. Now we can tab to the
submit button and we have created our first loaded audio.
Repeat
this process for the other audio files. You will need to create a sound
called Player Hurt and use the "Player Hurt.ogg" file, and you will need
to create "Enemy Punch" and use the "Enemy Punch.ogg" file.
Afterwards we should have a Player game object with 3 load audio actions
in its creation event.
Now go to the button 2 press event, we
will make it so when the player presses the button 2 it will set the Is
Blocking variable to one, and when the button 2 is released it will
return the variable back to 0.In the button 2 pressed event add a new
action, select the “set global variable” action. You will need to fill
in the variable and the value you wish it to be set to, so we want to
input "Is Blocking" and the value needs to be 1. Then click submit.
We can now go to button 2 release event and do the opposite, create an
action to set the global variable back to 0 using the same method.
Now we can identify when a player is blocking by using a global
variable. Next lets go to the button 1 press event and add an action.
Go back to the event drop down list and select button 1 pressed,
then click add action again. Add a condition begin action so we can make
it so the player cannot attack while they are defending. After selecting
the condition begin action from the action list you will then tab to the
condition settings next, the first setting is the Variable combo box
list, this allows us to select if the variable we are checking is a
Local variable, a Global Variable, or a constant number by selecting
rather L,G or C from the list. We will learn more about the Col option
in later tutorials. Select G so the condition will check for a global
variable and then tab to input the variable name, which is "Is
Blocking". Tabbing again will take you to the operator list, we want to
check if the variable is equal to 0, which means the player is not
blocking. Select the "==" option. tab again to select the variable we
will check against. In this instance we are checking if the Is Blocking
variable equal to 0, and 0 is a constant value. So select the C option
and input the number 0. We can now submit the condition begin. Next add
the play audio action. There are two text fields to enter, one is the
name of the audio you want to play, which in our case is Player Punch,
the other is the volume you want the object to play it at. The value can
be between 0 and 1. so 0 is silent, 1 is full volume and 0.5 is half
way. lets put a 1 in this text field and tab to submit. Now we just need
to add a condition end action which has no parameters to fill in, simply
select it in the list and click submit. In the event action list ensure
the action order is correct and use the move action buttons up and down
if not, the condition begin and condition end need to have the play
audio action between then to specify that action is in the condition
block and the condition must be met before it will play. If that is
correct then now every time the button 1 is pressed we will hear this
audio play.
Now we have completed our Player object and can
submit it. After submitting the object select it in the game object tree
and bring up its menu with shift + F10. Select "Set As Init Object" and
hit enter. This tells the Sonus that this is the first object to load.
Although we create objects in the G D E environment they are not created
when the game is running, we are creating the blueprints that the
in-game objects will use to create themselves. In-Game game objects are
created using the create object action, but the game must start with one
object already created, this object is called the initial object or Init
object for short. Player will be our Init Object and we will eventually
add a create object action in its creation event to create our opponent
object, but we must make the blueprint first.
Now add another
game object to the tree. This game object will be the players opponent
that will periodically attack. Give it the name "Enemy" and then tab
past X,Y,Z,BX,BY and BZ until you reach the delay text field. This text
field is where we can input a number to determine how many seconds need
to pass before the object fires its periodical event. This is an event
that will constantly loop every so often, if the delay is set to 0 the
event wont ever fire, but if for example it was set to 5, the periodical
event would fire every 5 seconds. lets input 10 and then tab to the
event drop down list and select periodical. Add a new action and select
the Play Audio action. Input "Enemy Punch" in the sound text field and
input "1" in the volume text field. Now every ten seconds this game
object will play the enemy punch audio.
So now we have our player
who can block and attack, and an opponent that will attack every 10
seconds. We now need to distinguish if the player is blocking when the
enemy attacks and play the "Player Hurt" sound if they aren't.
Go to
the Audio Finished event in the event combo box, which is the event that
will fire after the Enemy Punch audio has finished. In this event we
will check if the player is blocking and if not then create a new object
whose only purpose is to play the "Player Hurt" audio then destroy
itself. So add a new action to the audio finished event, make it a
condition begin and check if the Global Variable "Is Blocking" is not 1.
Next add a create object action. We haven't created the blueprint for
the game object we wish to create yet but we will name it "Player
Damaged", so add the create object action and input the name "Player
Damaged" into the text field and then submit. Now add an Condition End.
Now we need to create the blueprint for the “Player Damaged” game
object we were just referring to. Submit our new Enemy object and then
go back to the game object tree and add another game object. Call this
one "Player Damaged" and add an action to its creation event. The action
we want to add is Play Audio for the Player Hurt sound. Next go to the
Audio Finished event and add a destroy self action, so the object will
play the audio when its created and then destroy itself when it is
finished.
The last thing we need to do now is return to our Init
Object which was “Player”. Find it in the object tree, once it is
selected use the tab button to scroll through its properties. Tab to the
event combobox and ensure creation is selected. Add a new action. Add a
create object action and input the name “Enemy”, then click submit. This
means that as soon as the game starts the Init Object will runs its
creation event and create our opponent.
So to recap. We have
3 Game Object. A player, An enemy and an object to act as a single fire
sound effect. the Player object allows the user to play an attack sound
effect and also can block. The opponent will periodically attack and if
the player isnt blocking when the opponent attacks then the Player
Damaged object will get created to play the player hurt sound effect.
We should be able to use the alt key to enter then top menu and
press right until your find the run option so you can test your game.
Downlaods:
Player
Punch Player Hurt
Enemy Punch