Object Oriented Programming
A strange way to manage computer code

(This lesson is somewhat optional. It acts as an introduction to NPC and Weapon hacking. If you already know what object-oriented programming is and you've used it before, then go to the next lesson. Otherwise, stay on this page for a smooth and steady introduction.)

Object-oriented programming? Maybe you've heard of it before; maybe you haven't.

In the world of programming, object-oriented programming (abbreviated as OOP) is an interesting way to control data, variables, and functions so that the job of programming is much easier than it normally would be.

OOP does this by putting information inside objects. These objects can communicate with each other and perform various tasks.

What the heck is an object? An object in programming has a complex definition and people rarely explain it well. I'll try my best.

First, the simple idea. Pretend that an object is a bag of variables. For now, I will consider anything that can hold and store a number to be a variable, including memory locations and ASM registers. If each object acts like a bag, then you can use that bag to store a bunch of variables. Even better, you can create multiple bags for different purposes. One bag might hold all the variables related to the boss Monster X. That bag, or "object", might hold Monster X's current health, his maximum health, the amount of damage he can do, and how fast he is currently moving. A different bag of variables will be used for a different boss, such the Undead Core.

A bag is an okay definition of an object, but it is not a full definition. A better definition would be to describe an object as an advanced data structure. A data structure can hold different kinds of information about some specific topic. The topic could be Polar Star bullets. Pixel, while programming Cave Story, might think about representing a Polar Star bullet as an object. This "bullet object" would be full of data, and maybe even functions, that define the way a Polar Star bullet behaves. How fast does the bullet travel? How far? Does it have any fancy movement? By looking inside the object, you can figure out the answers to some of these questions.

So we know that an object is a structure that can hold data and functions, and that objects are located inside your computer. These objects are used to represent something, whatever that something might be. A text editor, such as Notepad, might use a "document object" to represent a text document that is being edited. The document object holds important information about the text. For example, you can can look inside the document object to figure out how long the text is, where the cursor is located in the document, whether it has been saved or not, and probably what the text contains.

Classes
Now we must talk about classes. Classes are essential to understanding object-oriented programming.

What is a class? In programming, a class has nothing to do with a classroom. Instead, think of a Knight, a Magician, and an Archer. These three types of fighters are "classes" of people.

Think about a beggar, an average worker, and a rich man. These three people belong to different social "classes". The beggar is lower-class, the worker is middle-class, and the rich man is upper-class.

Think about a cat, a dog, a bird, and a frog. These four things are "classes" of animals.

A class is able to categorize things that are similar. A chicken and a dove are both birds, so they could both be derived from the "Bird Class". But a chicken and a truck are too different. A chicken is derived from the "Bird Class" and a truck might be derived from the "Motor Vehicle Class".

In programming, a class is a blueprint for an object. Since the "Motor Vehicle Class" is a blueprint, it must tell you how to build a motor vehicle, such as a truck. Since the "Bird Class" is a blueprint, it must tell you about the bird's anatomy, how it flies, and how it behaves.

In Cave Story, Pixel might use a "Bullet Class" as a blueprint. The Bullet blueprint would define the rules on how to build every possible type of bullet. Not only do Polar Star bullets fall under this class, but also Machinegun bullets, Fireball bullets, Bubbler bullets, and so on. Every time you fire a bullet by pressing the shoot key, a new bullet object will be created (the object will follow certain rules in the Bullet class). When the bullet disappears, the bullet object is destroyed. Or maybe it's kept for a while and then destroyed.

What happens when you kill a monster? Surely, each monster is part of the "NPC Class", and each monster has its own object that represents it. You'd be right. When a monster dies, its object is destroyed. Or maybe the object is not destroyed when the monster disappears. Maybe the object is just "frozen" in place because it is no longer being used. A dead monster doesn't have much behavior, but the object (the data structure) might still be there. Maybe monster objects are only destroyed when you exit the map.

NPC Object-Oriented Programming

Now you have seen what an object could be used for. In fact, an object could be used to represent a lot of different things, which is exactly why the word "object" is so vague and abstract.

This is also why this lesson is so abstract. Normally, whenever I talk about assembly, I usually sound like, "blah blah Code blah blah Hexadecimal blah blah DWORD," or something like that. This lesson has none of those concrete, numerical ideas. Instead, object-oriented programming is quite abstract, yet it can do many useful and practical things - such as building a game called Cave Story. When you start up Cave Story, you are starting up a virtual landscape filled with objects that get created and destroyed every second. These objects form a network, and those same objects really do communicate with each other by exchanging information that defines how the game runs.

Well, it looks like you're ready for NPC hacking.

Navigation
Previous Lesson: Designing Functions
Next Lesson: NPC Hacking 1
Table of Contents