Packages allow you to add additional behavior before/after the main function and modify/compare the arguments given, and return value of the function, without rewriting the function
Here's an example:
Parent function:
function demo(%a)
{
echo(%a);
}
Package:
package demoPackage
{
function demo(%a)
{
echo("You wanted " @ %a @ " but I don't like that number!");
%a++;
Parent::demo(%a);
echo("I like cheese");
}
};
activatePackage(demoPackage);
Calling the function:
demo(3);
Will display in the console:
You wanted 3 but I don't like that number!
4
I like cheese
Line-by-line brown townysis of the contents of the function definition inside the package:
line 1 - echo(...); - This line echos to the console a string, with the original value you called the function with
line 2 - %a+=1; Increment %a, changing the value we gave the function
line 3 - Parent::demo(%a); Call the original version (the version before this package) with the modified function
line 1 of original function then echos the modified value
line 4 - echo(...); - echo another string after the original function completes
Or a more practical (and very commonly used) application:
If you want to message (or perform some other action on) a client immediately on joining a server, you would package GameConnection::autoAdminCheck. This is the function that is called when a client joins the server, and checks whether or not they should be given admin based on the autoadmin lists
package demoPackage2
{
function GameConnection::autoAdminCheck(%this)
{
messageClient(%this,'','Hello!');
%r = Parent::autoAdminCheck(%this);
if(%this.isAdmin || %this.isSuperAdmin)
messageClient(%this,'','You are an admin');
else
messageClient(%this,'','You are not an admin!');
return %r;
}
};
ActivatePackage(demoPackage2);
Some general things to know:
You can have multiple parents on the same function. When calling the function, it will execute in the reverse order of how they were executed.
When calling the parent function, make sure you always call Parent::functions(args);. Just calling functions(args); will cause infinite recursion which will crash the game