Creation/Dev/Using Classes Effectively

From Graal Bible
Revision as of 19:58, 18 December 2005 by Skyld (talk | contribs)

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

Why classes?

The idea

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.

What this means

Increased efficiency and productivity!

Using classes as standalone NPCs

What this means

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:

join("classname");

Joining using putnpc2

To create a databased copy, you can use putnpc2 serverside:

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

... replacing x and y with coordinate values, or variable names containing these values.

About database NPCs (including putnpc2 NPCs)

When you are finished with a DB NPC and want to destroy it, you should use destroy(); serverside.

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();

Using classes to share functions

What this means

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.

Use your functions

Next, join the class to your NPCs:

join("classname");

... and use your functions!

classname::funcName();

Using "player classes"

What this means

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.

Join the class

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

player.join("classname");

Define your functions

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

public function funcName(parameters)
{
  // code
}

Use your functions

Other NPCs can use your player joined functions through the player object like shown here:

player.funcName();

Using this.

this. is the player's object in a player joined class, i.e.

this.chat = "foo";

... would set the player's chat text to "foo".

Because of this, this. variables (when not writing to a built-in variable) are stored in the client strings, unlike weapons, etc.