Differences

This shows you the differences between the selected revision and the current
version of the page.


wmebook:ch3 2007/12/06 12:14 wmebook:ch3 2008/07/21 07:49 current
Line 1: Line 1:
====== 3. First tour into the real WME realm ====== ====== 3. First tour into the real WME realm ======
-So we now about the tools and we learned the basics of scripting and it’s the time to see how this knowledge applies to the game development. Before we start this tour let’s first make a brief overview of the file types you’ll be using in wme (and I strongly recommend using this naming convention):+We've found out about the tools and we've learned the basics of scripting and it’s the time to see how this knowledge applies to the game development. Before we start this tour, let’s first make a brief overview of the file types you’ll be using in WME (and I strongly recommend using this naming convention):
***.script** – are the files which contains the actual program ***.script** – are the files which contains the actual program
Line 58: Line 58:
Now this may look to you as an overkill script for something as similar as checking if we were in some scene or not but trust me, there’s more to it than meets the eye. This is actually very clever script which counts with many different possibilities. It counts with the game which was newly started, it counts with a scene revisiting, it counts with the fact that you can have on enter special events. Remember those cinematic moments when you played an adventure, walked in some room only to see the bridge collapsing? You wouldn’t want to see it collapse every time you visit that scene. Once is enough for sure. Now this may look to you as an overkill script for something as similar as checking if we were in some scene or not but trust me, there’s more to it than meets the eye. This is actually very clever script which counts with many different possibilities. It counts with the game which was newly started, it counts with a scene revisiting, it counts with the fact that you can have on enter special events. Remember those cinematic moments when you played an adventure, walked in some room only to see the bridge collapsing? You wouldn’t want to see it collapse every time you visit that scene. Once is enough for sure.
-So now when we uncovered what’s going on in this script, it’s actually very simple. As there are thousands of different methods, I’ve decided to choose the non violent way how to present them when needed.+So now when we uncovered what’s going on in this script, it’s actually very simple. As there are thousands of different methods, I’ve decided to choose the non violent way how to present them when needed, although Max was very unhappy with me last time, I've consulted nonviolent approaches.
Let’s look at one example from the beginning of the script. Let’s look at one example from the beginning of the script.
Line 161: Line 161:
</code> </code>
-As we see we’ve scratched surface of two objects so far. Actor object and Region Entity object, which we referenced by keyword **this** and hid it through setting its attribute Active to false.+We’ve scratched surface of two objects so far. Actor object and Region Entity object, which we referenced by keyword **this** and hid it through setting its attribute **Active** to false.
Let me return back to the image we’ve seen at the beginning and look at this very image from a developers point of view. Let me return back to the image we’ve seen at the beginning and look at this very image from a developers point of view.
Line 183: Line 183:
We’ll do something, which would demonstrate the power of WME now. Return to your warehouse scene in scene edit and let’s tweak it a bit. First we should create a new region and call it Exit. Position it to the left bottom corner as on the following image (you should immediately discover by now that it’s the red shape). Also note the position in the scene tree. If you put it above the floor, the mechanism presented wouldn’t work because the floor region would cover the Exit region and this way the Exit region would never get his voice. Last thing which remains to do is to attach a script to this region. Use an empty template and save the scene. We’ll do something, which would demonstrate the power of WME now. Return to your warehouse scene in scene edit and let’s tweak it a bit. First we should create a new region and call it Exit. Position it to the left bottom corner as on the following image (you should immediately discover by now that it’s the red shape). Also note the position in the scene tree. If you put it above the floor, the mechanism presented wouldn’t work because the floor region would cover the Exit region and this way the Exit region would never get his voice. Last thing which remains to do is to attach a script to this region. Use an empty template and save the scene.
 +{{wmebook:c5_3.jpg|}}
 +Before we go on, let’s discuss two more events (we already know Mouse related events). 
 +**on "ActorEntry"** occurs when actor enters the current region. This is the basic logic behind so called trap regions. Trap regions mean that actor triggers an event upon entering certain portion of the screen.
 +
 +**on "ActorLeave"** occurs when actor leaves the current region.
 +
 +Last thing we’d need for this example is taking the game out of player’s control. This is the vital part of successful game design. We want to trigger a scene and in the middle player clicks somewhere else, character didn’t finish the whole sequence and you’re stuck in an unsolvable loop. This kind of nightmare met many designers so let’s avoid it while we still can.
 +
 +**Game.Interactive = true / false;** Takes the game out of players control or returns it back. Always remember to return the game back to Interactive mode or your game will be stuck and players will get angry. So always think of the noninteractive mode as of a block.
 +
 +We’ll try to combine those ideas into our first more complex game logic.
 +
 +First we’ll adapt the **door.script** to look like this:
 +
 +<code script>
 +on "LeftClick" 
 +{
 + Game.Interactive = false;    // We want our player to make more things at once and we don't want to be interrupted.
 + actor.GoToObject(this);
 + this.Active = false;  // We disable the door so the hotspot is not visible or active anymore.
 + actor.Talk("Oh no. The door is welded shut.");
 + Game.Interactive = true; // Allow player to play some more.
 +}
 +</code>
 +
 +Then we open the **exit.script** and add a new event there. The file should look like this:
 +
 +<code script>
 +#include "scripts\base.inc"
 +
 +on "ActorEntry"  //Actor entered the Exit region
 +{
 + Game.Interactive  = false;
 + var door = Scene.GetNode("Door"); //We get the object corresponding to the scene name door into a variable named door
 +
 + if (door.Active) // If door was not clicked, it was not disabled, so this path will be used
 + {
 + actor.Talk("I don't want to return to my flat before I am sure I can't enter the warehouse!");
 + }
 + else //we know that the door is welded shut so we can leave the scene
 + {
 + actor.Talk("Oh well, there really is no way how could I enter that damn warehouse");
 + Game.ChangeScene("scenes\room\room.scene");
 + }
 + Game.Interactive = true;
 +}
 +</code>
 +
 +Okay. Time to explain what’s going on in here. Our thought process as a game designer was that player needs to enter the warehouse but the warehouse is welded shut. We for some reason need our player to discover the fact that the door is welded shut so the game could proceed. So we decided to allow him to change the scene only after he discovered that the warehouse was welded shut.
 +
 +Code-wise we have the region which would be normally used as an exit region, but we’ve added the condition that until the door region was active (which means that player didn’t click on it because if he did, the door region turns inactive), player can’t leave the screen.
 +
 +Save all scripts and run the game. Test the game thoroughly so you’re sure that you really understand the coding behind this logic. It’s one of the critical aspects of adventure game designing and if you get this right, you’ll be in no time creating your own adventures.
 +
 +**Important:**
 +Critical thing to know is how path in wme works in general. We’ve seen in **Game.ChangeScene("scenes\room\room.scene");** that we didn’t start with the data path. That’s totally correct! Write it thousand times – I’ll never include the root package name in the referenced path. Why? Because this would work only in debug mode, and when you compile your game in the packages and send it around, it won’t work. The same applies to all files (images, sounds etc.). Never reference files outside of packages. They’ll work in debug mode, but they won’t get compiled and you’ll be in it for the worst nightmares. Think of package as of a virtual root directory and all references should be starting from that point. More about that in the chapter about packages.
 +
 +Let’s move on to the last part of this chapter, which will cover the daemon issue. We’ll start right off with our first daemon. Before we explain how it works, we’ll have to take a look at a few new methods:
 +
 +**Game.AttachScript(filename);** - Attaches a new script file to the Game object, making it global for the whole game. This script is attached until it’s detached or ends.
 +**Game.DetachScript(filename);** - Detaches script from the Game object.
 +
 +**Scene.AttachScript(filename);** - Attaches a new script file to the current scene,  making it active until player doesn’t change the current scene, or script is detached or ends.
 +**Scene.DetachScript(filename);** - Detaches script from the Scene object.
 +
 +We can of course attach script to the scene object in the Scene Editor or script to Game object in the Project Manager, but we want our script to be attached in the middle of the gameplay.
 +
 +Next command we’d learn is totally necessary for the daemon writing. It’s command Sleep.
 +**Sleep(time);** - Pauses the script for certain amount of milliseconds.
 +
 +Sleep serves two purposes. First - it lets the script wait for certain amount of time and second - it hands the program flow to other threads. Here’s the typical daemon pitfall:
 +
 +<code script>
 +var a = 0;
 +while (1) // infinite loop, the same as while(true)
 +{
 +  a = a + 1;
 +  if (a > 100) a = 0;
 +}
 +</code>
 +
 +**Never do this. It’ll halt your computer. Script takes over the whole processing power and the rest of the game never get control back. This way also Windows never gets control back which results in hung up computer.** So how to fix this?
 +
 +<code script>
 +var a = 0;
 +while (1)
 +{
 +  a = a + 1;
 +  if (a > 100) a = 0;
 +  Sleep(1);
 +}
 +</code>
 +
 +This daemon is correct (remember our previous chapter), because it hands over the control from the script. One millisecond is here only formally. The important fact is that you DO hand over the control.
 +
 +So enough theory and let’s make some daemon fun with the commands, we’ve just learned. Oh wait. We’d need one more command:
 +
 +**Game.Msg(text);** - although we’ve seen it before let’s recap that it displays an onscreen debug message. Very useful command.
 +
 +What we’re going to do is a funny little timed sequence, which occurs in our warehouse scene. The scenario is as follows. Player examines the door to discover that there’s a time bomb attached to it. If he can’t exit quickly enough, the Scene is reloaded (it’s the hardcore-adventure-fan-way how to tell the player, that he has died).
 +
 +First we’ll create the new script in the scr folder (from the empty template) and name it **bomb.script**.
 +
 +Tip: if you get more skilled, you can add the new files manually by creating a text file with a script extension created in the corresponding folder.
 +Then we modify the **door.script** so it’ll read the following:
 +
 +<code script>
 +#include "scripts\base.inc"
 +
 +on "LeftClick" 
 +{
 + Game.Interactive = false;    // We want our player to make more things at once and we don't want to be interrupted.
 + actor.GoToObject(this);
 + this.Active = false;  // We disable the door so the hotspot is not visible or active anymore.
 + actor.Talk("Oh no. There's a timed bomb attached to the door! I have to find an exit before it explodes.");
 + Scene.AttachScript("scenes\warehouse\scr\bomb.script");
 + Game.Interactive = true; // Allow player to play some more.
 +}
 +</code>
 +
 +I’ve hilighted the line which attaches our new script and thus starting the countdown.
 +Now let’s look at the **bomb.script** contents.
 +
 +<code script>
 +#include "scripts\base.inc"
 +
 +for (var timer=6;timer>-1;timer = timer -1)  // let's set up a loop which would go down from 6 to 0.
 +{
 + Game.Msg("Countdown: " + timer); //Let's display how much time do we have left.
 + Sleep(1000); // Sleep one second (and also hand the game over to other threads)
 +}
 +
 +Game.Interactive = false; //Death cutsene
 +actor.Talk("I've just died. Let's try again");
 +Game.ChangeScene("scenes\warehouse\warehouse.scene");
 +var door = Scene.GetNode("Door");
 +door.Active = true; // If we die, we need to return the door to its active state or we'll never discover the bomb again.
 +Game.Interactive = true;
 +</code>
 +
 +And the last change we’ll make is to the **exit.script**. If we found an exit in time, actor will be able to escape, but what if he arrives there in the nick of time and while talking his line, the time runs away and he dies? So let’s prevent this from happening by detaching the bomb script and letting him say something more reasonable.
 +
 +So the contents of the **exit.script** would look like this:
 +
 +<code script>
 +#include "scripts\base.inc"
 +
 +on "ActorEntry"
 +{
 + var door = Scene.GetNode("Door");
 + Game.Interactive = false;
 + if (door.Active)
 + {
 + actor.Talk("I don't want to return to my flat before I am sure I can't enter the warehouse!");
 + }
 + else
 + {
 + Scene.DetachScript("scenes\warehouse\scr\bomb.script");
 + actor.Talk("Phew. That was close");
 + Game.ChangeScene("scenes\room\room.scene");
 + }
 + Game.Interactive = true;
 +}
 +</code>
 +
 +Save everything and test our little scheme to see some more of the effects.
 +
 +On closing of this chapter I’ll introduce a little change to our scheme. This change is not necessary in our example, but it will be necessary in different case. For now we’re testing, if the entity region Door is Active for another decision. But what if we need to apply the similar logic to more than one scene? If we stand in different scene, we logically can’t use entity this way because it’s not anymore part of the current scene.
 +
 +So what we can do is using a global variable. Let’s make a change to our scripts then!
 +Last code listing for this script triplet is then (changes in bold):
 +
 +**bomb.script**
 +<code script>
 +#include "scripts\base.inc"
 +
 +for (var timer=6;timer>-1;timer = timer -1)  // let's set up a loop which would go down from 6 to 0.
 +{
 + Game.Msg("Countdown: " + timer); //Let's display how much time do we have left.
 + Sleep(1000); // Sleep one second (and also hand the game over to other threads)
 +}
 +
 +Game.Interactive = false; //Death cutsene
 +actor.Talk("I've just died. Let's try again");
 +var door = Scene.GetNode("Door");
 +door.Active = true; // If we die, we need to return the door to its active state or we'll never discover the bomb again.
 +global doorClicked = false;
 +Game.ChangeScene("scenes\warehouse\warehouse.scene");
 +Game.Interactive = true;
 +</code>
 +
 +**door.script**
 +<code script>
 +#include "scripts\base.inc"
 +
 +on "LeftClick" 
 +{
 + Game.Interactive = false;    // We want our player to make more things at once and we don't want to be interrupted.
 + global doorClicked = true;
 + actor.GoToObject(this);
 + this.Active = false;  // We disable the door so the hotspot is not visible or active anymore.
 + actor.Talk("Oh no. There's a timed bomb attached to the door! I have to find an exit before it explodes.");
 + Scene.AttachScript("scenes\warehouse\scr\bomb.script");
 + Game.Interactive = true; // Allow player to play some more.
 +}
 +</code>
 +
 +**exit.script**
 +<code script>
 +#include "scripts\base.inc"
 +
 +on "ActorEntry"
 +{
 + var door = Scene.GetNode("Door");
 + Game.Interactive = false;
 + global doorClicked;
 + if (!doorClicked)
 + {
 + actor.Talk("I don't want to return to my flat before I am sure I can't enter the warehouse!");
 + }
 + else
 + {
 + Scene.DetachScript("scenes\warehouse\scr\bomb.script");
 + actor.Talk("Phew. That was close");
 + Game.ChangeScene("scenes\room\room.scene");
 + }
 + Game.Interactive = true;
 +}
 +</code>
 +
 +We’ve seen in this chapter some neat tricks with the way how we can handle the game logic. We’ve also learned some of the important concepts for the tying scene to the code and we are able to use a couple of very basic commands and object methods. But for now we all the time used a lot of prebuilt demo code. Next chapter is all about starting the project from scratch. We’ll build upon this blank project until we create a little feature packed game. So stay tuned, we’re getting into it.
 
wmebook/ch3.1196939658.txt.gz · Last modified: 2007/12/06 12:14 by metamorphium
Recent changes RSS feed Creative Commons License Driven by DokuWiki