CTIN 483
Week 5 Thursday
Drawing
Today
-
Classic Game list
-
Perforce setup
-
Instantiation and loops
Simple Game
- 2012, Blendo Games
- Unique lo-fi visual style
- Smash cuts
- Draws on cinema
Classic Games
Perforce
Demo: setting up a workspace
Remembering your child
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>();
Using Lists
What if you have more than one thing to remember?
The pattern is that you have a "manager" object, and that "manager" object has a List of the things it is managing.
So you might have a GoombaManager script that keeps a List of Goombas.
A List is like an array, but much smarter.
Lists are secretly arrays, behind the scenes, but that's not your problem.
grows and shrinks
List
Array
keeps the size it was given when created
can search for specific members
you have to loop over it to find things
can add or remove from the middle
you have to make a new array and copy what you need into it
can be sorted
you have to make a new array and sort it yourself
Imagine you've already placed a bunch of GOs in the scene and attached a Light component to them.
You can go find all the instances of a component in the scene like this:
Light[] lightArray;
lightArray = FindObjectsOfType<Light>();
sssssssssss
Then you can dump them into a List for easier management.
Light[] lightArray; lightArray = FindObjectsOfType<Light>();
List<Light> lightList;
lightList = new List<Light>(lightArray);
This line says "make a new List and copy the contents of this array into it"
Remembering your children
If you are spawning new things into the game, you can add each new thing to your List when you make it.
These are the steps:
- Declare and set up a List of whatever type you need.
- Instantiate the new thing and save a reference.
- Add the reference to the List.
List<Enemy> enemyList; enemyList = new List<Enemy>(); for (int i=0; i<5; i++) {
GameObject newEnemyGO =
Instantiate(enemyPrefab);
Enemy newEnemy =
newEnemyGO.GetComponent<Enemy>();
enemyList.Add(newEnemy);
}
List<Enemy> enemyList; enemyList = new List<Enemy>();
This just says you want to have a List at some point. Like you're thinking about needing groceries.
This line actually makes the List. Like getting out paper to write down your grocery list. But there's nothing in it yet.
for (int i=0; i<5; i++) {
GameObject newEnemyGO = Instantiate(enemyPrefab);
... }
"Do the following five times."
Make a new copy of the enemy prefab and keep a reference to it (instead of forgetting it forever.)
Make a new copy of the enemy prefab and keep a reference to it (instead of forgetting it forever.)
for (int i=0; i<5; i++) {
...
Enemy newEnemy =
newEnemyGO.GetComponent<Enemy>();
enemyList.Add(newEnemy);
}
Ask the newly created GameObject for a script component called Enemy.
Add this copy of the Enemy script to the List.
List<Light> listOfLights;
listOfLights = new List<Light>();
for (int i=0; i<5; i++) {
GameObject newLightGO =
Instantiate(lightPrefab);
Light newLight = newLightGO.GetComponent<Light>();
listOfLights.Add(newLight); }
It works the same way for other components.
List<Transform> enemyTransforms;
enemyTransforms = new List<Transform>();
for (int i=0; i<5; i++) {
GameObject newEnemyGO =
Instantiate(enemyPrefab);
Transform newEnemyTransform =
newEnemyGO.transform;
enemyTransforms.Add(newEnemyTransform); }
It works the same way for other components.
Loops and Lists
Having a List enables you to do any of the following useful things:
- Highlight the closest teleportation portal, health pack, etc. in a scene.
- Make all the enemies temporarily invincible (or invisible).
- Do cool rippling effects with lights.
- Manage an inventory.
- Broadcast a message to other players in the game.
Obviously, some of these take more work than others.
There is no way tell a List to do things (like become invisible). You have to visit each element in the List and tell it individually.
So you need a loop. There is a special kind of loop specifically for dealing with Lists and arrays.
foreach (Renderer thisRndr in enemyRenderers)
{
}
enemyRenderers[0];
Go visit each item in this List or array
Get each item in turn and put it in this temporary variable
foreach (Renderer thisRndr in enemyRenderers)
{
thisRndr.enabled = false;
}
Tell each item to do something using the temporary variable.
Lists aren't Perfect
Lists are much slower than arrays — that's why Unity only uses arrays.
There are other data structures and methods that are better suited for really large sets of objects (hundreds or thousands).
For example, if you need to very quickly find the closest 10 objects out of thousands, you would use something like this.
Lists are Slow
A List is a flexible way to store a "bunch" of something. You can sort them, shuffle them, and more.
But there are other useful data structures.
Lists are Flat
A Dictionary (or its close relative HashTable) is useful when you have two sets of things that should travel together. For example:
- a phone book stores names and numbers
- a dictionary stores words and definitions
- an inventory might store inventory slots and inventory items
- you could store game events (like "FoundGoblet") and their score values
More Structure
References
CTIN 483 - Week 5 Th Sep 23 - 2021 Fall
By Margaret Moser
CTIN 483 - Week 5 Th Sep 23 - 2021 Fall
Perforce setup. Remembering your children. Lists.
- 254