Sunteți pe pagina 1din 71

Unit 10

Game Development 2

Page 1 of 71

Unit 10 Game Development 2

Creating an Educational Title using Adventure Game Studio

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 2 of 71

Initial Setup
At the first window, select Start a New Game and click continue At the Start New Game Wizard Click Next At the Template Window, select Default Game. For your own game try any of the available templates At the New Game Details window, give your new game an appropriate name. o For this example, something like First game, Tutorial or Example would be appropriate. Then enter the file name for the executable. Keep it to 8 characters maximum. o Again keep it appropriate, like Example etc. o Your project game will have its own appropriate name Then set the folder where the game will be stored during its creation. o Set this to your external USB drive or your H:\ drive Click Finish and after a little but you will be presented with the AGS interface ready for development

Ongoing / Subsequent editing


For subsequent editing of your game, when your open AGS, you can either select; o Continue an existing game, and browse for its location. o Or if your game is showing in the Recently Edited Game list, you can select it, and click Continue a Recently edited game, and click continue.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 3 of 71

Configuring the General Settings


Now we have the game started, the first changes we need to make are to the General Settings for the game. These control things such as colour depth, character movement, text output and other settings which are required for setting up the game. To access the General Settings, simply double-click the General Settings node in the Project Tree.

Project Tree

Properties Grid

The General Settings Page opens allowing you to configure the setup of the game Make the following changes under the relevant headings (Setup) Colour depth : 32-bit Resolution : 320 x 200 By specifying this resolution, it means that you will need to create all of your background images to match this resolution. If you want to have higher resolution, you need to specify it here as well as create higher resolution background images. NOTE: We set the actual screen size of the game later using the Runtime Engine Setup program.

Character movement All Character Movement Settings are set to True


First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 4 of 71

Dialog Speech style : Lucasarts

Text-output Anti-alias TTF fonts : True

Visual Enable Letterbox mode : True

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 5 of 71

PART 1 Daves Room

Characters
By default the game comes with only one character, cEgo, who is quite an unattractive sprite. We will need to import (and later for your project, create) our own characters. In the Project Tree expand the Characters node. You should see the default character called cEgo underneath the tree. Import a new character by Right-clicking on the Characters node and selecting Import Character

Navigate to the characters folder and open Dave.cha The character cDave will now appear under the Characters node. The Convention for naming characters is cName_of_character Double-Click on cDave to view his properties.

You need to click here to make Dave the Players character Drag the handle to resize the Property Grid

View characters animations

At any stage you can also delete the ego character, views 1 & 2, as well as the default sprites for the ego character, but only do this if you are confident that you know what you are deleting.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 6 of 71

Examining Daves Properties The Property Grid shows all the properties for the selected item.

ScriptName: How Dave is referred to in script StartingRoom: The room that Dave starts in StartX: Initial x position StartY: Initial y position

Import the following characters the same way you imported Dave. Baron.cha Mika.cha Max.cha Chris.cha

As we no longer need cEgo delete him by right-clicking on him and selecting delete if you havent already. Change all the characters except Dave to Room 0. We will set these as needed later. Now that we have some characters for our game we need to import some rooms.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 7 of 71

Rooms
Expand the Rooms node in the Project tree to reveal the default room in the game. Right Click on Room and Click New Room. You can then delete Room 0 by right clicking on it and clicking Delete this room. Now expand the 1: node and double-click on Edit room. Naming the Room: In the Properties Window change the Description to Daves room. The Room Editor itself is currently blank (as shown Below)

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 8 of 71

Import a Background: The first thing we need to do is import a background. Ensure that Background to display: is set to main Background and click on the Change button.

Navigate to the backgrounds folder and select Davys Room.bmp The Background Preview Window will now show the imported background

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 9 of 71

Game Setup
At this stage we can test our game and see how it looks Before we run it though we need to run the Game Setup. This is a two part process.

Start by selecting Build Build EXE (F7) from the menu bar. Once the game has compiled select Build Run Game Setup Choose the settings shown below. It is at this point that you change the graphics filter to 2x nearest-neighbour filter. This will change the Resolution to 640x400.

(Note that some of the options may be different depending upon your PC. Also some PCs may not support all the options available. You will need to experiment to find the best settings for your PC).

Press Save and Run If everything went well your game should run. If there are any error messages, simply re-run the game setup and change the options. In particular, the graphics driver and filter used. Dont worry about the GUI error messages at the bottom of the screen at this stage if you get any. These will most likely be relating to your image size used for your backgrounds.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 10 of 71

Playing the game

When you are playing an AGS game your mouse cursor is replaced by an icon representing the mode you are in. The default mode shown (Walk to) allows you to direct the player around the screen by left-clicking where you want the player to walk to. Right-Clicking allows you to change the cursor mode between the following modes Walk to Look at Interact with Talk to

Moving the cursor to the top of the screen reveals a menu which also allows you to choose one of the modes as well as other options we will examine later.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 11 of 71

Use the Walk-to mode to direct your player around the screen. You will notice that wherever you click your player walks, even on top of the bed and on walls. To define where characters are allowed to walk within a room we need to define what are called walkable areas. Close the game and return to the Room editor

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 12 of 71

Walkable Areas
To define walkable areas you need to choose the Walkable areas option from the drop-down menu.

You create walkable areas by simply filling in the areas you want to be walkable with the drawing tools provided. AGS provides Line, Freehand, Rectangle and Fill tools.

Use the left mouse button with these drawing tools provided to make a walkable area similar to that shown. Dont worry about the bit that is in front of the bed post at this point. Well fix it shortly.

Walkable Area

Because it is the bottom middle of the character that is used to check these areas you should create the areas slightly smaller than they need

Test your game by pressing the Run button

OR by pressing F5.

Your character should now only be able to walk on the areas you defined. The player will be able to walk anywhere covered with a coloured blue rectangle(s). If for some reason you want to edit or delete the walkable areas, select the Rectangle tool and use the right mouse button to drag a rectangle over the existing area you want to remove, starting in the top left corner.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 13 of 71

Walk-Behind Areas
The next task is to define the walk-behind areas. When testing your game you may have noticed that when the character walks past the bedpost at the top they are drawn in front of the post.

Walk-behind areas tell the game where the character needs to be drawn behind the background image. In this case, behind the bedpost. From the drop-down box choose Walk-behinds.

Use the drawing tools to create an area over the bedpost.

Setting a baseline To make our walk-behind work we have to create a baseline which defines where the player has to be in order to be drawn behind the walk-behind area. You set the baseline by moving the mouse cursor to the bottom of the walk-behind area and reading the y coordinate. You then type this number into the baseline property of the walkbehind.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 14 of 71

x
Get y coordinate here

Enter y coordinate

Hold mouse cursor

Test your game. This time when the character walks behind the post, they should be drawn behind it.

Debugging
You can view the walkable areas of the game while you are playing it by pressing CTRL-A. CTRL-D will provide you with some information about the current room. CTRL-X will allow you to teleport to another room. There are other debugging features available which will be explored once we do some scripting.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 15 of 71

Interaction
While the character can walk around, they cannot do anything else. We need to add some interaction to our game. There are three things that characters can interact with in AGS Hotspots, objects and other characters. We will start with hotspots

Hotspots
A hotspot is an area of the background image that the player can look at and interact with.

