Creation/Dev/Using Classes Effectively: Difference between revisions
No edit summary |
No edit summary |
||
Line 20: | Line 20: | ||
<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. | ||
== About database NPCs (putnpc2) == | |||
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: | |||
<pre>with (findnpc(identifier)) | |||
{ | |||
// foo | |||
}</pre> | |||
<pre>findnpc(identifier).foo();</pre> |
Revision as of 18:26, 18 December 2005
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 (putnpc2)
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();