Glue:GlueVault:Component Pages:Game Scripting Plugin:Implementation Tutorial

Preparing a project

The first step is to create a Glue project. This tutorial assumes you know how to do so, so we won’t go into project creation details. If you haven’t you will need to install the Game Scripting Plugin that you downloaded from GlueVault.

Once you have a project open, you can add the files to the project by clicking the “Add Game Script Core Classes” option under the Plugins menuAddGameScriptCoreClasses.png

Now your project should contain all of the necessary files to begin creating scripts.

Creating a Screen

Next we’ll create a Screen. Typically script classes are specific to a certain Screen, although some Screens may have multiple scripts. For example, a GameScreen may have one script for each level (Level1, Level2, etc).

For this example we’ll be creating a GameScreen class and a script for this class which will trigger scripts according to the position of a Sprite object. This example will be very simplified – typically games which require scripting tend to be larger.

To set up a Screen and the object within the Screen:

  1. Right-click on Screens and add a new Screen called “GameScreen”
  2. Right-click on the newly-created Screen’s Objects and select “Add object”
  3. Select “FlatRedBall Type”
  4. Select “Circle” as the type and click OK
  5. Select the newly-created object (CircleInstance)
  6. Set its “HasPublicProperty to “True”. We are doing this so that our script can access the Circle object from the script.HasPublicPropertyTrueCircleInstance.png

Creating a Script

Next we’ll create a Script class. To do this:

  1. Open your project in Visual Studio
  2. Create a folder called “Scripts”
  3. Right-click on Scripts and select “Add”->”Class…”
  4. Enter the name GameScript and click the Add button

Next we will create two interfaces and implement them in our class. The code should look like this when finished:

Why did we create the interfaces and properties?The reason that we did this is because we want to restrict which functions are available when using “If” and “Do”. To do this, we can create interfaces which the script will implement. With this approach, the “If” property will only provide access to decisions, and the “Do” property will only provide access to actions.

In other words, we did this to make writing scripts easier.

Passing references to our object

Since our script will need access to the GameScreen, we will create a constructor which takes a reference of the GameScreen. This will be stored locally. To do this add the following at class scope:

Now our scripts will be able to use the GameScreen.

Defining If’s

If’s are functions which control whether Do’s are executed. In other words, If’s are ultimately functions which return a bool. If the bool is true, then the contained Do’s will execute. If not, the If will be continually evaluated until it is true.

As mentioned above, to define an If, you will need to define it in two spots:

  1. In the IGameScriptIf
  2. In the GameScript class itself

For this tutorial we’ll create a script to check if the player has moved past particular points in the level. To do this we’ll check the X of the player – which in this tutorial is the CircleInstance in the GameScreen.

First, define the function in IGameScriptIf:

Notice that the function does not return a bool. The reason is because the function’s purpose is to create a delegate which will be called later.

Next add the implementation of this variable in the GameScript. To do this, add this function to the GameScript:

Notice that the delegate can contain anything so long as it is in scope and so long as the method returns a bool. This means that you can create methods which can perform essentially any type of test.

Defining Do’s

Creating Do’s is almost the same as creating If’s, except the delegate does not need to return anything since its purpose is simply to be executed.

For this tutorial we’ll create a Do which can print text to the screen using the Debugger class.

Just like before, first we need to add a method to the IGameScriptDo interface. To do this, add:

Next implement this method in the GameScript:

Creating the Script

Once you have defined If’s and Do’s you can start using them in your script. Of course more If’s and Do’s are usually added as the scripting system is used in a game, so no need to try to get all implemented at first.

To add script, add the following empty function:

Next you can add the following script to your Initialize function:

As you probably will be able to tell, this code will result in the text written above being printed out to the screen as the CircleInstance moves along to the right.

Testing the Scripts

Finally we’ll need to test the scripts. Since our script expects that the player is moving to the right (increasing X) well simply make the CircleInstance move by adding the following line in GameScreen’s CustomInitialize:

Next well need to add the GameScript to our GameScreen. To do this, first add an instance to GameScreen.cs at class scope:

Then we’ll instantiate it, passing the GameScreen instance (this) to the constructor:

Finally the script must have activity every frame. To do this, add the following line of code in the CustomActivity of GameScreen:

Seeing the result

If you run your game you’ll see the text update as the circle moves to the right.RunningScripting.png

Of course in a real game the character wouldn’t move on its own, but hopefully you can see how you would use this type of code to create your own scripts based off of player location, time, or any other kind of logic specific to your game.