| Home Page | Recent Changes | Preferences

OOP Overview

In OO programming, the world is made up of objects, but not all objects are the same. In an Unreal game, for example, there are players, weapons, pickups, lights and so on. These all look different and they do different things. What makes them different is that every object is of a certain class. Every player has a ShieldGun, and each one is an object of the class "ShieldGun". Each ShieldGun object has different properties, such as its location, but they all have things in common.

There are many ways to think of classes and objects: you can imagine a class as a "recipe" or a "blueprint" for making things. A class is basically a piece of UnrealScript code. It defines properties that every object of this class will have, and functions that every object of this class can perform. In effect, every object in the program is like a mini-program of its own, that is constantly iteracting with other mini-programs. Each object runs the script for whatever class it is: the ShieldGun objects run one script, the players run another and so on.

Creating a Class

Say you want to create a Pizza. A Pizza is a meal, so:

class Pizza extends Meal;

A Pizza is generally divided into smaller parts:

var int NumberOfParts;

Also, a TRUE Pizza has tomato sauce and cheese:

var bool bHasSauce;
var bool bHasCheese;

You can add sauce, or eat the sauce, you can add cheese, or eat the cheese:

function AddSauce()
{
  bHasSauce = true;
}


function EatSauce()
{
  bHasSauce  = false;
}

function AddCheese()
{
  bHasCheese = true;
}

function EatCheese()
{
  bHasCheese = false;
}

You can eat parts of the pizza:

function EatParts(int number)
{
  if(NumberOfParts < number)
    NumberOfParts = 0;
  else
    NumberOfParts -= number;
  if(NumberOfParts <= 0)
    Destroy();
}

Notice that if the pizza object has 0 parts left, it no longer exists and must be destroyed. If you really wanted to be polite, you might want the EatParts function to return a value to tell the person trying to eat it if they have been successful or not, for instance trying to eat four parts when there are only three left...

Where's My Pizza?

Mind you, what we've been doing up to here is just describing what a pizza looks like and how to create (and eat) it. It's like you're writing a recipe (that's your UnrealScript class).

You still don't have a pizza to eat right now, even though you perfectly know how to eat it and what it would look like. In real life, you'd have to gather kitchen utensils and pizza ingredients and bake one; in UnrealScript, you can just wave your magic wand and spawn one:

// Now that I know what a pizza looks like, I'm hungry and want to eat one.

local Pizza MyPizza;             // That's where I'm going to hold the pizza.  Like a dish.

MyPizza = Spawn(class 'Pizza');  // Hooray!  Pizza's ready!
MyPizza.EatParts(2);             // Now I'm eating two parts of my pizza.

Subclassing

Now, you want a better Pizza, with mushrooms and bacon and egg. This is an enhanced pizza, so:

class BetterPizza extends Pizza;

This subclass inherits all the variables (NumberOfParts, bHasCheese, etc) from the Pizza class. So it already has cheese and it has sauce, but this sauce doesn't include egg and mushrooms, so we have to change this:

var bool bHasMushrooms;
var bool bHasEgg;

function AddSauce()
{
  bHasEgg = true;
  bHasMushrooms = true;
  Super.AddSauce();
}

Hold it! What's this "Super" thing?

It is a way to call the original Pizza's AddSauce function. This way, we don't have to re-code the Sauce Management :D Same goes for EatSauce:

function EatSauce()
{
  bHasEgg = false;
  bHasMushrooms = false;
  Super.EatSauce();
}

The BetterPizza also inherited the functions to handle the parts. It is a complete, functional Pizza :)

Now that you've mastered the use of a single object, you can begin to work with multiple objects that interact with each other. This is covered in the next part of this this tutorial.

What next

Related Topics

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In