We will create two hotspots. One on the picture on Daves wall and the other on the window. To add a hotspot make sure you are in the room editor and select Hotspots from the same drop-down menu that you chose Walkableareas and Walk-behinds from. Create a hotspot over the picture using the drawing tools the same way you added a walkable-area.

Now that we have defined a hotspot we need to provide it with a Name and a Description. In the Properties Grid for the hotspot define the Description as James Hetfield Pic and the Name as hpicture

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 16 of 71

To add another hotspot select the drop-down of the selected hotspot and choose hHotspot2.

Because the first hotspot is no longer selected it is drawn in grey.

Draw the second hotspot over the window. Define the following properties Description: Daves Window Name: hwindow

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 17 of 71

Walk To Points
When the player uses the look-at mode to look at a hotspot we want the character to walk to the hotspot. We can set this up through the walk-to-point property for each hotspot.

Select the hpicture hotspot from the selector To work out the location where you want the character to walk to when you look at or interact with the hotspot move the mouse cursor (crosshair) to the location on the background where you want your character to walk to. Read the Mouse Position located under the Change button. The 1st number is the x location while the 2nd number is the y location.

Type this number into the WalkToPoint in the properties for the hotspot. Repeat this process to place a WalkToPoint for when Dave interacts with the Window. Test your game. Check that when you use the look-at mode to interact with both the picture and the window Dave walks to the walk to points you set. You will notice however that Dave does not face the hotspots. To make this happen we need to do some scripting.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 18 of 71

Scripting
Since AGS 3.0, all the interactivity in the game is handled using scripts.

Make sure the hotspot hpicture is selected, and then click on the Events button The Property Grid changes to reveal all the events for the hotspot. EVENTS occur when the player does something in the game. At the moment there are no events set up. When the player looks at the picture we want 2 events to occur. 1. Dave faces the picture 2. Dave speaks

Click on the that appears.

row and then click on the button with the three dots

The script editor opens with a function that will run when the look-at mode is used on the hpicture hotspot. Type the following script in-between the curly brackets
cDave.FaceLocation(211, 53);

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 19 of 71

As you are typing you will notice that you are given code hints to help you choose the command you are looking for. This script tells the character cDave to face the location 211,53. The location was obtained the same way a WalkToPoint was (By simply holding the mouse cursor over the location we want Dave to face). Test your game. Dave should now face the picture after he first walks to the WalkToPoint. Now it is time to make Dave talk. Before Dave actually speaks we will display a message to tell the player what Dave is looking at. After the command for making Dave face the picture add the following commands.
Display("It's a picture of James Hetfield from Metallica"); cDave.Say("I Love this picture");

Test your game to view the result.


Display("It's a picture of James Hetfield from Metallica"); cDave.Say("I Love this picture");

Displays a message to the screen. It is displayed in the standard message box, and centered in the middle of the screen. It will only be removed when the player clicks or presses a key

Displays the message as speech above the character's head. Only appears for a limited time.

The default style that is used to display the character speech is set up in the General Settings under the Dialog heading.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 20 of 71

Other Display Options


DisplayAtY (5, "It's a picture of James Hetfield from Metallica");

Similar to the Display function, except that this will display the message box at the specified Y location on the screen. The Y defines the co-ordinate of the top of the message box. DisplayAt (180,30,100, "It's a picture of James Hetfield from Metallica");

Identical to the "Display" function, only this allows you to define the position and size of the window where the text is displayed. The X and Y variables define the co-ordinates of the upper-left corner of the window. The above message will be displayed at coordinates 180,30 in a box 100 pixels wide. DisplayTopBar(25, 8, 7, "Narrator", "It's a picture of James Hetfield from Metallica");

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 21 of 71

Displays a message in a text window, with a caption bar on top of it. The 2nd number (8) is the text colour and the 3rd number is the back colour (Light grey) of the caption. cEgo.SpeechColor = 10; cDave.Say("I Love this picture");

Gets/sets the character's speech text color. The initial colour is set up in the characters property grid. cDave.SayAt(120, 20, 100, "I Love this picture");

Similar to Say, except that the text is displayed with its top left corner at (X,Y), in an area WIDTH wide. SetSpeechStyle(eSpeechSierra); cDave.Say("I Love this picture");

Changes the way in which speech text is displayed. SpeechStyle can be: eSpeechLucasarts - speech text over character's head eSpeechSierra - close-up portrait of character eSpeechSierraWithBackground close-up portrait + background window for text eSpeechFullScreen QFG4-style full screen dialog pictures

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 22 of 71

We now need to repeat this process so that when Dave looks out the window, Dave faces the correct location and we get an appropriate message displayed. What you do is up to you. Perhaps something like this. (Notice it occurs in the EVENT of an Anyclick on hotspot)

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 23 of 71

PART 2 The Rest of the Game Starting with the Hallway & Inventories
OK, moving on, we need to create the rest of the house. Well start by creating a new room. To create a new room right-click on the Rooms node and select New room Import the background image hallway.bmp In the Properties Grid assign Hallway as the Room Description. Create a Walkable area that will allow characters to walk on the carpet and down the stairs.

Scaling walkable areas


In rooms like this one which have a significant Z axis variance (ie depth) to them, it is a good idea to set up scaling in the walkable areas. This is used so that the characters grows in size as they get closer, and appear to walk off into the distance by shrinking. This is used to provide an artificial 3D effect to a 2D room. To do this, make sure the rooms walkable areas are being shown, then in the scaling properties, change UseContinuousScaling to True. This will reveal two new parameters of MaxScalingLevel and MinScalingLevel. For this room, set Max to 100 and Min to 85. NOTE: Your MaxScalingLevel can actually be greater than 100 For your game, you will need to use a bit of trial and error to get the appropriate scaling levels.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 24 of 71

Making characters change room


For Dave to leave the room we want him to use the interact mode (the hand) on the Door in his room. We will set this up now. Open the editing window for Room 1. Select the next available hotspot and give it the properties shown on the left. Create a hotspot that covers the door and a walkto point near the foot of the door. Click on the Events button for the hotspot and select the Interact hotspot Event. Click on the button to open the script editor.

Once again a function is created to handle the event. Add the following script between the curly brackets cDave.ChangeRoom(2,211,145);

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 25 of 71

The ChangeRoom function


The ChangeRoom function changes the room the character is in. You can also set the x and y coordinates for where the character will be positioned when they enter the room

cDave.ChangeRoom(2,211,145) will move the character cDave to Room 2 at x coordinates 211,145.

Transitions
You can also specify what transition to use as the game changes between rooms. On the line directly before the ChangeRoom function add one of the following commands. All future transitions will use the transition specified until it is changed.
SetNextScreenTransition(eTransitionBoxout); SetNextScreenTransition(eTransitionFade); SetNextScreenTransition(eTransitionDissolve); SetNextScreenTransition(eTransitionCrossfade); SetNextScreenTransition(eTransitionFade);

At this point you also need to set up a change room function for walking back from the Hallway (Room 2) to the Bedroom (Room 1) Youll need a hotspot and walkto point for the hallway door. Youll then need an Interact hotspot function when you click with the hand on the door. This will need appropriate x,y coordinates for the arrival point in the bedroom.

Its a good habit to get into to create the appropriate change room functions as soon as you create each new room.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 26 of 71

