Log in

View Full Version : Array Problems


Randymarsh9
02-06-2013, 12:24 AM
I am hoping to be able to do some sort of system where I can have an array of quest rewards for each class. This is a very simple example of what I am trying to do.

sub EVENT_SAY
{
@Shadowknight = (1001,1002,1003);
@itemArray = "@$class";
my $summon = quest::saylink("summon");
if ($text =~/hail/i)
{
quest::say("Do you want me to $summon you something?");
}
elsif ($text =~/summon/i)
{
quest::say("$class");
quest::say("@itemArray");
quest::summonitem($itemArray[1]);
}
}

Ideally, if someone completes the quest, I could just have it choose their reward from their class's array. The problem I have already seen though, is that getting any array element other than the one at the 0 location returns a 0. So in the script I copied above, it will correctly state the character's class, then (since I am a Shadowknight) it will list all of the elements in the shadowknight array. The problem comes with retrieving individual elements. When I do summonitem($itemArray[0]), it summons an item; however, if I do quest::summonitem($itemArray[1]), it says there is no item with ID = 0. Does anyone know why perl is only recognizing the first element of the array?

c0ncrete
02-06-2013, 12:40 AM
you probably would be better off using a hash of arrays.

my $reward = {
Shadowknight => [1001, 1002, 1003],
Druid => [1004, 1005, 1006],
# etc
};

quest::say($class);
quest::summonitem($reward->{$class}->[1]);

assuming you want it to choose a random element from the array, you'd use
quest::summonitem( quest::ChooseRandom( @{ $reward->{$class} } ) );
or
quest::summonitem( $reward->{$class}->[ rand int @{ $reward->{$class} } ] );

Dunge0nMastr
02-06-2013, 12:40 AM
This is what i use to reward based on class:


my %Reward = ("Warrior" => '132639', "Rogue" => '132667', "Monk" => '132672', "Berserker" => '132671', "Shadowknight" => '132670', "Paladin" => '132669', "Ranger" => '132662', "Bard" => '132668', "Beastlord" => '132673', "Cleric" => '132640', "Druid" => '132650', "Shaman" => '132651', "Wizard" => '132663', "Magician" => '132664', "Enchanter" => '132666', "Necromancer" => '132665');


then:


quest::summonitem($Reward{$class});


obviously ignore the IDs i have in place :P

Randymarsh9
02-06-2013, 01:21 AM
Thanks for the quick responses. I like both of those methods a lot, so I'll bookmark this page haha.

c0ncrete
02-06-2013, 01:29 AM
you rarely ever want to use variables in variable names, even if you really think you do. it's generally not worth the headache.

http://perl.plover.com/varvarname.html