Author Topic: What is the point of package in TS (and more questions)  (Read 1888 times)

/title

I mean, I know they can rewrite functions, is that all? If that's all to it, at what occasion would I use packages
« Last Edit: July 24, 2014, 09:05:10 PM by chubaka452 »

They can be easily disabled if something goes screwy, as well as parenting functions.

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:
Code: [Select]
function demo(%a)
{
echo(%a);
}

Package:
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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
« Last Edit: July 24, 2014, 09:06:33 PM by Headcrab Zombie »

Okay that's cool, I have another question about something different, should I change the heading or what?

Okay that's cool, I have another question about something different, should I change the heading or what?
I added another example which you should check out
Also, in help threads, you should never get rid of anythign related to your question, as other people might be searching and find it. I'd just add "(and more questions)" or something like that to the end of the thread subject, leave your current question in place, and add the new question as a new reply.

I added another example which you should check out
Oh thanks! Didn't think about that. It's easier to understand than I thought.

Also, how do you I know automatic things like that function in the game GameConnection::autoAdminCheck(%this)
« Last Edit: July 24, 2014, 09:20:41 PM by chubaka452 »

You can also put functions in a package, and when the package is disabled, the functions also disappear, like they were never defined!

Also, how do you I know automatic things like that function in the game GameConnection::autoAdminCheck(%this)

http://forum.blockland.us/index.php?topic=191941.0
« Last Edit: July 24, 2014, 11:10:39 PM by elm »

You can also put functions in a package, and when the package is disabled, the functions also disappear, like they were never defined!

http://forum.blockland.us/index.php?topic=191941.0

Okay thanks!