help with subs

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

Moderator: Moderators

Message
Author
ever_boy_
Developers
Developers
Posts: 308
Joined: 06 Jul 2012, 13:44
Noob?: No

help with subs

#1 Post by ever_boy_ »

I did this sub:

Code: Select all

macro qqq {
	$i = 0
	log @eval(&playerExists)
}
sub playerExists {
	if ($::players{$::playersID[$::i]}) {
		return 1;
	} else {
		return 0;
	}
}
It works just fine if I manually set $::playersID[#number].
But if I set it to [$::i], where $i is given from my macro It always returns 1, even when $i = unexisting ID
So it seems my sub is working, but it's not getting the $i value from my macro, and I just can't figure out why...

Also, I'd like to know if I can set a variable to 'empty'/undefined value inside a macro.

EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: help with subs

#2 Post by EternalHarvest »

Macro variables aren't imported into "main" module where macro subs reside. You need to fetch them from where macro plugin stores them.

iMikeLance
Moderators
Moderators
Posts: 208
Joined: 01 Feb 2010, 17:37
Noob?: No
Location: Brazil - MG
Contact:

Re: help with subs

#3 Post by iMikeLance »

Try this:
$Macro::Data::varStack{'variable_name'}
or
$Macro::Data::varStack{'i'}

ever_boy_
Developers
Developers
Posts: 308
Joined: 06 Jul 2012, 13:44
Noob?: No

Re: help with subs

#4 Post by ever_boy_ »

thank you both, now it works:

Code: Select all

sub playerExists {
	if ($::players{$::playersID[$::Macro::Data::varStack{i}]}) {
		return 1;
	} else {
		return 0;
	}
}
Just one thing, why is it that using eval I can set the var like this:
@eval($::char->{'party'}{'users'}{$::partyUsersID[$sacerID]}{'pos'}{'y'})

while inside a sub it should be used with $macro::data:varstack

Ain't it all perl just the same?

EternalHarvest
Developers
Developers
Posts: 1798
Joined: 05 Dec 2008, 05:42
Noob?: Yes

Re: help with subs

#5 Post by EternalHarvest »

Macro plugin distorts your code in @eval. That's why you usually see that $:: nonsense here, too - and you don't need to write like that in normal Perl.

Post Reply