CTIN 583

Week 2 Wednesday

Today

  • Review homework

  • Udemy course

  • Scriptable Objects

  • Arrays

  • Spheres array demo

  • Vectors, perhaps

  • Introduce keyboard race demo

Brackeys

How did it go?

He drags in a component from the same GameObject to set the reference.

 

This is the exact same thing as using GetComponent!

You don't have to delete the using statements!

 

They're not hurting anyone!

Udemy Course

https://gdev.tv/archive-cud

The password is: archive.

You need this for the next homework!

Udemy Course

Take a couple of minutes and try to access it now.

Scriptable Objects

sound harder than they are

Scriptable Objects

A Scriptable Object is a special Unity structure that is a template for data objects.

 

You can think of it as the master copy of a form – one that you keep blank so you can make more copies when  you need them.

 

Scriptable Objects are mostly intended to store variables, but you can also write functions in them.

Scriptable Objects

A Scriptable Object is a special Unity structure that is a template for data objects.

 

You can think of it as the master copy of a form – one that you keep blank so you can make more copies when  you need them.

 

Scriptable Objects are mostly intended to store variables, but you can also write functions in them.

Games involve a lot of information.

  • A text game has a lot of strings and paths
  • An arcade game has enemy attributes
  • A tower-defense game has waves
  • A puzzle game has levels

This information has to live somewhere.

Some things about your game can be represented in the scene file.

 

For example, you might have a scene for each level of a platformer like Thomas Was Alone.

 

But what about a strategy game like Civilization?

Many game teams manage this information outside the game engine, using spreadsheets, text documents, and so on.

image credits: "The VGLC: The Video Game Level Corpus"

image credit: Ruby Cow Games

Pros:

Very flexible.

 

Cons:

The designer has to understand the format of the document, i.e. what information goes in what column.
 

And the engineer has to write code to get and read the file. Unity does not do this automatically.

Scriptable Objects are meant to be a kind of in-between option that lets the designers do their work directly in Unity.

In the homework you will see two examples of the kind of information you might store using Scriptable Objects:

  • Data for Hearthstone cards
  • Scenes (text passages) for your text adventure project

Another way you might use them is like a table in a Word document. For example, to store how many points different things are worth.

image credit: classicgaming.cc

The basic steps for creating ScriptableObjects are:

  1. Create and name a new C# Script.
  2. Delete "MonoBehaviour" and replace it with "ScriptableObject".
  3. Add the variables you need for your object.
  4. Above the "public class" line, write
     
  5. Return to Unity and choose
    Assets->Create->Your Object.
  6. Fill in the values for your object. Repeat steps 5 and 6 for every instance you need.
[CreateAssetMenu]

Ponderable

How would you use Scriptable Objects to represent information in

  • an RPG?
  • a match-three game?
  • an arcade game like Frogger or Pac-Man?

Here's one example, an RPG weapon:

Stretch

Demo: Spheres Array

Or more Scriptable Objects

Arrays

An array is like what humans would call a list of objects. (To computers, a List is a different thing.)

 

An array can only contain one kind of value, that is, strings, floats, GameObjects, and so on.

Sometimes there is a set of things you care about as a group:

  • all the currently active enemies
  • all the spawn points in a level
  • the cards a player is currently holding
GameObject card1;
GameObject card2; 
GameObject card3; 

Here is what not to do:

Instead, if you want to store a set of things together, you can create an array variable to hold them. That looks like this:

GameObject[] cards;
string[] studentNames;
InventoryItem[] playerInventory; 
GameObject[] cards; 
  • You can store any number of cards you need without changing your code
     
  • You can easily treat them as a group, for example, tell them all to flip over

For the most part, if you are working with an array you will make it visible in the Inspector, like in the homework:

[SerializeField]
GameObject[] cards;

Then you can just drag in what you need.

 

Later you will learn about List, which is a more flexible version of an array. We will return to Lists soon.

Using the Index

You can only get to an item inside an array via its position, or index.

 

Arrays start counting at zero, which is confusing for humans:

studentNames = new string[3];
studentNames[0] = "Sabrina";
studentNames[1] = "Sandy";
studentNames[2] = "Sriram"; 

Even more confusingly, when you give a length for the array you don't start at zero but at one.

 

So this code says there are three items in the array, but the last item in the array has index 2.

studentNames = new string[3];
studentNames[0] = "Sabrina";
studentNames[1] = "Sandy";
studentNames[2] = "Sriram"; 

The first index (position) in an array is always zero.

studentNames[0] //"Sabrina"

The last position in an array is more complicated.

 

It is the number of items in the array (its length) minus one, because of starting at zero.

studentNames.Length - 1

So the last thing in your array is here:

studentNames[ studentNames.Length - 1 ]

A loop visiting each element in your array will therefore count from 0 to (length-1).

for (int i=0;
     i < studentNames.Length - 1;
     i++)
{
       print(studentNames[i]);
}

Arrays: Oops

If you try to get to a position that isn't actually in the array, you will get a particular kind of error:

studentNames = new string[3];
studentNames[0] = "Sabrina";
studentNames[1] = "Sandy";
studentNames[2] = "Sriram";
studentNames[3] = "oops";

Arrays: Oops

If you forget to initialize your array you'll get an error like this.

 

There are lots of other ways to get this error, but if you see that this error comes from a line of code using an array, then you probably forgot to initialize.

Made with Slides.com