Scripted, The Story of Events

So we’ve seen a lot of great art, great design work, and even learned about our new voice actors. Something I want to throw at you guys today is some great C# code from the depths of Unity.

The point of the script I’ll be showing off is to create scripted events in a game world. Basically we’ll be taking away the players ability to move so that we can lead our player the right way during critical parts of the game.
As far as actually doing it, all we need here to start is a class to handle the input. We’ll call this ‘InputMaker.’ Inside of this class we’re going to create a static instance of the class itself, so that we have something to call from.


public class InputMaker
{
    public static InputMaker myInput;
}

Great start right? So the next part is creating arrays that contain everything about our inputs for our game. In my case, because our character moves around the level, I ended up putting four different arrays here. You can look at the example below.

You’re also going to notice that I create a bool called playerInput. We’re going to use that to tell our game if the player is doing input, or if we are making input for the player!


string[] inputNames = {"ActionOne", "ActionTwo", "ActionThree"};
bool[] inputKeysOn = {false, false, false};

string[] movementInputNames = {"Horizontal", "Vertical"};
float[] movementValues = {0, 0};

int checkNum;

public bool playerInput = true;

Obviously these are all just an example, and go right below the static myInput we created before. To break everything down, the inputNames and movementInputNames are going to be lists of all of your inputs. The first one will be all of your button presses, so any keyboard buttons or controller button input you have. For the movement array, that’s going to be things like a joystick or a mouse that you have that needs to be moved around. That’s why we set the movementValues array to contain floats, because for us movement isn’t just on or off, we have different variants of speed that it can go at.

Now that we have these values, it is time to use them! What we’re going to do next is create some methods to be able to set whether or not we want these keys activated. That way in a script we’re able to call these methods and force our player to have some input.


public void SetKeyOn(string inputName)
{
    for(int i = 0; i < inputNames.length; i++)
    {
        if(inputNames[i] == inputName) checkNum = i;
    }

    inputKeysOn[checkNum] = true;
}

public void SetKeyOff(string inputName)
{
    for(int i = 0; i < inputNames.length; i++)
    {
        if(inputNames[i] == inputName) checkNum = i;
    }

    inputKeysOn[checkNum] = false;
}

public bool GetKeyOn(string inputName)
{
    for(int i = 0; i < inputNames.length; i++)
    {
        if(inputNames[i] == inputName) checkNum = i;
    }

    return inputKeysOn[checkNum];
}

So these three methods will work for the simple button presses, when using the float values you’re going to need to pass in float values for the Set, as well as return a float for the Get.

Now, something I recognize is that having a SetKeyOn AND SetKeyOff can seem a little redundant. However, it doesn’t affect runtime to badly, and as well it makes things easier to trace in the long run. We’ll be able to debug each thing separately.

The last big thing we’ll need to do before actually scripting our events is making changes to our player class. Here I’ll just show a simple example that hopefully you’ll be able to use in your own games! Here we’ll look at what happens when we’re trying to get input from our ‘ActionOne’ button being pressed. Normally it would look like what we have below here.


if(Input.GetButtonDown("ActionOne")
{
    //do stuff...
}

With our system the if statement is going to get a little bit longer, but we’ll have a larger area of control overall with our scripted events.


if((Input.GetButtonDown("ActionOne") && InputMaker.myInput.playerInput) || (InputMaker.myInput.GetKeyOn("ActionOne") && !InputMaker.myInput.playerInput))
{
    //do stuff...
}

So now we’re checking who is in control, and if any input is even trying to pass through anyway.

Now, the last thing we need is an actual script to make everything work! So create a simple MonoBehavior script, and inside here is what we do.


public class PressButton : MonoBehavior 
{
    int currentStep = 0; // This will keep track of what command needs to happen.
    float timePassed = 0; // This will give us time to pass, if needed.

    void Awake()
    {
         InputMaker.myInput.playerInput = false;
    }

    void Update()
    {
        time += Time.deltaTime; // We increment time, important for 
                                // movement commands.
        
        if(currentStep == 0) // Check the current step.
        {
            InputMaker.myInput.SetKeyOn("ActionOne"); // Here we set the action on.
            currentStep++; // And finally we increment the step, so that we can 
                           // use more steps moving forward!
        }
    }

}

And there you have it! You will now be able to use Unity to make some great scripted events of your own. I hope you were able to get some good information out of this post, and I’m excited for you to check out all of our scripted events inside of Building 37, being released this May. Here’s a sneak peak at one of our events the player will go through.

Untitled-1

– Austin Stewart, Lead Programmer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s