0% found this document useful (0 votes)
106 views47 pages

Text Adventure Game

This document discusses creating a text adventure game in Unity. It covers creating UI canvases and text elements, adding scripts to manage game states and story text, and using ScriptableObjects to store game data like starting states outside of scripts. ScriptableObjects allow mounting data as assets independently of scripts. Methods are explained as reusable blocks of code that can accept parameters and return values to perform actions. Public and private access modifiers determine whether methods and variables can be accessed from other scripts.

Uploaded by

Hanz Osida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views47 pages

Text Adventure Game

This document discusses creating a text adventure game in Unity. It covers creating UI canvases and text elements, adding scripts to manage game states and story text, and using ScriptableObjects to store game data like starting states outside of scripts. ScriptableObjects allow mounting data as assets independently of scripts. Methods are explained as reusable blocks of code that can accept parameters and return values to perform actions. Public and private access modifiers determine whether methods and variables can be accessed from other scripts.

Uploaded by

Hanz Osida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

TEXT ADVENTURE GAME

In this lesson we are going the learn:


• Unity Game Objects
• Game States
• Unity Scriptable Objects
• Methods with Return Types
• Arrays
• Loops
• Publishing the Game
About the Game:
It is simple text adventure game, and by creating this, we’ll be understanding more
about state machines, and how different states can lead to other states.
Gameplay Screen:
3 components of gameplay screen:
1. Game Title – which consist of the title of your game.
2. Story Text – a brief description of your game
3. Options – options on what to do with your game
Core Game Design
Player Experience – feeling of discovery.
- The player experience is something that we all shoot for in game design. That's
because the player experience is what the player goes through when they play the
game. That experience can be as simple as gamified mechanics in gamification; a
games-based learning class; a simulation; or a serious game.
Core Mechanics – choose your own adventure
- Core mechanics refer to the action of play in a game. These patterns result from the
interaction between sprites. A game’s core mechanic is the action of play: the activity
players do over and over again in the game, like jumping, collecting, flying, or shooting.
Theme – A game theme refers to the subject matter the game is built around. A theme
could be very abstract, like “shapes” for example. A game could also be very story
driven and have a theme like “mystery.” Themes are a great way to find other games
with a similar subject matter to games you like.
Core Game Loop – player is shown text on what is happening in the world and given 1-
3 choices on how to progress.
- A core gameplay loop is a gameplay loop in which a highly specified set of actions
undertaken by the player, most often the main actions defining the game. In Call of Duty
or Battlefield, the core gameplay loop is more or less targeting enemies, shooting them,
and seeking out new enemies

What are Story Hooks?


- A hook. It's a term used in all sorts of media. Basically, it's either a summary where
you can explain what is so good about your game (a sales pitch), or it's something that
happens early in the (in this case) game that makes people want to keep playing.
Example:
Challenge: Create your OWN Adventure!
• What is your game theme?
• What is an image that sums up your game theme?
• Who is the player?
• What is the goal?
Examples: https://community.gamedev.tv/tag/1_TX_CUD
Creating the Game
Templates: 2D
Project Name: <Surname>Text101
Location: <to your desired location>

UI Canvas and Text


What is UI?
- UI or also known as User Interface, is anything a user may interact with to use a
digital product or service.
- Examples: buttons, text, menu
In unity, UI lives on the “canvas”
- canvas is overlayed on top of the game
What is UI Canvas?
- The Canvas is the area that all UI elements should be inside. The Canvas is a
Game Object with a Canvas component on it, and all UI elements must be children of
such a Canvas.
Creating UI Canvas in Unity:
Creating a Canvas UI is very easy in Unity! You simply just have to right-click in your
Hierarchy, scroll down to UI, and select Canvas. Just like that you’ve created a layer for
all your UI elements to appear on!
Double click on the Canvas and we’ll see it in its entirely

You may also want to drag the Game Scene window side by side with the Scene
window to see what is happening.
To fix the ratio of our game, Go to Game Scene Window and click on the Free Aspect
Tab, select an Aspect, or we can set our own. To do that, click on the “+” button.

Set the Label to “1080” and Width & Height to 1920 x 1080.

Now let’s add some Text. Right-click on the Canvas and select UI, then choose Text.
You can change the properties of the Text by going to the Inspector. Example Changing
the Text Color to White.

You may also want to change the Font Size to 40 or bigger size.
Now that we’ve changed it to 40, we can’t see it at all. That’s because it doesn’t fit within
our text box. So, drag the sides of the text box to make it bigger.