Room 2
In Room 2, Dave finds a credit card lying on the floor. When Dave picks the card up it is added to his inventory so that he can use it later. The first thing we need to do is import the graphics required. Double-Click on the Sprites Node The Sprites that are used in the game are displayed in folders on the left. Right-Click in the whitespace and choose Import new sprite from file

Select ccard.bmp from the Objects folder The Import Sprite Dialogue Box opens

Accept the defaults and press Import Whole Image. The sprite is added to the Sprites folder. Each sprite has a unique number. The credit card is given the next available unique number. We now need to put the object in the room Open Room 2 and change the drop-down selector to Objects
First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 27 of 71

Right-Click on the background room image and select New Object Here A picture of a blue cup appears. This is the default object graphic (the symbol for AGS). We simply need to replace this with the picture of our credit card. Select the blue cup and in the properties grid click on the image property. Now click on the button. All the sprites available in your game are shown. Choose the credit card sprite and select Use this Sprite. The image of the cup is replaced with the credit card sprite. Give the credit card a Description and the Name: oCreditCard.

The card is a little too big though. To change the size that it is displayed at we need to return to the Sprites Node. Select the credit card and change the Resolution property in the property grid to the 640x400, 640x480, 800x600 option.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 28 of 71

Return to Room 2 and the Credit Card should be a more realistic size. Note however that the size of the Credit Card object shown in the hallway in the development window is different (much larger) than it is shown when the game is compiled and run. Dont let this fool you. You always need to test you game to see what it will look like when it is compiled and run. The last thing we need to do is set the baseline for the card. Change the baseline property to 5 so that it is always drawn behind Dave, and so that Dave is not drawn behind the card. To do this, Set BaselineOverriden to True and Baseline to 5.

Picking up Objects
Now that we have an object for Dave to pick up we need to create an inventory item for it. At the moment the card is simply an object. Inventory items are totally separate from room objects. Items which the player can carry need to be set as inventory items.

Inventory Items In the Project Tree double-click on the Inventory Items node. There are currently two inventory items in there a key and a poster. These are there by default To add a new inventory item right-click on the inventory items node and select New Inventory Item. In the Properties Grid for the new item give it the Description: Credit Card and the Name: iCard.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 29 of 71

Click on Image and then Choose the Credit Card Sprite from the Sprite Window and select Use this Sprite (You must do this step so that you have a visible item in the inventory. If you dont do this, when you select the object in the inventory your cursor will disappear) You now have an inventory item that can be used. When Dave interacts with the credit card OBJECT we will give him the credit card INVENTORY ITEM. Go back to the Objects pane of the Room Editor for Room 2. Click on the Events Button in the Properties Grid.

Click on the button in the Interact Object event. There are basically 2 things that we need to do when Dave interacts with the card 1. Remove the card object from the room 2. Add the iCard item to his Inventory

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 30 of 71

Type the following script in the oCreditCard function


function oCreditCard_Interact() { oCreditCard.Visible = false; cDave.AddInventory(iCard); }

Run your Game and use the Interact_mode (the Hand) to pick up the Credit Card. Once the Credit Card has disappeared move the mouse cursor to the top of the game screen to reveal the menu bar. Click on the Suitcase icon to reveal Daves inventory items.

The Credit Card appears in the list of inventory items. Click on the Credit card to select it. The mouse cursor is replaced by the card. (As long as you selected an image for the inventory item in the earlier steps) Click OK. The credit card becomes one of your Modes which you can cycle through by right-clicking. In the Inventory Window you will also notice a magnifying glass. This is so that you can Look at your inventory items. We need to add an event however for what happens when it is used on an inventory item.

In the Project Tree open the inventory node and double-click on iCard. Select the Event button and click on Look at inventory item. Open the script editor by clicking on the three dots () This time instead of the Room Script opening the Global Script opens.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 31 of 71

The Global script is responsible for handling game events such as inventory items and characters which are not confined to specific rooms.

Add the script shown below between the { } of the function that is created.
function iCard_Look() { Display("It's your Credit Card"); }

Now when you run the game and use the magnifying glass to look at the Credit Card the message Its your Credit Card will be displayed.

Tidying up the Credit Card pickup


At the moment Dave can pick up the credit card from anywhere in the room. He doesnt walk to the card first. Lets tidy this up so that when Dave interacts with the card object he first walks to it and then says a message before picking it up. Open the Rooms node (Hallway) in the Project Tree and double-click on the Room Script for Room 2 Hallway. Add the following commands shown in italics to the oCreditCard_Interact() function.
function oCreditCard_Interact() { cDave.Walk(180, 140, eBlock); cDave.FaceObject(oCreditCard); cDave.Say("What is my Credit Card doing here"); cDave.Say("I better pick that up"); Wait(40); oCreditCard.Visible = false; cDave.AddInventory(iCard); }

Make sure you put the Walk, Face and Say actions before the Inventory action, as these actions occur in the sequence they are written in the script.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 32 of 71

Lets examine the commands


1. cDave.Walk(180, 140, eBlock);

This is the command to tell a character to walk to a location in a room. It requires 3 parameters.
X location (180) Y location 9140) Blocking (eBlock)

Blocking can be either eNoBlock (the default) or eBlock.


eNoBlock returns the control immediately to the next line in the script meaning that the character will move in the background while the script continues to run. eBlock will wait for the character to finish moving before returning control to the script and running the next command.

2.

cDave.FaceObject(oCreditCard);

This command will make Dave face the object specified (oCreditcard).

3.

cDave.Say("What is my Credit Card doing here"); cDave.Say("I better pick that up");

These commands we have already used before. They simply show Dave talking. Remember that you can change the text colour if you want.

4.

Wait(40);

This command simply pauses the script for the given time measured in loops. There are 40 loops/second. Therefore a value of 40 will pause the game for 1 second.

Unnecessary Default Inventory Items


Before you do this step, go through the next example of how to combine inventory Items first. Then once you know how that works, its fine to remove the two default Inventory items of Key and Poster. Right click on them in the Project Tree and click Delete this Item. You can also remove their sprites if you dont think youll use them.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 33 of 71

Combining Inventory Items


First, create the sprite for the new object that will be created by combining inventory objects. Now, in Inventory Items, choose one of the items that will be combined (in this example well use the key) and click on the Events Button In Events, choose Use inventory on this item Add the following
function iKey_UseInv() { if (player.ActiveInventory==inventory [2]) { cDave.Say ("Cool, look at the thingy I just made"); cDave.Say ("Dunno what it does, but its cool"); cDave.AddInventory (iThingy); cDave.LoseInventory (iKey); cDave.LoseInventory (iPoster); } else {cDave.Say ("That's not gonna work"); } }

PART 3 The next room videos & invisible objects

Room 3 : The Laundry


You should be able to complete the following 3 steps by referring to previous sections of this tutorial 1. Create a new room with the following properties Background: laundry room.bmp Description: laundry 2. Create an appropriate walkable area 3. Set up hotspots for the doors so that Dave can walk into the laundry from the hallway and back into the hallway from the laundry. Do not forget to set walktopoints Test your game and ensure that Dave can easily travel between the rooms. The Laundry Room story In the Laundry is a washing machine and a dryer. Dave notices that there is something in the dryer on the right. Unfortunately it wont open. Dave decides to use his credit card on the dryer (a trick he picked up from watching Magyver) and the dryer opens. Inside is Daves iPod. On the screen is a message telling him to play it. He does and a video clip plays.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 34 of 71

