Creation/Dev/Using Classes Effectively

From Graal Bible
Revision as of 18:34, 15 September 2006 by Stefan (talk | contribs) (Changed the phrase "class joins to npc" to "npc joins the class")

Note: Code examples in this article use new engine scripting.

Why classes?

Classes are a very useful concept in Gscript. They allow many instances of the same code to be run and even dynamically placed. They are also an effective means of sharing functions between scripts. You can attach them to the player, too, where the player is treated as the current object.

Using classes as standalone NPCs

Objects, including NPCs from levels or database NPCs, can join a class. That class is then controlling the behaviour of the object. The class can so act either as level NPC, when being joined from a level NPC, or as database NPC, when joined from a database NPC. Many objects can join the same class, which is useful e.g. for ATMs.

Joining from a level

To let a level NPC join a class, simply create an NPC and put:

this.join("classname");

The NPC will then join the class once created. When updating a class with RC that is joined by level NPCs, you may have to update the level in order to see the update.

Joining using putnpc2

To create a databased NPC, you can use the function putnpc2() on serverside:

putnpc2(x, y, "join(\"classname\");");

... replacing x and y with coordinate values, or variable names containing these values. A putnpc2() NPC is called a "database NPC", being that it is databased in the NPC-Server, and does not belong to a specific level. Database NPCs usually receive updates when the script is updated with RC immediately. They can also warp to other levels.

About database NPCs (including putnpc2 NPCs)

When you are finished with a DB NPC and want to destroy it, you should use this.destroy(); serverside. The NPC will then be totally removed from memory. Note that the class will of course still remain on the server.

DB NPCs have unique identifiers, which can be read using this.name. This identifier can be used to locate the NPC on the serverside, like shown:

with (findnpc(identifier))
{
  // foo
}
findnpc(identifier).foo();

This allows other scripts to interact with the database NPCs that you have placed. With the new scripting engine you can also access those npcs directly by using identifier.foo.

Like mentioned previously, database NPCs are not specific to one level, and such you can use the warpto() command to move the NPC into a new level:

this.warpto("levelname", x, y);

This enables great portability.

Using classes to share functions

If you write a set of functions that you wish to use in more than one NPC, then you can define them in a class, and let each npc join the class instead of copying the functions into each. It also means that you can update your functions across all your scripts, too.

Define your functions

Open up your class, and define your functions like you normally would!

function funcName(parameters)
{
  // code
}

Then save your class. This will provide these functions to any NPC that joins the class, and will allow you to easily update functions between several NPCs.

Using your functions

Now, before you can use the functions in your other scripts, you must let the current NPC join the class. Like a level NPC, you use the following command:

this.join("classname");

Then, you can use your functions.

You can easily let an NPC join more than one class, for example:

this.join("classone");
this.join("classtwo");

However, this can cause problems where there is more than one function with the same name. You can specify which class you want to use the function from by using the :: operator. The :: tells the scripting engine to look in that specific class, like so:

classtwo::myFunction();

This means that you do not have to worry about using the same function name more than once. If function names are colliding, then the first function found will be used, which means either from the script of the npc itself or of the class joined first.

Using "player classes"

Player classes are classes joined by players. A player-joined class treats the player as the current object (this.). Also, you can create functions available to other scripts through the player object, for example, creating a function that damages the player called doDamage() and then calling it from any script using:

player.doDamage();

This allows the player to be treated easier using different functions.

Join the class

You can let the player join the class on serverside like this:

player.join("classname");

You could do this in your Control-NPC to, say, join the classes whenever the player logs onto the game server.

Player classes are no different to ordinary classes - you should edit them in RC the same way as you do with normal classes.

Define your functions

For functions to be useable by other NPCs, they must be public:

public function funcName(parameters)
{
  // code
}

This means that other NPCs can run the function, for example:

player.funcName(1, "Hello!");

Non-public functions will not be useable from outside the current script, so you can define normal functions to create a function that you don't want to be called externally, for example:

function funcName()
{
  // code
}

Using your functions

If your functions are public, then other scripts can easily call the functions that you have defined in your class, for example:

player.funcName();

this. object

Player classes work in a different focus, where the current object (that's the this. object) becomes the player. So, if you wanted to change the player's chat text in a player joined class, you would use the following:

this.chat = "Chat text!";

Calling functions using this. will also call functions from player joined classes, for example:

player.join("classname");
this.playerFunction();
this.anotherFunction();