CTIN 483

Week 4 Tuesday

Drawing

Today

  • Version control concepts and vocabulary

  • Thinking like a programmer

  • Design journal

  • References

Simple Game

  • 2014, State of Play
  • Flood fill: the game
  • Beautiful and satisfying
  • Well made puzzles and challenge levels

Version Control

Version Control: Why?

Using version control in your project:

  • Gives you a way to recover from your mistakes
     
  • Makes it possible to trade files with a team
     
  • Gives your team a way to recover from your mistakes

Version Control: How?

We will use a system called Perforce to turn in homework starting soon.

 

We will just be using it as a way to trade files for a while. Later in the semester we will go through how to set it up with Unity projects so that you can work with a partner.

What is Perforce?

Perforce is a centralized system of version control – there's a central server (perforce.sca.usc.edu) where files live.

 

To keep things tidy, each class session at SCA gets its own area on the server, called a depot. (Other version control systems call this a repository.)

 

Then there are clients (individual laptops, lab machines, etc.) that can copy files from the server, make changes and send them back.

Perforce Overview

Perforce Overview

Unlike Google Drive, in Perforce you can't make changes directly to what's on the server. You can't make a new document or upload a file to a particular place.

 

The only way to communicate with the Perforce server is to sync – that is, you make changes to what's on your computer and then push those changes to the server.

Perforce Overview

When you push a bunch of changes up to the server, you send them as a single bundle called a changeset.

 

A changeset can be one or several files that you've updated, moved, added, or (gasp) deleted.

 

Perforce saves a copy of every changeset, so that you can get back to earlier versions of your files if there is a problem.

Perforce Overview

We will use a program called P4V to interact with our Perforce server. If you want to use it on your own computer, then you'll download it as part of the homework.

 

P4V lets you see what's on the server and what's in your local copy of those folders, that is, your workspace. You will use it to get files, like demo projects for class, and to push files, like your homework.

P4V Interface

Think like a programmer

Example: Dragging in Candy Crush

Smarter Collisions

Smarter Collisions

Here are three ways to decide what the other object is, in a collision:

  1. What is its name?
  2. What is its tag?
  3. Does it have a certain script attached?

Note that there are many others.

  if (collision.gameObject.name == "Goomba")
  {
       Die();
  }
  • likely to break because people change GO names a lot
  • level designers work quickly, make copies of GOs with names like "Goomba (1)"
  • easy

Pros:

Cons:

  • one tag per GO, but a GO might belong to multiple groups
  • difficult to wrangle more than a couple dozen tags
  • easy

Pros:

Cons:

  if (collision.gameObject.tag == "Goomba")
  {
       Die();
  }

Another way to know whether some GO is the kind of GO you care about: ask for its script components.

collision.gameObject.GetComponent<Goomba>()

If the other GO has a Goomba script attached, you can assume it's a Goomba.

collision.gameObject.GetComponent<Goomba>()

If the other GO has a Goomba component, this will get you a reference to it.

 

If the other GO does not have a Goomba component, this will return the special value null, which is nothingness.

So to determine whether the other GO has the script, you ask if the result is not null.

 

Not-nothing is something. So it must be a Goomba.

Goomba potentialGoomba = 
  collision.gameObject.GetComponent<Goomba>();

if (potentialGoomba != null)
{
   Die();
}
  • a little annoying to set up
  • sometimes GOs don't really need scripts and it's awkward to add them just for this
  • reliable
  • tells you what you probably really want to know, which is how the other object acts
  • now you have a reference to the other script

Pros:

Cons:

if (potentialGoomba != null)
{
   Die();
}

Prefabs

are files

Prefabs are necessary in order to spawn (create) new things during your game – enemies, bullets, etc.

Why? Because Unity needs to have a template to work from when you tell it to make something new.

Instantiate:
The Power of Creation

Once you have a prefab to work with, you can stamp out as many copies as you like.

 

There are two steps.

First, in the script that is going to spawn new objects, give yourself a [SerializeField] variable to hold a reference to the prefab.

[SerializeField]
GameObject blockPrefab;

This will usually be of type GameObject.

But later on you will sometimes find it more useful to use another type.

Then you can use Unity's Instantiate function to crank out the new objects.

Instantiate (blockPrefab);

This is the simplest version. It will make a new copy of the prefab. The new object's position will be whatever the prefab has as its position.

Instantiate (
    blockPrefab,
    position,
    rotation
);

Often you want to set the position of the new object at the moment you make it.

 

But this function requires you to set a rotation as well.

What to make (prefab)

Where (V3)

How to rotate it (Quaternion)

About 99% of the time you will use one of these two values for the rotation:

Quaternion.identity



transform.rotation

no rotation

use this GO's rotation

You will use these two versions of the Instantiate function about 90% of the time.

 

There are others that let you set the parent of the new object, or all of the above. See the documentation.

 

But you can do much more with that newly spawned object... if you save a reference.

Remembering your child

Sometimes, as with bullets in Ikaruga or splats in Splatoon, you just need to crank out a bunch of objects and forget about them.

 

In that case you can simply do this:

Instantiate (
    splatPrefab,
    position,
    rotation
);

Other times you will want to remember the thing you instantiated.

 

Sometimes it is just for a moment, so you can set its properties. Other times you want to keep the reference for longer. Either way, this is how you save a reference to the new object:

Instantiate (
    thingPrefab,
    position,
    rotation
);
GameObject newThing =

Once you have the reference to the new instance, you can do anything you can do with a GameObject or its components.

newThing.transform.parent =
                     gameObject.transform;

newThing.GetComponent<Renderer>().enabled
                     = false;

Remember, your scripts are components too. Here is how to get a script component from a newly instantiated object and call a method on it.

Instantiate (...);
HomingMissile newMissile =
newMissile.SetTarget(player);

assumes the HomingMissile script has a public method called SetTarget

GameObject newMissileGO =
newMissileGO.GetComponent<HomingMissile>();

References

Made with Slides.com