Multiple Backgrounds Rooms in your game can have up to 5 backgrounds. These can be used for animating the screen or changing scenes in the same room. For example, going from night to day. In our game when Dave is in the laundry room and he opens the dryer with his credit card we want to show a background with the Dryer door open.

Dryer door closed

Dryer door open

To import another background into a room select the drop down menu shown and choose <Import new background>

Choose laundry room2.bmp By default when there are more than 2 backgrounds to a room the game will cycle through the rooms to create the illusion of animation. This is not what we are after though. We only want the second background to be shown once Dave has used the Credit Card on the Dryer. We need to set the background to the main background when Dave first enters the room. Open the Events for the Room and select Enters room after fade-in Open the script editor for the event and type the following command inside the function
function room_AfterFadeIn() { SetBackgroundFrame(0); }

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 35 of 71

This command sets the Background image to (0) the main background.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 36 of 71

Hotspots and Interactions


Add a hotspot to the door on the Dryer on the right and setup a walktopoint. Setup the following interactions Dave Looks at the hotspot Dave says It looks like there is something in there Dave interacts with the hotspot Dave says It doesnt want to open

Using Inventory items on Hotspots The next event we need to setup is for when Dave uses the Credit Card on the hotspot. In the events window for the hotspot select the event Use inventory on hotspot

Add the following commands to the function


function hDryer_UseInv() { if (player.ActiveInventory == inventory[3]) { SetBackgroundFrame(1); } }

This is called a conditional statement or an if statement. It is used to check a condition and then only perform the commands inside the { } IF the statement is TRUE. In this case we are checking IF the player is using inventory item 3 the Credit Card. IF they are THEN the background frame is set to 1, which is the second image showing the dryer door open.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 37 of 71

Ensuring only the Correct Inventory item works on a given Hotspot The example above where the credit card is used to open the dryer is a perfect example. Another more complicated example is when you want only a certain item to be able to unlock a locked door. In this case you would do the following. First set the variable (for whether it has been interacted with ) in the global script and export the variable.

Then, in the room where the action takes places, import the variable Then write the function for using the correct inventory item on hotspot. In this example function, use an if then else statement, using the player.activeinventory script and the inventory item number to set the condition. Very importantly, set the variable now to 1

Finally, write the function for simply interacting with or looking at the hotspot. Again in this function, use an if then else statement, using the if variable == int script to set the condition.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 38 of 71

The iPod
We want to do more than just show the second background image though. Inside the dryer should be Daves iPod. To achieve this we will add an object to represent the iPod and simply make it visible when the second background is displayed. The iPod will not be an inventory item however, therefore we only need a sprite to represent the object. Import the Sprite Open the spites node in the Project Tree and right-click in the white space and choose Import new sprite from file Select iPod_small.png Say YES to use the Alpha channel and select the top-left pixel as the transparent colour.

Add an Object to the Laundry room Open the laundry room in the editor and use the drop-down selector to choose objects. Right-Click anywhere in the room and select New object here

In the properties grid change the description to iPod and the name to oIpod

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 39 of 71

Replace the image of the blue cup with the iPod sprite by clicking on image in the properties grid and selecting the iPod sprite.

Place the iPod object in the dryer window.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 40 of 71

Making an Object Invisible


We do not want the iPod to be visible until the dryer has been opened. When Dave first enters the room we want the iPod to be invisible. Open the Events for the laundry room and choose the Enters room before fade in event.

Add the following command to the function. The room_Load() function runs as soon as the room loads
function room_Load() { object[0].Visible = false; }

This command simply sets the visible property of object[0] (which is the iPod) to false therefore making it invisible. While you have the room3 script open find the hDryer_UseInv() function and add the command in bold to it. function hDryer_UseInv() { if (player.ActiveInventory == inventory[3]) { SetBackgroundFrame(1); object[0].Visible = true; cDave.Say("It's my iPod"); cDave.Say("How did that get in there"); } } This will make the iPod visible when the background frame changes after Dave uses the credit card on the dryer door. A couple of messages have also been added.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 41 of 71

Interacting with the iPod When Dave interacts with the iPod he is provided with a close up view of the iPod screen. On the screen is a message saying Click to Play. Clicking on the iPod screen plays a movie.

To achieve this we will create a new room with a blank background and only an iPod object. Create a new room called iPod (this should be Room 4) Choose blank.bmp as the background. Change the ShowPlayerCharacter property to False. When we switch to the room we will not be able to see Dave. This allows us to create the effect of a close-up view of objects. Import the sprite for the iPod Open the Sprites node and import the image iPod.png from the backgrounds folder. Change the resolution to 640x480. This will be used for the object in the room.

Add the iPod object to the room Go to the editor for the iPod room and select objects from the dropdown. Add a new object to the room. Choose the iPod sprite imported in the previous step to represent the object. Give it the name oiPodLarge and description iPodLarge to distinguish it from the smaller iPod object in the Laundry Room Position the iPod in the room as shown below. Take notice of the yellow box. It is this which you use to position the iPod. It should be equally spaced from left to right, and close to the top of the white space.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 42 of 71

Now we need to setup an interaction so that when Dave interacts with the iPod in the laundry room he is taken to Room 4 showing the iPod. Open the laundry room, choose objects from the dropdown and click on the iPod in the Dryer to select it. Open the events panel for the iPod and select Interact object. Add the following script
function oIpod_Interact() { cDave.ChangeRoom(4); }

Notice we do not provide x and y coordinates as Dave will not be visible in the room. Test your game. When Dave interacts with the iPod inside the dryer, room 4 should open showing the close-up view of the iPod. Dave should not be visible.

Now we want to set it up so that when Dave clicks on the iPod screen a movie will play.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 43 of 71

Playing video files


AGS can play any video files that are supported by Windows Media Player. You will need to make sure the video plays in media player before you use it with AGS. Open Room 4 and select Objects. Select the iPod object and open the events. Select the Any click on object event. Add the following script to the function
function oiPodLarge_AnyClick() { PlayVideo ("dog.avi", 1, 1); Wait(16); cDave.ChangeRoom(3,219,136); }

Playvideo
The PlayVideo command requires 3 parameters PlayVideo(NameOfVideoFile, skip, flags) dog.avi = name of video file to play Skip defines how the player can skip the video. Options are
player can't skip video player can press ESC to skip video player can press any key to skip video player can press any key or click mouse to skip

Flags defines how the video will be played. Options are


player can't skip video player can press ESC to skip video player can press any key to skip video player can press any key or click mouse to skip

While the video plays the game is paused IMPORTANT: To play video files they must be located in the Compiled folder where your game files are. You will need to copy dog.avi from the video folder to the compiled folder in your game folder.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 44 of 71

Once the video has finished playing there will be a slight wait period and then Dave will be returned to the laundry room. Note that for completeness of this tutorial game, you should really add the iPod to the Inventory, and set it so that when you interact with the iPod whilst in the Inventory it replays the dog.avi video. There are some other more significant problems however. Firstly, when Dave changes rooms, the room functions run again. Therefore, the background of the room is restored to background[0] with the Dryer door closed. Secondly, if we want other scripts to run once Dave re-enters the room (for example, we want Dave to say some messages once the video has finished), we cannot add these commands in the room 4 script because they will only apply to the current room (room 4). To overcome these problems we will need to use some global variables. Global variables are variables that can be used in any script in any room. Declaring Global Variables Before you can use a global variable it must be declared in the global script. Open the scripts node in the Project Tree and double-click GlobalScript.asc. There will already be a significant amount of code in there already as part of the default game template. Add the following lines

