Thread: NPC Include
View Single Post
  #6  
Old 07-07-2002, 07:26 AM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
"join" is a server-side command. Part of the scripting docu that I am working on :

Script classes

On the npcserver (server-side scripting) you can use the script classes system to organize code better and reuse code easily. The classes are saved in separate text files, e.g. class 'gralats' is saved in scripts/gralats.txt.

To join a class you must add the statement 'join <classname>;' to your script. That command works similar to the C/C++ command 'include', but is dynamic. It attaches the class script to the script of the npc, when executed it is handled like on big script. So you can put often used functions in class files and then all npcs that need the functions join the class.

Script class example:
An npc joins the class 'messagedisplays':
NPC Code:

if (created) {
setstring this.message,hello!;
}
join messagedisplays;


The class script 'messagedisplays' displays a message:
NPC Code:

if (playerenters) {
message #s(this.message);
}


That will result in a new virtual script for the npc:
NPC Code:

if (created) {
setstring this.message,hello!;
}
if (playerenters) {
message #s(this.message);
}



You can also join classes from inside classes, the scripting engine automatically prevents recursive joining. One important thing to know is that local variables (i,j, etc.) are only valid for the current script, so when you want to pass parameters to functions in joined classes by assigning values to temporary variables then you must use this.variables because normal variables cannot be accessed from the function.

When script classes contain functions then you can overwrite them in the npc script, the scripting engine chooses the right function script at runtime. The function implementation that is found first will be used. an example for this:
An npc joins the class 'messagedisplays':
NPC Code:

if (created) {
displaymessage();
}
function displaymessage() {
message Hello!;
}
join messagedisplays;


The class script displays 'Hi!':
NPC Code:

function displaymessage() {
message Hi!;
}


The resulting virtual script of the npc:
NPC Code:

if (created) {
displaymessage();
}
function displaymessage() {
message Hello!;
}
function displaymessage() {
message Hi!;
}


The npc will display 'Hello!' because the function displaymessage() of the npc is appearing before the displaymessage() of the joined class.

The script of classes is only kept one time in the memory, all npcs joining the class only hold pointers to the script class. That means using script classes is saving resources and makes it easy to update the script because whenever the script of the class is changed then automatically all npcs that have joined the class will use the new script.

With the remotecontrol.exe administrators with 'npccontrol' right have access to the class scripts. In the class script window you see the currently classes loaded in the npcserver. To 'add' a new class you must first add an npc using the class, removing a class file from the list is currently not supported (only disappears after restarting the npcserver).
Reply With Quote