Continue to drag them according to the Game Design.

Like this …
It doesn’t need to be perfect for now, we just need to get the general layout of the Text.
Next, always rename our Game Objects (Text), so it makes sense.
Scroll up to the top of Inspector, and instead of just plain old text, Rename it to the Story
Text.

On the Text (Script), we’ve just got placeholder that says, New Text.

Let’s make a whole bunch of placeholder story text just so we can see how it fits on the
screen. You can always adjust the Text size if necessary.
Now let’s add more Game Object.
Again, on the Canvas, right-click, then select UI and choose Image.

and you see a small white square


Drag the side of that so it fits a little bit bigger than where the Text at.

Then, change its color.

Finally, drag the Image at the top of the Text from the Hierarchy, to send the Image to
the back of the Text.
Challenge: Get your Canvas ready
• Create a text field for your story
• Create a heading
• Optional: Add some flair/ match your theme.
Update Text Component
Here, we are going to add Text, programmatically. So that, when we hit Play, the text is
fade in from our script and it is put within our game.
Now we want a script where we can put our game logic that says instruction to the
player. And when we create a script, it needs to be attached to a game object. They
need to be component of a game object.
To do that we need to create an Empty Game Object. Right-click on the Hierarchy, and
choose Create Empty, then rename it to Game.

What is Game Object?


A game object generally represents a particular object or element in your game that can
emit a sound, including characters, weapons, ambient objects, such as torches, and so
on. In some cases, however, you may want to assign game objects to different parts of
an in-game element.
Components can have properties and it consist of two things:
• Values – anything that describes a component
• References – the source of a component

In our Game Empty Object, we got values such as Transformation properties.

And so, every game object has a transform because we need to know where in the
world it will be placed. It’s a good practice, when we create an empty game object, is to
reset the position back to zero.
Or right click on Transform and choose Reset.

Next, we are going to add a Script Component, click on the Add Component Button and
scroll down to New Script.

Or just search on the name of Script, example “AdventureGame”, then choose New
Script
Then, click Create and Add Button

You can see, after we do that, down in out Asset directory, we now have the new script,
with AdventureGame as the name.

Double click on that to open your default Text Editor.


Create a variable Text for our Text Game Object, name it textComponent.
Then import UnityEngine UI for us to use the User Interface namespace.

In the front of Text variable type in [SerializeField]

That means, we have now this available within our Inspector, so that we can make
changes in there.
With that, we want our Story Text to be its Reference, so to do that. Click on the
Selector.

And choose Story Text.

Or you can just drag it there.

We now have Story Text as a Reference


On you Script type in the following to print something on the Story Text on Runtime.

Save it and Play the Game.


Game States
In order for us to create an interesting text adventure game, we're going to need a lot of
text and a lot of adventure, to state the obvious.
To do that, we are going to need to organize our bits of information, our pieces of the
story to give to the player.
We're going to need a good way to organize our states.
A state is an action or a process or a behavior or the thing that we are doing usually.
A state machine assumes only 1 state at a time. A condition (requirements) to
transition from one state to the next.

In our game, we may have 100s of states


Each state may have many lines of story
Need a way to manage all data

Challenge:
• What is the first thing that your player is confronted with?
• Write 2 or 3 sentences that will start your game.
• What are the 2 choices your player can take?
Scriptable Objects
• ScriptableObject is a class that lets us store data in stand alone assets.
• Keep mountains of data out of our scrpits
• It is lightweight and convenient.
• Used as a template for consistency.

Create another script file within our Assets, name it States

Open it and replace its inheritance from MonoBehaviour to ScriptableObject


Delete both methods and type in a string variable name “storyText”, this will be our
starting scene. Serialize it.

Above the class name, type in the following code:

This will add a ScriptableObject “States” menu to our Asset.

Now create a ScriptableObject name, StartingState.


Note that in the Inspector, there is our Story Text field, the one the we created from the
Script.

If we want to convert this to a TextArea, so we can type a paragraph, add the following
code to our storyText variable.

The first number (14) is the minimum side visible in the Inspector and the second one is
the number of lines (10) before the scroll will appear.
Public Methods and Return Types
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as
functions.
• Why use methods?
o To reuse code: define the code once, and use it many times.

Public vs. Private


Essentially, making something public makes it global that you can access anyway in
your code.
Now let's talk about return types, if we have void, which is what we've been using, so
private void StartGame that means there is no return type.

