Creation/Dev/Using Classes Effectively

From Graal Bible
Revision as of 13:58, 6 February 2006 by Skyld (talk | contribs)

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 other scripts. You can attach them to the player, too, where the player is treated as the current object.

Using classes as standalone NPCs

Classes are capable of being "standalone", i.e. being treated like a level or database NPC. This makes it possible to create many copies of a single script. This could be useful for, say, ATM scripts.

When joined from a level, the class script is being included into the script of the level NPC that it is being joined from, therefore it acts as a level NPC. When joined from, say, a putnpc2 NPC, then it acts like a database NPC.

Joining from a level

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

this.join("classname");

The class will be joined to that NPC. When updating a class in NC that is joined to a level, you may have to update the level in order to see the update.

Joining using putnpc2

To create a databased copy, you can use the putnpc2() 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 in NC immediately.

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 still remain in 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.

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 join the class to each NPC 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 join the class to the current NPC. Like a level NPC, you use the following command:

this.join("classname");

Then, you can use your functions.

You can easily join more than one class to an NPC, 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.

Using "player classes"

Player classes are classes joined to the player. 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 join the class to the player on the serverside like this:

player.join("classname");

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

Player classes are no different to ordinary classes - you should edit them in NC the same way as you do 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();