Sub and Function Files for Perl?
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"; 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"; perl_functions.pl Code:
sub CastSpellOnTarget { 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. |
I would really like all of the perl functions listed like this (if that's possible), particularly since I'd like to be able to overload some of them (quest::say actually).
Admittedly, I don't really understand how the perl scripts interact with the server, the whole C/perl combination thing confuses me. |
Yeah, I don't quite understand how they interact either yet, which is why I posted this lol. But, if we can get it working, I imagine you would use Quest Objects and make functions to use them to replace stuff like quest::say(). For example, you might make something like this:
TestNPC.pl Code:
require "./home/eqemu/server/quests/perl_functions.pl"; perl_functions.pl Code:
sub NPCWhisper { |
I was working on a script to change languages of every npc in the game to something more akin to their race, as part of some customization for my server to make languages feel more important.
I wanted to overload quest::say() to automatically do a lookup on the npc's race and then select a language that I had set in a data structure. |
Yeah, if someone can help me get this system working properly to let NPC quest files access other files, you could probably build a function that includes an array or hash that will convert each race to whatever language you want that race to speak and then have them speak in that language. It might work something like this:
TestNPC.pl Code:
require "./home/eqemu/server/quests/perl_functions.pl"; perl_functions.pl Code:
sub RacialLanguageSay { BTW, here is a page that explains require(), use(), Libaries, Modules and other similar things that might help in getting this to work. http://www.perl.com/pub/a/2002/05/14...rl.html?page=3 I have tried a few ways, but no luck so far. |
Ah right, I understand what you were trying to say now.
Yeah, it would be nice if we could have an included file or (more extensible), a folder that perl would include for user defined functions. |
I haven't ever really played with the quest plugins in the plugins folder, so maybe they already do what I am wanting to do. Though, it is my understanding that you have to restart the whole server to get new plugins to take effect.
If plugins can already do it, then I will just get started on some new plugins I guess lol. Here is an example plugin I have in my plugins folder (I do not currently use it): soulbinders.pl Code:
#!/usr/bin/perl TestNPC.pl Code:
Sub EVENT_SAY { |
Yeah plugins basically work like this. It's plugin not pluggin though.
|
Oops, typo. I fixed it, thanks.
So, I am guessing that plugins get loaded into memory when the server starts? That would probably explain why they only update when the server is restarted. Any possibility of a way to make them update without a restart? If not, no biggie, but it would be helpful for testing purposes. I plan to add a decent number of plugins now that I know how to use them somewhat. I will still need to figure out how the "shift" works for using the arguments, but it doesn't seem too hard. I also need to to play around with it and see how well it works for returning data. I have read that for certain perl features similar to this to work, the result of the file must always be true. That is why the files all end with "1;" as the last line. I am not sure that this is required for our plugin system yet, but some testing can probably determine that. |
require does work.
If I put this in /tmp/myfunc.pl Code:
sub MyFunc { Code:
require ("/tmp/myfunc.pl"); Code:
/tmp/myfunc.pl did not return a true value at Guard_Bixby.pl line 1. |
Ahh sweet! Thanks Derision!
I kept messing with it, and did try returning a 1 result a couple of times, but I don't think I hit the right combo of how to get it to work. It may just have been that I didn't put a full path in the require and assumed that it would look for the file in the same directory as the NPC script was running from. That was on my list of things to try still, but hadn't gotten that far yet and not at home to test it out. At the very least, using require should be a perfect way to test out plugins without having to reload the server to do so. Then, you just test them out with require and adjust the needed stuff afterward to use it as a plugin instead for the next restart. |
Yup, I tested the require as Derision posted it, and it works perfectly as far as I can tell. Here is an example one that I made and tested tonight:
/home/eqemu/server/quests/functions/script_functions.pl (note I added a new folder named "functions" to my quests directory) Code:
# Script file to hold various subroutines and functions for multiple scripts to use TestNPC.pl: Code:
require '/home/eqemu/server/quests/functions/script_functions.pl'; |
Plugins should be reloading when you do a #reloadqst.
|
Quote:
|
Yeah, I had always read that they required a reboot and wasn't able to get the ones I was making working properly when I tested, so I assumed that was how it worked. But, after more work and testing, they definitely do update with a #questreload.
The only problem I see with plugins vs a require() function/sub is that $npc, $client and such are not available inside the plugin unless they are passed into the plugin as an argument. I think plugins would be more useful if they could be passed those variables without having to put them in as an argument. |
All times are GMT -4. The time now is 08:22 PM. |
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.