int playvid = 0; creates a variable called playvid and initialises its starting value to 0; export playvid; allows the variable to be imported into other scripts Reopen the script for room 4 At the very top of the script add
import int playvid;

This imports the playvid variable so that it can be used in the room. Add the following line (shown in bold) to the oiPodLarge function
function oiPodLarge_AnyClick() { playvid = 1; PlayVideo ("dog.avi", 1, 1);

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 45 of 71

Wait(16); cDave.ChangeRoom(3,219,136); }

This sets the playvid variable value to 1 Open the script for the laundry room At the very top of the script also add
import int playvid;

Find the room_AfteFadeIn() function and add the following shown in bold.
function room_AfterFadeIn() { if (playvid==1) { SetBackgroundFrame(1); object[0].Visible = true; Wait(16); cDave.Say("Wow, that was a strange movie"); cDave.Say("Who is the Baron?"); cDave.Say("..and why do I have to go to the general store?"); } SetBackgroundFrame(0); }

This is the function that runs after the room has faded in. Previously it was being used to set the background image to the first background. We use an IF statement to check the value of playvid first. If it equals 1, meaning that the video has been watched then we can set the background to the 2nd image, make sure the iPod is visible and have Dave say a couple of messages. Note that for completeness of this tutorial game, you could also add the iPod to the Inventory, and set it so that when you interact with the iPod whilst in the Inventory it replays the dog.avi video.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 46 of 71

Room 5: Downstairs an encounter with the Baron


We will now create Room 5. Room 5 is Daves lounge room. It is here that he first meets the Baron and engages in a conversation.

Create a new Room with the description Lounge Room. Import the image DavesLounge.bmp Create some walkable areas as well as a walk-behind for when Dave is walking down the stairs. (Dont forget the baseline for the walkbehinds) Walkable areas Walk-behinds

Open the characters node in the Project Tree and double-click on cBaron to open his properties. Set his starting Room to 5, his x location to 41 and his y location to 165. Also change Daves starting room to room 2. This will allow you to easily test your game without having to navigate all the other rooms.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 47 of 71

Changing Rooms
Dave needs to be able to access the Lounge room from the hallway. Therefore we need to create an event that allows Dave to leave Room 2 (hallway) and enter Room 5 (Lounge Room) Open Room 2 the hallway Dave will change to the Lounge room when he leaves the screen on the left i.e. he has walked down the stairs. We can manually define where the edges of the room are located. Open the drop down for the room and choose edges You will notice that there are 4 yellow lines that define the rooms edges. Hold the cursor over the left edge until it turns into a double-arrow and drag the yellow line to the right as shown below.

Open the Rooms events and select

In the room script add the following to the function

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 48 of 71

function room_LeaveLeft() { cDave.ChangeRoom(5,270,55); }

Test your game and enter the Lounge Room. Unfortunately the Baron is facing in the wrong direction. In the enters room before fade-in event for the lounge room add the following script
function room_Load() { cBaron.FaceCharacter(cDave); }

This script will make the Baron (cBaron) face (FaceCharacter) Dave (cDave).

OK, we now have characters, rooms, transitions, videos, inventory sorted. So on to one of the major components of adventure games, the dialogue between characters.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 49 of 71

PART 4 Time for a Chat

Conversations
A conversation in AGS allows the player to choose from a list of topics to talk about. Creating a Dialog Open the dialogs node in the Project tree. Double-Click on the available default dialog and change its name to dBaronTalk. Add the following responses in the Topic boxes. These are all the options that Dave can choose from. If they have a tick beside Show then the text will be displayed when the dialog first runs.

Dialog Script

Topics

When the player selects one of the options from a topic the topics dialog script is run. For example, if the player chooses Topic 1: What are you doing in my house? then @1 in the script will be run. Currently all scripts are blank. The @s entry point defines what happens when the dialog is first started. When Dave first starts the conversation with the Baron we want him to say Hello, who are you and what are you doing in my house? and the Baron to reply with I am the Baron This is the non-optional component of the conversation, the rest becomes player optional and uses the option text already entered. Add the following lines under the @s entry point.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 50 of 71

Important: Note that in dialog script we do not put the c in front of the characters name. Therefore cDave becomes Dave.
@S // Dialog startup entry point Dave: Hello, who are you and what are you doing in my house? Baron: I am the Baron return @1

Before we go test our game to see how this works, we need to invoke the dialog when Dave talks to the Baron. In the Project Tree open the characters node and double-click on cBaron. Open his events and select Talk to character Add the following command to the function
function cBaron_Talk() { cDave.Walk(103, 169, eBlock); cDave.FaceCharacter(cBaron); dBaronTalk.Start(); }

This will make Dave walk to the Baron and face him before starting the dialog when we use the talk-to mode on the Baron

Run your game and use the talk-to mode on the Baron The first thing that will happen is Dave will talk and then the Baron will reply as we defined under the @s entry point. Once the Baron has finished the return command signals the end of the @s script and the topics with the Show option selected are displayed. If you select one of the options. For example, option 1 What are you doing in my House? then Dave will say the text and @1 will run. Currently we have no script in @1 so nothing will happen once Dave has spoken.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 51 of 71

The Baron also uses the same speech colour as Dave so we need to change it by opening cBarons properties and changing the SpeechColour property to another number.

Finding Speech Colours You can easily find the colour of speech by opening the Colour node in the Project Tree and either typing a number into the Colour Finder and pressing Find Colour and selecting a colour. You can then type this number into the SpeechColour property.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 52 of 71

Add the rest of the script as shown. option-off turns a topic option off so that it will not be displayed option-on turns a topic on return returns the dialog to the script stop exits the conversation Run the game and test out the topics. Follow along with the script as you go so that you can see how it works. There are a number of commands which are available inside the dialog script to give the player inventory items, give points, change rooms, play sounds etc. We will use one of these to run a script which makes the baron move and disappear after the conversation has finished. That is, after a stop command.

// Dialog script file @S // Dialog startup entry point Dave: Hello, who are you and what are you doing in my house? Baron: I am the Baron return @1 Baron: I need your help Dave option-off 1 option-off 2 option-off 3 option-on 4 option-on 5 option-on 6 return @2 Baron: What have you heard? Dave: The dog told me to be careful of you Baron: Why would you believe a talking dog? option-off 2 return @3 Baron: OK, but be careful Dave stop return @4 Baron: I know a lot of things about you Dave. option-off 4 return @5 Baron: I hope you'll reconsider some time Baron Goodbye Dave stop return @6 Baron: On your way to town you will meet a stranger. Baron: You will learn more then. Baron: Remember the number '24'. stop return @7 return

At the moment, once the conversation ends Dave is left in the room with the Baron. We cannot add any commands after the dBaronTalk.Start() command that will run after the conversation has finished. The only way to achieve this is by running a script from the dialog itself. Before each stop command in the dialog add the following command.
run-script 1

This tells the game to run a global script called dialog_request in the global script and pass it the value 1.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 53 of 71

Open the global script globalscript.asc. Add the following function to the bottom of the script
function dialog_request (int bpass){ if (bpass == 1){ Wait(16); cBaron.Say("Goodbye Dave"); cBaron.FaceLocation(5, 150); while (cBaron.Transparency < 100){ cBaron.x-=1; cBaron.Transparency +=1; Wait(1); } StopDialog(); cDave.Say("WOW, that was freaky"); } }

