Adding a screen resolution option

Adventure Creator has built-in support for basic options such as audio levels and subtitles. However, when building a game for PC and Mac, it's likely that you'll want to add further options - particularly graphical ones.

In this advanced tutorial, we'll add one of the more common graphical options - the screen resolution. We'll be making use of Menus, Variables, as well as some scripting. The scripting will be quite basic, but it is still recommended that you have a basic knowledge of coding principles beforehand - though the complete script is still shown within.

We'll have three options available - 640x480, 800x600 and 1280x960. So that AC can remember the player's choice, we'll make use of a Global Variable.

We'll begin by going to the Variables Manager, and adding a new Global Variable.

Rename this Variable ScreenResolution. Since we have three options, we'll set this Variable's Type to Integer. It's value will be either 0, 1, or 2 - depending on the chosen resolution. Since we've set our default resolution to 800x600, we'll need to set the Initial value to 1.

Finally, we want to set the Link to field to Options Data. This will keep the Variable's value stored separately from save games, so that it only need be set once by the player.

Make a note of the Variable's ID number, as we'll need this in the next step. The ID number is listed to the left of it's name in the list.

Now we're ready to amend our Options Menu to include a resolution option. This tutorial assumes you already have an Options Menu to work with - if you don't, you can simply copy one from the Demo_MenuManager asset file, which looks like this:

Open the Menu Manager and select your Options Menu. Under the list of elements, choose an Element type of Cycle and click Add new.

A Cycle element will let the player choose between multiple values - in this case, the various resolutions. Rename it ScreenResolution, and re-style it so that it fits in with the rest of the menu:

The Cycle's value will be linked to the Variable we made earlier, so set the Cycle type to Variable, and enter the ScreenResolution Variable's ID number as the Global Variable ID. In this case, it's 1.

We have three choices, so enter 3 into the Number of choices field. Three choice options will appear beneath - this will be the display text that corresponds to each Variable value. Enter the three resolution choices into the fields:

If you test the game now, you'll see that the Cycle's default option is "800x600", and that we can cycle between the three resolutions by clicking on it. And since the value is saved in our PlayerPrefs, the change is remembered even after exiting and re-playing the game. However, clicking the Cycle will have no effect on the actual resolution - to change that, we're going to have to create a custom Action.

The easiest way to create a new Action is to duplicate ActionTemplate.cs. This file can be found within Assets -> AdventureCreator -> Scripts -> ActionList in the Project window. Duplicate it, rename it to ActionApplyResolution, and move it to a new empty folder.

An error will appear in the Console, because the code inside the new script still uses the same "class" name as ActionTemplate, so we'll need to fix that. Open the script file and change both instances of ActionTemplate to ActionApplyResolution. These will be on lines 23 and 29.

Next, we need to give it a new title. This is the name that will appear in any list of available Actions. Change the title string on line 32 to Engine: Apply resolution.

This Action is very simple, and won't involve any settings to change, or GUI elements in any Editor, so we only need to write code in the Run() function, which is called when the Action is run.

Remove everything from lines 38 to 61 inclusive, so that the Run() function is empty:

When this Action runs, we want to read the value of our ScreenResolution Global Variable, and set the game's resolution accordingly. So first, we'll define a new integer, resolutionSetting and set it to the Global Variable's current value:

Where 1 is the ID number of the Variable.

We'll now set the screen resolution to one of our three possible sizes, according to it's value:

(Note that the "true" argument will set the game to full-screen. To add a full-screen option to your game, you will need to override this with a new Global Variable in the same way).

Because AC's Menus are, unless set otherwise, proportion to the screen size, changing the resolution means we will need to re-calculate them. This can be done with the following command:

AC.KickStarter.playerMenus.RecalculateAll ();

The Run () function requires a float to be returned. This represents how long the Action takes - in the case of instantaneous Actions such as this, we only need to return zero:

return 0f;

And the complete Run() function:

Our Action is now finished - we now just need to plug it into our Actions Manager, so that we can use it in our ActionLists. Save the file, and go the Actions Manager. In the Custom Action scripts panel, click Set directory and point the window to the directory where our new Action is stored. Be sure that the directory contains nothing but custom Action scripts. Then click Refresh list, and you'll find the new Action's title listed with the others:

We're now ready to call this Action when the "Apply" button is clicked. We'll do this by placing it in an ActionList asset, that can be called in any scene. Choose an appropriate folder in your Project window, and choose Create -> Adventure Creator -> ActionList asset.

Rename this asset to Set resolution and view it in the Inspector window. Click Add new Action once to create a new Action, and set the Action type to Engine: Apply resolution.

Now return to the Menu Manager, select the ScreenResolution cycle element, and assign the Set resolution ActionList as the ActionList on click.

Now when the Cycle is clicked on, the screen will set itself to the player's chosen resolution. We now just need to ensure that the game keeps this resolution when the game re-starts. We can do this by setting the Set resolution asset as our game's ActionList on start, as listed in the Settings Manager:

Adventure Creator and this website are copyright Chris Burton, ICEBOX Studios

Go to top