|
|
 |
 |
 |
 |
|
 |
 |
|
 |
 |
|
 |
|
Quests::Q&A This is the quest support section |
 |
|
 |

11-06-2011, 09:14 PM
|
Hill Giant
|
|
Join Date: Sep 2006
Posts: 112
|
|
Quest Using Hashes (%)
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.
Code:
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.
|
 |
|
 |

11-06-2011, 09:37 PM
|
 |
Developer
|
|
Join Date: Mar 2003
Posts: 1,498
|
|
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));
|

11-06-2011, 10:02 PM
|
Hill Giant
|
|
Join Date: Sep 2006
Posts: 112
|
|
Alright, so instead of using:
Code:
print %dadscookies($text);
I should do something like:
Code:
quest::say(%dadscookies($text));
Here is what my NEW code looks like, and it is still not working.
Code:
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
|

11-06-2011, 10:46 PM
|
 |
Developer
|
|
Join Date: Mar 2003
Posts: 1,498
|
|
You also don't need the extra comma:
"no" => "Okay, I will eat it!",
|
 |
|
 |

11-06-2011, 11:46 PM
|
Demi-God
|
|
Join Date: Aug 2010
Posts: 1,742
|
|
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.
Code:
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]});
}
}
Last edited by lerxst2112; 11-06-2011 at 11:53 PM..
Reason: Added error handling.
|
 |
|
 |

11-07-2011, 12:40 AM
|
Hill Giant
|
|
Join Date: Sep 2006
Posts: 112
|
|
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
|

11-07-2011, 05:56 AM
|
Demi-God
|
|
Join Date: Aug 2010
Posts: 1,742
|
|
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.
|

11-07-2011, 06:20 AM
|
 |
Developer
|
|
Join Date: Aug 2006
Location: USA
Posts: 5,946
|
|
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.
Code:
#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:
Code:
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!");
}
|
 |
|
 |

11-07-2011, 07:39 AM
|
Hill Giant
|
|
Join Date: Sep 2006
Posts: 112
|
|
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
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 07:59 PM.
|
|
 |
|
 |
|
|
|
 |
|
 |
|
 |