When the run_script 1 command is run this function will be called and passed the value 1 as the parameter bpass. It then checks the value of bpass. If it is a 1 then the Baron says a message cBaron.Say("Goodbye Dave"), turns around cBaron.FaceLocation(0, 109)and then begins to move towards the door disappearing as he goes.

while (cBaron.Transparency < 100){ cBaron.x-=1; cBaron.Transparency +=1; Wait(1); }

Once he has disappeared Dave says WOW, that was freaky cDave.Say("WOW, that was
freaky").

When would you use something like this in your own game? You would use it when you want the NPC to leave after the dialogue event has completed. If you are happy for the NPC to remain and for the dialogue to be repeated if the player talks to the character, then you would NOT use this.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 54 of 71

PART 5 Let the game do some talking (Intro, Cutscene or Conclusion)

Cutscenes
Now that Dave has the information he needs we can send him off to town. Once Dave leaves his house he will meet a stranger on the way to town who will give him more clues. We will take the interaction away from the player here and create a cutscene. You could use a cutscene in your game either during the game to supply information which does not require any player interaction, or alternatively at the start to provide an introduction to the game, or at the end to conclude the game. IMPORTANT: If / when you use a cutscene as the ending for your game, make it very very good. It is after all the concluding finale, and the final reward to the player for having invested their time in playing the game. There is nothing worse than being let down by a crappy final ending cutscene. A cutscene is basically a sequence of actions over which the player has no control.

Create a new room and import the background streetwalk.bmp Create some walkable areas

Change the properties for the character cMax so that he starts in Room 6 at x = 202, y = 105.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 55 of 71

Exiting Room 5 The Living Room Create a hotspot on the living room door and set a WalkToPoint Add an event when Dave interacts with the hotspot and type the following room change command in the function.
function hLRoom_Door_Interact() { cDave.ChangeRoom(6, 16, 105); }

Add the script for Room 6 The Streetwalk


Open the editor window for Room 6 and go to the events window. Choose the enter room after fade-in event and add the following script.
function room_AfterFadeIn() { cMax.Walk (150,108); cDave.Walk(170,108, eBlock); cMax.FaceCharacter(cDave); Wait (5); cDave.Say("How you doin?"); Wait (2); cMax.Say("Not too bad, yourself?"); cDave.Say("Been better."); cDave.Say("My name is Dave"); Wait (5); cMax.Walk (150,118,eBlock); cMax.FaceLocation(150, 200); cMax.Say("I know"); cMax.Say("Dave, you are in terrible danger"); Wait (10); cDave.Walk (170,118,eBlock); Wait (10); cDave.FaceLocation(170, 200); cDave.Say("What do you mean?"); Wait (10); cMax.FaceCharacter(cDave); cMax.Say("When you get to town visit the General Store"); Wait (10); cMax.Walk (360,108); cDave.Walk (14,108, eBlock); //cDave.ChangeRoom(7, 100, 50); }

Note that the last line changes Dave to room 7 which we have not created yet. This is why it is commented out. When we add Room 7 we can simply uncomment the code by removing the // lines in front of it.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 56 of 71

To display a message before the script runs, go to the Events for the Room again and locate the First Time Enters Room event. Open the event and add the following to the function.
function room_FirstLoad() { Display ("Dave is on the way to town"); }

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 57 of 71

Additional Scripting Notes In AGS, all the interactivity in the game is handled using scripts. They're an essential part of game-making, so it's important that we go through how to use them. The Basics Right, we'll start off by doing something very simple - displaying a message to the player when they click the Look icon on a hotspot. Assuming that you've done the main AGS Tutorial, you should already have created a script that looks like this: function hDoor_Look() { Display("It's quite a large, ominous looking door."); } Let's look at this in detail. "function" tells AGS that this is a block of script code that will run when an event occurs. "hDoor_Look" is the name of that event. The empty parentheses ( ) tell AGS that this function takes no parameters (we'll come to them later). Finally, the curly brackets { and } define where this block of code starts and ends. Anything that you put between the { and the } will be run when this event is triggered. Display is the name of the built-in command that we're going to run. Then, in parenthesis we put the parameters to that command. Parameters are extra pieces of information that the command needs to run -- in this case, it needs the message that we want to display. Finally, we end the line with a semicolon. You use semicolons to tell AGS that it's the end of the command. Anything that you put after the semicolon becomes a separate command, and you'll usually do this on a new line. It's important to mention here that lines beginning with a double-slash // are comments - AGS will ignore them, they are just there to help you remember what your script is doing. You can add comments to your script simply by starting the line with the // symbol.

Commands Explained Each different command you can use in the script is also called a function. All the available commands are listed in the Scripting part of the manual, which also tells you how to use them. We want to display a message to the player, so we used the Display function. Looking this up in the manual gives us the definition: Display (string message, ...) Displays a message to the screen. It will be displayed in the standard message box, and centred in the middle of the screen. (description continues...)

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 58 of 71

The key point here is the part of the first line inside the brackets. This is called the parameter list and defines what parameters you give to the function. A parameter is some information that the function uses to decide what to do. Each parameter is listed, separated by commas. They can be one of the following:

string name This parameter is a string, ie. a piece of text. This means that for this parameter you supply text, surrounded by double-quotes. For example, "My text" . name is the name by which the parameter is referred to in the function description, it is not relevant for writing your script. int name This parameter is an integer, ie. a numerical value. This means that for this parameter you supply a number, for example 65 . CHARID This parameter is a character script name. You need to supply the script name of one of the game characters. InventoryItem* This parameter is an inventory item. You need to supply the inventory item you want to use. ... This parameter is optional, and can be of any type. You do not have to supply it at all, but if you do the function description will tell you what type of value to use.

So, we know that our Display function needs a string and an optional parameter. The description goes on to explain that the optional parameter is used for advanced things like displaying variable values, so we can ignore it for now. To make our script call the function we write its name, then the parameters inside brackets, and finally a semicolon. This is very important, as without the semicolon the script won't compile. Also, note that we DO NOT write the parameter type (eg. "string" or "int"). So, we can add this line to our script: Display("It's quite a large, ominous looking door."); This is what we already did in the main tutorial, but having examined it in more detail it should now make more sense why we've done it this way.

Instances The AGS script language is object-based. What this means is that many commands are operated on something in the game. You should not confuse object-based with room objects, they are entirely different things. There is a script instance for each character in the game, each object in the room, each hotspot, and so on.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 59 of 71

The name by which you access an instance in the script is given in the editor as the item's Script Name. For example, assuming our main character's script name is cEgo, then his instance name will be cEgo . To perform a command on something, you type its Script Name followed by a dot, followed by the command name. When you type the dot, the AGS Editor will automatically pop up a list of the available commands:

The script editor's auto-complete You then choose the appropriate command, and place any parameters in brackets, like we did above with the Display command. For example:

cEgo.AddInventory(iKey); This will add the Key to the EGO character's inventory. If you look up AddInventory in the manual, you'll find it takes two parameters, an InventoryItem * and an optional int. The InventoryItem* means that you need to supply the Script Name of the inventory item; this is something you can set in the script editor. The optional int means that there is an extra parameter which you can supply if you want to, but you don't have to. In this case it allows you to specify where in the character's inventory list the new item should be added, but we're not concerned with that right now. As well as all the individual character instances such as cEgo, there is a special instance called player. This always corresponds to the current player character, so if you just want to perform a command on the player character (especially useful in games where the player can control different characters) then you can use the player instance to do so.

Command Sequences Suppose we want the player to be given a pink poster when they look at the hotspot, as well as displaying the message. Assuming we have an inventory item set up to be the poster with the Script Name of iPoster (see the main tutorial for how to do this), the script enables us to easily make this happen. Our final script will look like this:

Display(It's quite a large, ominous looking door."); cEgo.AddInventory(iPoster);

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 60 of 71

Note that the script system is case sensitive, so writing for example addinventory(iposter); will not work. The script commands are processed from top to bottom in the order that you write them, so writing something like: Display("Why did the chicken cross the road?"); Display("Because he was bored."); will mean that the player gets the two messages in the order you specified.

Recap So, did you remember these vital points:


Strings are surrounded by double-quote marks. Semicolon after the closing bracket on each line. The script is case sensitive. To use an instance-based command, type the Script Name followed by a dot, followed by the command name. The player instance corresponds to the current player character.

Variables Scripts in AGS can use variables. A variable is an area of memory storage that contains a value, which you can check and change with your script. To declare a variable, you write the variable type, followed by the variable name, then a semicolon. The type is either "int", "String" (note the capital S) or "float", and the name can be anything you like - it is what you will use to refer to it later. For example: int myCounter; The variable name can only contain letters A-Z, a-z and the underscore _ character. You need to declare a variable before you can use it, so that the compiler can spot any mistakes and knows what type of things you can store in it. Initially, your variable will have the value 0. Optionally, you can set the starting value within the declaration, like this: int myCounter = 5; which would set it to contain the value 5 initially instead.
First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 61 of 71

Variable Scope An unfortunate side effect of the script's attempt to emulate the 'C' language is variable scope. In short, this means that you need to place your variable definitions OUTSIDE all the event handlers, otherwise their values will keep getting reset. So, to declare a variable for use by one of the room interaction scripts, you need to place the definition above the main function body. So, it should look something like this: // room script file int myCounter; (other event scripts) function hDoor_Look() { Display("It's quite a large, ominous looking door."); } (rest of file follows) No script commands can be used outside functions (or AGS wouldn't know when to run them!) - only variable declarations are allowed there. Changing variables You can change the value of a variable very easily in the script - simply write the variable name, the equals sign, then the new value, followed by the semicolon. So: myCounter = 10; will change the value of our variable to be 10. You can add to and subtract from a variable using the += and -= operators. So, to add 3 to the current value of myCounter, do the following: myCounter += 3; Checking variables Obviously we need a way to find out what value our variable contains, otherwise it's useless. We do this using conditional statements, called if statements. An if statement looks like this: if (myCounter == 5) { myCounter = 0; }

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 62 of 71

what this means is that if myCounter contains the value 5, then the script inside the { } brackets will be run (which in this case changes the value of myCounter to zero). If myCounter does not equal 5, the script inside the brackets is not run and execution carries on from after the } . Note the double-equals in the if statement. In an "if" statement, you ALWAYS use the double-equals operator, which compares the two values. If you used a single equals it would set the value instead, which will yield some strange results. The == is called an operator, because it performs an operation on the two values. The following basic operators are available:

== compares the two values, and proceeds if they are the same. != compares the two values, and proceeds if they are not the same. < compares the two values, and proceeds if the left hand value is less than the right hand value > compares the two values, and proceeds if the left hand value is bigger than the right hand value. <= proceeds if the left hand value is equal to or less than the right hand value >= proceeds if the left hand value is equal to or bigger than the right hand value

Putting it into practice Now let's do something useful with our variable. Suppose that we want to have different messages every time the player looks at the hotspot. So, the first time they look it will describe it, then if they look again they get a different message describing something in more detail. Our code will want to look something like this: if (myCounter == 0) { Display("You see a bookshelf."); } if (myCounter == 1) { Display("Looking closer, you see a book called Hamlet."); } if (myCounter == 2) { Display("There is also a book called Harry Potter."); } if (myCounter == 3)

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 63 of 71

{ Display("There is nothing else of interest on the shelf."); } if (myCounter < 3) { myCounter += 1; } myCounter starts off set to 0, so the first time this script is called it will run the first Display command, but not the others. Then, since 0 is less than 3, it will increase myCounter by 1, and since 0+1 = 1 it now holds the value 1. Once the player has seen all the messages (myCounter == 3), it no longer increases the value so if they click again they will keep getting the final message. Global variables made easy Sometimes, you may want to set a variable value that can be shared between a room script and your global script. There are two ways to do this -- you can export the variable from the global script and then import it in the script header, but that's a bit advanced for this tutorial. A simpler way is to use one of the 300 GlobalInts, which use the script functions SetGlobalInt and GetGlobalInt to access them. See their manual descriptions for more information.

Functions that return a value When reading function descriptions in the manual, you will notice that some of them say they return a value. For example, IsGamePaused () Returns 1 if the game is currently paused, or 0 otherwise. You use these much as you would use a literal value such as "9". For example, you can do: // Put the return value into our variable myCounter = IsGamePaused(); (OR) // Test the return value directly if (IsGamePaused() == 0) { myCounter += 5; } Be sure to remember the parenthesis ().

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 64 of 71

Common Shortcuts The script system has a few nice shortcuts for common tasks which you will find yourself using regularly. Firstly, the ++ and -- operators increase and decrease the variable by 1, respectively. So, the last part of our previous script could have been written: if (myCounter < 3) { myCounter++; } Also, the { } brackets are only needed if you are using more than one command inside them. Since we have only one command, the "my_counter++;" line, we can remove the { } completely and just be left with: if (myCounter < 3) myCounter++; However, this can lead to mistakes in scripts that are hard to spot, so I would advise always using brackets just to be safe. Finally, if you want to test whether a value is zero or not, you can just write it as follows: if (myCounter) Display("counter is non-zero"); which is equivalent to: if (myCounter != 0) Display("counter is non-zero");

Loops You may find it useful to have a sequence of commands that are executed more than once. AGS supports this, using the while keyword. Its format is virtually identical to if: int counter = 1; while (counter < 10) { counter ++; } This means that the body of the while statement is repeatedly processed, as long as the condition is true. So, in this case, since counter starts as 1, the loop will be run 9 times, since on the tenth time counter will be 10 and therefore it will stop.
First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 65 of 71

NOTE: be careful with while loops - it is possible to crash your game by using a badly written one. Consider this: int counter = 1; while (counter > 0) { counter ++; } Here, counter starts off as 1, so the loop will run. Then, it will increase to 2. Since it is still greater than 0, the loop will run again. This particular loop will run forever, since counter can never become less than 1, and your game will grind to a halt.

Multiple conditions On several occasions, you don't just want to decide what to do based on one variable - there may be a combination of things you want to consider. For example, suppose that you want to display a message if the player has two specific inventory items. You could do this, and put two if statements inside each other: if (player.HasInventory(iFirstItem)) { if (player.HasInventory(iSecondItem)) { Display ("You have both the items!"); } } However, this is quite unwieldy and if you have a lot of conditions it will look rather messy. So, AGS lets you do it this way: if ((player.HasInventory(iFirstItem)) && (player.HasInventory(iSecondItem)) ) { Display ("You have both the items!"); } This makes use of the && operator. Note that you need an extra pair of outer parenthesis, to contain the whole expression. Each part of the expression goes inside parenthesis, as normal, and they are joined with the && symbol. You can extend this to as many checks as you like. For example, this will display the message if the openedDoor variable is currently set to true, and the variable timer is between 5 and 10. if ((openedDoor == true) && (timer > 5) && (timer < 10)) {

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 66 of 71

Display ("It's all working out fine."); } 'OR' expressions Sometimes you will want to do some processing if one or another condition is true. For example, you might want to let the player open a door if he has either the door key or a chainsaw. if ((player.HasInventory(iDoorKey)) || (player.HasInventory(iChainsaw)) ) { player.ChangeRoom(10); } The operator here is the double-vertical-bar || operator. It works similarly to the && operator, but it will process the commands inside it if either one expression, or the other, or both, are true.

