Creation/Dev/Using Classes Effectively
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.