Creation/Dev/Using Classes Effectively: Difference between revisions

From Graal Bible
m (Using Classes Effectively moved to Creation/Dev/Using Classes Effectively)
No edit summary
Line 1: Line 1:
Note: Code examples in this article use new engine scripting.
Note: Code examples in this article use new engine scripting.
= Why classes? =
= 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.
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 =
= 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.
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  
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.
from, say, a putnpc2 NPC, then it acts like a database NPC.
== Joining from a level ==
=== Joining from a level ===
To join a class to a level NPC, simply create an NPC and put:
To join a class to a level NPC, simply create an NPC and put:
<pre>join("classname");</pre>
<pre>this.join("classname");</pre>
== Joining using putnpc2 ==
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.
To create a databased copy, you can use putnpc2 serverside:
=== Joining using putnpc2 ===
To create a databased copy, you can use the putnpc2() serverside:
<pre>putnpc2(x, y, "join(\"classname\");");</pre>
<pre>putnpc2(x, y, "join(\"classname\");");</pre>
... replacing x and y with coordinate values, or variable names containing these values.
... 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) ==
=== About database NPCs (including putnpc2 NPCs) ===
When you are finished with a DB NPC and want to destroy it, you should use destroy(); serverside.
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:
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:
Line 28: Line 25:
}</pre>
}</pre>
<pre>findnpc(identifier).foo();</pre>
<pre>findnpc(identifier).foo();</pre>
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:
<pre>this.warpto("levelname", x, y);</pre>
This enables great portability.
= Using classes to share functions =
= 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.
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 ==
=== Define your functions ===
Open up your class, and define your functions like you normally would!
Open up your class, and define your functions like you normally would!
<pre>function funcName(parameters)
<pre>function funcName(parameters)
Line 37: Line 39:
   // code
   // code
}</pre>
}</pre>
Then save your class.
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.
== Use your functions ==
=== Using your functions ===
Next, join the class to your NPCs:
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:
<pre>join("classname");</pre>
<pre>this.join("classname");</pre>
... and use your functions!
Then, you can use your functions.
<pre>classname::funcName();</pre>
 
You can easily join more than one class to an NPC, for example:
<pre>this.join("classone");
this.join("classtwo");</pre>
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:
<pre>classtwo::myFunction();</pre>
This means that you do not have to worry about using the same function name more than once.
 
= Using "player classes" =
= 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, for example, creating a function that damages the player called doDamage() and then calling it from any script using:
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.
<pre>player.doDamage();</pre>
== Join the class ==
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:
You can join the class to the player on the serverside like this:
<pre>player.join("classname");</pre>
<pre>player.join("classname");</pre>
== Define your functions ==
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:
For functions to be useable by other NPCs, they must be public:
<pre>public function funcName(parameters)
<pre>public function funcName(parameters)
Line 55: Line 70:
   // code
   // code
}</pre>
}</pre>
== Use your functions ==
This means that other NPCs can run the function, for example:
Other NPCs can use your player joined functions through the player object like shown here:
<pre>player.funcName(1, "Hello!");</pre>
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:
<pre>function funcName()
{
  // code
}</pre>
 
=== 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:
<pre>player.funcName();</pre>
<pre>player.funcName();</pre>
== Using this. ==
this. is the player's object in a player joined class, i.e.
<pre>this.chat = "foo";</pre>
... 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.
=== 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:
<pre>this.chat = "Chat text!";</pre>
Calling functions using this. will also call functions from player joined classes, for example:
<pre>player.join("classname");</pre>
<pre>this.playerFunction();
this.anotherFunction();</pre>

Revision as of 13:58, 6 February 2006

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