Riding the Rails

Riding the Rails

Welcome again! If your scene is not already open, again, reopen it now. Now that we have the tracks in place, let's have learn to ride along them!

To start, let's name the track sections we laid down earlier. Giving the tracks names will allow us to find them more easily later.

    1. Select the first section of track in your path (order will matter!).
    2. Open the Properties window again (this window will contain the properties for whichever object is selected).
    3. Select the Search Properties tab (the second one, a magnifying glass over 5 blue cubes).
    4. Type a convenient name for the track object, such as "Track 1" and press enter.
    5. Repeat for all track sections, in order. Be sure to use a convenient, easy-to-remember name scheme, for example: Track 1, Track 2, Track 3, etc. Spelling, capitalization, punctuation, and spacing will matter, so be consistent and remember the names you choose. Naming them in numerical order will also make your life a lot easier in the end. Don't forget to press enter!

Now, let's dive in and create our first script! We'll create a main script for our game: this is the most important script of all, because it is the one that is responsible for the gameplay. For those familiar with programming, this script is the one that contains the equivalent of your "main" function.

    1. Click the Global Properties icon (single gear).
    2. Select the Script Properties tab (the second one, a green triangle in a circle).
    3. Click "Open Script Directory."
    4. In the directory that opens, create a new text file for the script. Name this file something that ends with the extension .lua, such as MyFirstGameScript.lua - be sure that you have file extensions turned on, so that your file does not accidentally become named MyFirstGameScript.lua.txt
    5. Open this file in any text editor.

Now we're ready to write some code! The first function we're going to create is called onSceneStart(). This function (a function is just a block of code that can do something) is always called by the Wild Pockets engine when the game is first started. Again, for programmers, we can thing of onSceneStart as our "main." This is the typical place to do all of our setup work and any preparation before the game starts. The function starts like this (copy and paste this code into the text file):

function onSceneStart()

Since this is a shooter on rails, we'll be controlling the camera to follow those rails ourselves, so let's disable the standard Wild Pockets camera. Add to the script:

Camera.setMode("Manual")

This will allow us full control over our camera. Next, we need to define a path for our camera to follow. We start by creating a new path:

myPath = Path.new()

This creates a new path and calls it myPath. Now, we need to fill this path with "waypoints," which are places the path will go. Each waypoint will be a position in space and a rotation that represents which way it is pointing. Fortunately, we have a series of tracks that will help us out. We will create one waypoint for each section of track that we laid when building the game. This is why we gave the tracks names, so that we can refer back to them. This sample code assumes that the first track segment in the path is named "Track 1" (if not, replace "Track 1" in the code below, both times, with the actual name of the first track segment).

myPath:setPosition(1, SceneManager.getObject("Track 1"):getPosition()+vec(0, 0, 3))
myPath:setRotation(1, SceneManager.getObject("Track 1"):getRotation())

This code creates a waypoint on the path. The first line sets the position of the waypoint, the second sets the rotation.

The first number in each line (1) indicates the waypoint number. The path is going to visit the waypoints in numerical order according to these numbers, so our first track section is assigned to waypoint 1. We then set the actual position of the waypoint, which will be 3 units above the position of the track segment (so that the camera does not slide along the ground). We retrieve the position of the track segment using getPosition(), and we add (0,0,3) to stay 3 units above the ground. We then set the rotation of the waypoint, which looks exactly like setting the position, only we use setRotation() and getRotation() instead of setPosition() and getPosition(). We also do not need to add (0,0,3), or anything at all, to the rotation: we simply want to be pointed in the same direction as the track is pointed. Note that we again refer to waypoint 1.

We now need to repeat this for each section of track. Repeat the same code, increasing the waypoint number, for each section of track you placed in your scene. Remember to increase the waypoint number and change the name of the track segment for both lines. For example, your next two lines of code will look something like:

myPath:setPosition(2, SceneManager.getObject("Track 2"):getPosition()+vec(0, 0, 3))
myPath:setRotation(2, SceneManager.getObject("Track 2"):getRotation())

Repeat this for every track segment in your scene, in order. Remember: the order that your track segments are travelled will be based on the numbers at the beginning of these lines of code. If you have lined everything up in the Builder, the camera will follow this path smoothly. If you are having trouble understanding this, look ahead to our completed sample pocket.

Now that our path is set up, we need to tell the path what will be following it. In this case, it is our camera that will be following the path, so that it will look like the player is riding the rails:

myPath:setTarget(SceneManager.getCamera())

Finally, having set up this path, we need to actually start it moving. To make the path start going, simply use:

myPath:go()

Now we finish up the onSceneStart function by closing it with the end statement.

end

That's it! Be sure to save this file, and then go back to the Game Builder. Now we just have to set this file to be our scene's script, and then we're done (if we have been following these instructions to the letter, the Global Properties window should already be open, and the Script Properties tab should already be selected).

    1. Open the Global Properties window
    2. Select the second tab, Script Properties.
    3. In the "Script File" section, click the button labeled "Set." NOTE: If you do not see the "Set" button, click on the red X to the right of the script name to clear the script already on the scene, then click "Set."
    4. In the box that pops up, enter your Wild Pockets username, followed by a slash, followed by the name of the file you just made, but do not include the .lua extension at the end. For example, "YourUserName/MyFirstGameScript" (without the quotes). Click "Okay."

Now we're ready to try out our changes. Save your scene (File menu, select Save). Then under the "Test" menu, select "Begin Test." A new window will open containing your pocket, and the onSceneStart function we just wrote will be run. If everything went well, the camera will follow the tracks we laid down earlier! If the camera goes off in a strange direction, double-check your script code and the names of the track segments to make sure they're all in the right order. We can also go back and redesign the course: move our track segments around or even add or remove segments, as long as we change the script to use the new list of tracks. If things still look weird, pay attention to the tracks - the side that looks like actual tracks should be face-up, and the camera will try to go in the direction of the arrows.

Congratulations! We now have a pocket with some action in it! In the next step, we'll add a little more challenge: some enemies to deal with.