I was thinking it would be cool if we had an easy way to force our current scripts to read a function or subroutine file prior to reading the actual script file. This would allow some repeatable sections of scripts to be created as functions which are run through 1 or 2 files to handle the sub or function part of the script. This is probably fairly basic stuff for you Perl people out there, but I didn't know it was even possible until recently and I think it would be awesome if I could get it working lol.
The idea is that we might have a file named "perl_functions.pl" in our quest directory and for any NPC script that we wanted to have access to the functions in that file, we would add a line similar to this:
Code:
require "./home/eqemu/server/quests/perl_functions.pl";
And then the NPC script would be able to use any of the functions set from that file.
So, for those not familiar with a system like this, here is an example of how it would work:
TestNPC.pl
Code:
require "./home/eqemu/server/quests/perl_functions.pl";
Sub EVENT_ATTACK {
&CastSpellOnTarget(9425);
}
perl_functions.pl
Code:
sub CastSpellOnTarget {
my $CastSpellID = $_[0]; #Use the Spell ID Supplied to the Function - "$_[0]" means to use the first argument given
my $Cur_Target = $npc->GetTarget(); #Get the current Target for this NPC
if ($Cur_Target) { #Only cast if the NPC actually has a Target
my $My_Target = $Cur_Target->GetID(); #Get the Entity ID of the Target
$npc->CastSpell($CastSpellID, $My_Target); #Cast the requested Spell ID on the NPC Target
}
}
(Note: this section of code would be much more useful in an EVENT_TIMER where the $client is not exported)
With that section of script being set as a function that can then be called at any point, we can reduce the overhead within long scripts that repeat the same section of scripting over and over.
At this point, I just can't get the require to work right to run functions from other files like I need for a system like this. Does anyone know how to do that?
If we can get this working, I think it would allow for some special perl functions to be made that don't necessarily need to be added into the source code. Then, we could even add a page on the wiki that contains a compiled list of functions different people have made so everyone can make use of them just by adding the require line (or whatever is needed) to any NPC scripts they might want to use it on.