Doing one thing or another Sometimes, you want the script to take one course of action if a variable is set, and another course of action if it isn't. Initially, you might think you would do this: if (timer == 5) { // do something } if (timer != 5) { // do something else } That would work -- however, there is a neater way, using the else keyword: if (timer == 5) { // do something } else { // do something else } This also allows you to modify the variable inside the first block of code, without affecting whether the second block gets run or not. You can do as many tests as you like, using the else if keyword. So, a complete piece of code could look like this:

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 67 of 71

if (timer == 5) { // do something } else if (timer == 6) { // do something different } else { // do this if it's not 5 or 6 }

Your Own Functions You've probably noticed in the manual, it mentioning functions such as repeatedly_execute, and on_event, and how you can add them to your global script to do cool stuff. But you may be wondering, how exactly to go about it. Remember in tutorial 1, we learnt about function parameters and how they could be int, string, etc. Well, you write your own functions like this: function dialog_request(int param) { // contents of function go here } You start with the keyword function, then follow it by the function name, and then parenthesis listing the parameter types and names. For each parameter that you want, you need to write its type (int or string), followed by the name it will be known by inside the function. This name can be anything you like - it is similar to naming a variable. There are some fixed functions, such as dialog_request and on_event, which are part of AGS and therefore you MUST use the correct number and type of parameters. However, you may also add your own functions by naming them however you like, and having as many parameters as you need. Functions are useful if you have a block of script code that you need to use in two different places - putting it in a function instead means you don't have to copy & paste, and that if you modify it, all other script that relies on it gets updated too. To call your function from elsewhere in the script, just do it exactly like you call a built-in function - ie. just write its name, parameters then a semicolon. First of all, let's show a fixed function, on_event: function on_event (EventType event, int data) { if (event == eEventGotScore) {
First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 68 of 71

if (data == 5) { aSpecialScoreSound.Play(); } Display("You just got %d points!", data); } } With this script, whenever the player scores points, they will get a message telling them so. Also, if they happen to get 5 points at once, it will play audio clip aSpecialScoreSound. As you can see, you use the function parameters just like any other script variables. Our own function Now, suppose we have a special animation of the player doing a dance, and we want to be able to play it from various points in the script. By far the easiest way to do this would be to put it in a function: function do_dance() { cEgo.LockView(10); cEgo.Animate(2, 5); cEgo.UnlockView(); } This function runs view 10, loop 2, as the character's animation, waits until it finishes and then reverts to the default view. TIP: If you're wondering where to place your custom functions, just open up the global script (Game menu, Edit Global Script) and write them in there. The function must be outside all other functions. Now, elsewhere in your script, when you want the player to dance, just do: do_dance(); Returning a value You may have noticed that some of the built-in functions, such as IsGamePaused, return a value to the script. You can do this from your own functions, using the return keyword. So: function add(int a, int b) { int result; result = a + b; return result;

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 69 of 71

} This function adds the two numbers together and returns the result (a useless function in practice since the + operator does the same thing, but it demonstrates the point). Another part of your script could then do: total = add(5, 10); for example. Using functions from room scripts You may notice that when you add your own function to your global script, you can call it fine from other places in the global script but attempting to use it in a room script gives a parse error. The manual explains how to solve this using the script header.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 70 of 71

