Confused with $monstersList, @monstersID, %monsters

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Confused with $monstersList, @monstersID, %monsters

#1 Post by sofax222 »

I am confused with three global variables, $monstersList, @monstersID and %monsters.
What differences in these three global variables, $monstersList, @monstersID and %monsters ?

For example:

my $monster1 = $monstersList->getByID($mID);
my $monster2 = $monsters{$mID};

Is $monster1 the same with $monster2 ?

Then, how about the @monstersID ?

When do "$monstersList->clear();" in function.pl, does nead to clear the %monsters and @monstersID ?
EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: Confused with $monstersList, @monstersID, %monsters

#2 Post by EternalHarvest »

$monstersList is an ActorList, indexed by server ID ($actor->{ID}).
Usually you need to use this list, with ->getItems or ->getByID.

@monstersID is an array of the same Actor objects, but indexed by openkore's UI ID (usually something like 0, 1, 2...; $actor->{binID}).
This list is useful when you need to find monster by that user interface ID, like when handling a console command.

%monsters is a hash indexed by server ID. It doesn't do anything new (you should use $monstersList) and it's deprecated, see the related commit: http://openkore.svn.sourceforge.net/vie ... ision=4305

The same is for other similar lists (items, players etc).
sofax222 wrote:When do "$monstersList->clear();" in function.pl, does nead to clear the %monsters and @monstersID ?
You should not change @monstersID or %monsters anywhere since they're being maintained automatically with this:

Code: Select all

	foreach my $list ($itemsList, $monstersList, $playersList, $petsList, $npcsList, $portalsList, $slavesList) {
		$list->onAdd()->add(undef, \&actorAdded);
		$list->onRemove()->add(undef, \&actorRemoved);
		$list->onClearBegin()->add(undef, \&actorListClearing);
	}
sofax222
Developers
Developers
Posts: 214
Joined: 24 Nov 2010, 03:08
Noob?: Yes

Re: Confused with $monstersList, @monstersID, %monsters

#3 Post by sofax222 »

I got it !!
Thanks a lot !