View Full Version : Quest Using Hashes (%)
Kingmen30264
11-06-2011, 09:14 PM
It's been awhile since I bugged anyone here on the forums about anything... and I got a question about a few quest(s) that I am composing.
First I am going to start off by saying this, "I finally got myself a Perl Book, and I have been slowing learning about Perl and its' entities."
As stated above, I am slowly learning Perl (What with College, a kid, and a family hehe), and here is something that I have come up with.
sub EVENT_SAY
{
my $cookie = quest::saylink("Cookie");
my $cookieid = quest::varlink("19732");
my %dadscookies = (
"Hail" => "Would you like a [$cookie]?",
"cookie" => "Yes, a Cookie! Here is what it looks like [$cookieid]",
"no" => "Okay, I will eat it!",
)
print %dadscookies($text);
}
I have tried it several forms of this, but I just cannot seem to grasp how it should be laid out.
If someone could please help me with this, then that would be awesome.
What I am trying to do is this:
Eliminate the IF/elsif/elseif command, and from what I have read, this is VERY possible thanks to "Hashes".
Once again, as always, sorry for the noob question, but I am trying learn this without asking too many people for help.
Thanks to all that respond.
Kingmen.
joligario
11-06-2011, 09:37 PM
Don't have my system up to test, but a few things.
Not sure if it will break the script, but you don't need the last comma.
Instead of print, you should use quest::say(%dadscookies($text));
Kingmen30264
11-06-2011, 10:02 PM
Alright, so instead of using:
print %dadscookies($text);
I should do something like:
quest::say(%dadscookies($text));
Here is what my NEW code looks like, and it is still not working.
sub EVENT_SAY
{
my $cookie = quest::saylink("Cookie");
my $cookieid = quest::varlink("19732");
my %dadscookies = (
"Hail" => "Would you like a [$cookie]?",
"cookie" => "Yes, a Cookie! Here is what it looks like [$cookieid]",
"no" => "Okay, I will eat it!",
)
quest::say(%dadscookies($text));
}
Once I get ONE of these %hashes working, making my other quest(s) this way should be a breeze :)
Thanks for answering,
Kingmen
joligario
11-06-2011, 10:46 PM
You also don't need the extra comma:
"no" => "Okay, I will eat it!",
lerxst2112
11-06-2011, 11:46 PM
This is a way to do what you want. I added some error handling, so the npc will ignore anything said to it that isn't in the hash.
You had a few errors. A missing ; at the end of the hash statement, using () instead of {} to extract data from the hash, and some case issues.
As you go forward with this it might help to have the npc echo back the text you're sending it, by adding a quest::say($text); at the top. This would help you to see why it wouldn't have worked the way you were doing it. Specifically, hailing sends more than "Hail", which is what the split is for, and your saylink sends "Cookie" but the hash uses "cookie" as a key, which is what the lc() is for. It will also let someone say "No", "no", "nO", etc to the npc and still get the proper result.
sub EVENT_SAY
{
my $cookie = quest::saylink("Cookie");
my $cookieid = quest::varlink("19732");
my %dadscookies = ("hail" => "Would you like a [$cookie]?",
"cookie" => "Yes, a Cookie! Here is what it looks like [$cookieid]",
"no" => "Okay, I will eat it!"
);
my @txt = split(",",lc($text));
if(exists($dadscookies{$txt[0]}))
{
quest::say($dadscookies{$txt[0]});
}
}
Kingmen30264
11-07-2011, 12:40 AM
Thank you very much for helping me with this. This is EXACTLY what I wanted.... But I had one question....
Is it possible to run this without the use of IF commands?
Thanks for all the help,
Kingmen
lerxst2112
11-07-2011, 05:56 AM
I am not sure what you are asking when you say you don't want to use if commands. The only if in that script is to handle the case where the user says something to the npc that it doesn't understand. If you don't care about that and you want the npc to say " whenever that happens then go ahead and remove the if although that makes no sense to me.
trevius
11-07-2011, 06:20 AM
I am not exactly sure what you are wanting to do, but maybe a plugin can help simplify things a bit for you. I haven't tested this at all, but it may work for what you are wanting. Just add following plugin to one of your plugin files in your plugins folder.
#Usage: plugin::EventSay("ExpectedText", "Response");
# Example: plugin::EventSay("Hail", "It Worked!");
sub EventSay {
my $text = plugin::val('$text');
my $CheckText = $_[0];
my $Response = $_[1];
if ($text =~ /$CheckText/i)
{
quest::say("$Response");
}
}
Then, you should be able to test it using this:
Usage Example:
sub EVENT_SAY {
my $cookie = quest::saylink("Cookie");
my $cookieid = quest::varlink("19732");
plugin::EventSay("Hail", "Would you like a [$cookie]?");
plugin::EventSay("cookie", "Yes, a Cookie! Here is what it looks like [$cookieid]");
plugin::EventSay("no", "Okay, I will eat it!");
}
Kingmen30264
11-07-2011, 07:39 AM
Ah, okay. I see what is going on now. I am still in the middle of reading my Perl Book, but I think once I finish it, I will understand this a tad better eheh..
Thanks for the posts everyone... sorry for the ignorance,
Kingmen
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.