Conclusion
That essentially covers the key skills that you will require for the development of your game in this Unit. There are other areas that you can refine the look at feel of your game, and I encourage you to check out the other tutorial document Getting Started with AGS ver 3.0.1.doc Of particular interest, check out the available templates which you may feel better suit the GUI for your game. There are several available including ones which duplicate the Monkey Island interface. Before you get started on your own game, there are now several steps which you must undertake. 1. 2. 3. Creating your teams complete game script Overlaying backgrounds in Excel for pixel referencing Fleshing out full dialogue trees

These should be jobs that you allocated to individual members of the team.

Finally, for an additional resource on creating your first AGS game, refer to the document Getting Started with AGS ver 3.0.1.doc. (in the same folder as this document). It was created from the built in tutorial that comes with AGS and covers the same concepts but from the perspective of the original developer of Adventure Game Studio, as well as other areas like using different cursors and different fonts.

First AGS Game Tutorial Using Default Game template

Unit 10

Game Development 2

Page 71 of 71

Programming CODE Summary WHAT ACTION


Face a location Face a character Change Speech colour Set speech style Display a message Make a character talk Change room Pick up an inventory item Walk to a location Set a pause Change the background Check if player has used an inventory item Play a video Using variables Start a dialog Run a script Run global script dialog_request

TUTORIAL GAME EXAMPLE


cDave.FaceLocation(211, 53); cBaron.FaceCharacter(cDave); cDave.SpeechColor = 10; SetSpeechStyle(eSpeechSierra); Display("It's a picture of James Hetfield from Metallica"); cDave.Say("I Love this picture"); cDave.ChangeRoom(2,211,145); oCreditCard.Visible = false; cDave.AddInventory(iCard); cDave.Walk(180, 140, eBlock); Wait(40); SetBackgroundFrame(0); if (player.ActiveInventory == inventory[3]) { SetBackgroundFrame(1); } PlayVideo ("dog.avi", 1, 1); int playvid = 0; (GLOBAL) export playvid; (GLOBAL) import playvid; (ROOM) playvid = 1; (INTERACTION); dBaronTalk.Start(); run-script 1 function dialog_request (int bpass){ if (bpass == 1){ } } while (cBaron.Transparency < 100){ cBaron.x-=1; cBaron.Transparency +=1; Wait(#); }

GENERIC CODING REQUIRED


Character.FaceLocation(x,y); Character.FaceCharacter(Character); Character.SpeechColor = 10; SetSpeechStyle(speechstyle); Display("message"); Character.Say("message"); Character.ChangeRoom(Room,x,y); oCreditCard.Visible = false; Character.AddInventory(iCard); Character.Walk(x, y, eBlock); Wait(40); SetBackgroundFrame(#); if (player.ActiveInventory == inventory[#]) { SetBackgroundFrame(#); } PlayVideo ("video", 1, 1); int playvid = 0; export playvid; import playvid; playvid = 1; Dialog Name.Start(); run-script 1 function dialog_request (int parameter){ if (parameter == 1){ . } } while (character.Transparency < 100){ character.x -=1; character.Transparency +=1; Wait(#); }

Run a loop

First AGS Game Tutorial Using Default Game template

S-ar putea să vă placă și