| Home Page | Recent Changes

Object Oriented Programming Overview

In Object Oriented Programming (OOP), 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 interacting 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

Discussion

KewlAzMe: A little confused at your EatParts function. It states that If NumberOfParts was less than number, NumberOfParts would be zero. But it's more likely that if you would want to eat 5 pieces of pizza, and only 3 were left, you would probably eat those 3 rather than no pizza at all. So if(NumberOfParts < number) NumberOfParts = number; (not 0)

Wormbo: NumberOfParts is set to the number of parts left after eating number parts. If you were to do "if(NumberOfParts < number) NumberOfParts = number;" then that would mean you still have number parts after eating number parts.
Look at this part of the function carefully:

  if(NumberOfParts < number)
    NumberOfParts = 0;
  else
    NumberOfParts -= number;

You have to read this as: If there are less parts than I want to eat, then zero parts are left afterwards, otherwise there are number parts less left than before.
Note the -= operator – it means "subtract and assign" or longer: subtract the value of the second operand from the first and assign the result to the first operand.

KewlAzMe: Ah I see what you're saying. I was assuming NumberOfParts to be a preset global total slices (say 8 slices), and number to be how many of those 8 slices you wanted to eat, not noticing that NumberOfParts was not set and is currently an empty variable which would result in 0 parts. Thanks for clearing that up.

KewlAzMe: Actually I think I'm more confused now. NumberOfParts is currently not set to any value. So when you go to eat 2 parts with the EatParts function, NumberOfParts will always be 0 or empty, and will keep exiting the function. Will this function ever eat pizza until you set NumberOfParts to a defined number of parts first at the top? Or am I missing something? Think you need:

var int NumberOfParts;
NumberOfParts = 8;

n8: You are right. The tutorial doesn't go to that level of detail yet as this is an overview.

RealmRPGer: That eating parts function is a bit redundant. The following works just as well:

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

Since the pizza is destroyed if there are zero or less parts, it doesn't matter whether or not the parts are negative.


Category Tutorial

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

FAQs

Help Desk

Mapping Topics

Mapping Lessons

UnrealEd Interface

UnrealScript Topics

UnrealScript Lessons

Making Mods

Class Tree

Modeling Topics

Chongqing Page

Log In