CONTRIBUTION: Accessing $text, $mname, etc. in a plugin::sub()
Background: I'm a veteran (1999) EQ player and hobby programmer who is very new to the EQEmu scene and never looked at Perl until a few days ago, so I apologize ahead of time if I say or do anything n00bish.
It is way cool that one can put common subs into a 'plugins' file and access them from any other .pl file in the quest folders. It is unfortunate that the 'plugins' subs do not have access to the game variables ($text, $mname, %itemcount, etc.). I noticed that in order to get around this complication, everyone has been manually passing the required game variables as arguments to the plugin subs so they can be read (and in the case of \%itemcount, manipulated) as needed. To me this seemed unintuitive and difficult to maintain, so I looked for a better way to get around the problem. I ran across someone else's post (which I unfortunately cannot find anymore) somewhere on the forums here, which utilized Perl's 'caller(x)' function to access variables in the top-level scope, and knew I was on the right track. After researching the caller() function and modifying the original post's code, I was able to come up with a simple function that gives any sub in any context access to ALL of the game variables accessible to EVENT_SAY et al ($name, $text, %itemcount, etc.), without having to pass them as arguments anymore! Function: var($varName) -- Returns a reference to the game variable requested. Code:
# var - Access a quest-related game variable from within any context In NPCID.pl... (Heck, even default.pl) Code:
sub EVENT_SAY Code:
sub defaultSay() Note that because var() returns references, you'll have to use $$text instead of $text, etc. See val() and setval() below for a split-sub workaround if this irritates you. EXCEPTION: In the case of accessing %itemcount from EVENT_ITEM, you'll have to define and use it as follows: Code:
sub defaultItem() Now, since I got that working, I went a step further and made a couple of shortcut functions: Code:
# val - Shortcut that returns the -value- of a quest-related game variable instead of a reference to it Replacing the defaultActions.pl->defaultSay() function listed above... Code:
sub defaultSay() This is not a problem for the game classes ($client, $entity_list, etc.), since they're read-only anyway. However, if you want to use single $ syntax and still be able to change global variables' values from time to time, you can use the setval() function, as follows: In NPCID.pl... Code:
sub EVENT_SAY Code:
sub changeText() NPC says, 'BEFORE: Hail, NPCName' NPC says, 'AFTER: Change comes from within!' Try it out! Let me know if it looks like I missed something or if anything could be simplified or optimized! (Or if I typoed something in my examples. I tried copy/paste and testing before submitting the post!) - Shendare |
All times are GMT -4. The time now is 11:04 AM. |
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.