However, if there is a return type then the method is going to execute all of the things in
this curly brace and then it's going to say, hey, here's a piece of data for you back to the
place where it was called from.
In our Scriptable Object States, create a public method called GetStateStory that a
return type of string.

This means that the method will return whatever is current within storyText.
So, this is our end goal is to return that information into our text box in here.
Next, is to execute this particular method by going to AdventureGame script, typing the
following code:

Then, on the Inspector, drag the StartingState Scriptable Object to the Starting State
Reference of the Game Empty Object.
To know continually know what state were in, add the following code to the
AdventureGame script.

Save and click on Play.


Creating an Array
An array stores into a variable, multiple times of the same type.
Example:

Type / Data Type


- this is an integer, but it can be a float or string. We use the square brackets to
denote that this is an array.
Name
- variable name
Values
- This variable is going to store these particular numbers, these particular values.
- it will store them at specific locations, called index that starts at 0.

On the State script, what we want to do is have a variable which stores all of our next
states.
To do that create 2 more State. Name them Room 1 and 2 (for the moment)

On the Room 1, just type something like, “I am in room 1!”

Then, do the same in Room 2.


Going back to States script, type in the following codes:
Serialize Array of type State called nextStates
Public method that returns nextState called GetNextStates

Return type needs to be the same as variable type.

Note that in the Inspector of StartingState, we have now a field Next States

Type in the 2 for the size of our States array (click on the dropdown icon to see
Elements field).
Then drag Room 1 to Element 0 and Room 2 to Element 1

Manage Next States


Create a little loop with the States that we’ve got at the moment.
On Room 1 State, drag Room 2 to the Element 0 and StartingState to Element 1.

And from Room 2 State, drag Room 1 to the Element 0 and StartingState to Element 1.

On the AdventureGame script, type in the following code:

This will call the GetNextState() from the State script, that will return an array of State
(Room 1, Room 2, or StartingState)
Then, this will update the value of state if the player press number 1.
Challenge:

Then, we need a way of presenting the display to the player, within our text field. To do
that add the following code:

Finally called the ManageState method within the Update method.

Save it and click the Play button to test the game.


StartingState
When press 1.

When press 2
Loop
• Repeated event until condition is met
• Very useful when counting or iterating
• One type of loop is For Loop

Fixing the bug:


Instead of putting the input commands to a if statement, we can put them in a loop so
that if player press other numbers rather than 1 or 2, the game will ignore it.
Game State Story Design
Here we are going to see the flow diagram of the story “Steam Punk”. First download
the xml file from:
https://drive.google.com/file/d/1qC1qVkVA_CBks6nsATzVwdRBQv67z09t/view?usp=sh
aring
The go to Draw.io
https://app.diagrams.net/

Click on the Open Existing Diagram, then upload the file.

Here we have all the States that we could use in the Text Adventure Game as a
practice.
Challenge:

TextMesh Pro and Polish


Unity has a lot of goodies that we can find in our Package Manager. So let’s go to:
Window->Package Manager

Click on TextMesh Pro, install it if not yet installed

Then Restart Unity.


To use it, click on the Window->TextMesh Pro->Font Asset Creator

Then, click on “Import TMP Essentials” and “Import TMP Examples and Extras” button
Close it, now we have Font Asset Creator

Here we need a font to play with.


You can go, find and download a font at
https://www.dafont.com/

On the Asset, Create a Folder name Fonts

Double click on it and drag the Font file there.


Go back to Font Asset Creator window, and drag the Font from the Asset to Source
Font File field

Then, click on the Generate Font Atlas button and Save it to the default directory.
Close it. Back on the Canvas, right-click on that and select UI, then choose
Text-TextMeshPro.

Move it on the Title Text and delete the Title Text. Then Type in the Title

On the Font Asset, click on the Selector


Then, browse our Font

Customize the theme, by sliding the sliders from the Inspector


For color Theme you can go Adobe Colors:
https://color.adobe.com/create/color-wheel

Challenge

Example:
Publishing
First, we need to check our canvas if it fits to screen size. Click on the Canvas, on the
Inspector, scroll-down to Canvas Scaler and change the UI Scale Mode to Scale with
Screen Size.

Next, the Reference Resolution. Make it 800 x 600

Go to File->Build Settings

Check on some setting, like what platform are you going to target.
For this example it will be deployed on PC. Then , click on Build and Run.

Save it wherever you want, on my case it will be in a folder on my Desktop.


Finally, locate the Build File and Run by double clicking on it.

End.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy