Perl Help

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

Moderator: Moderators

Message
Author
beststorm
Noob
Noob
Posts: 5
Joined: 03 Nov 2009, 19:42
Noob?: No

Perl Help

#1 Post by beststorm »

I'm writing a plugin. I need it to wait 60 seconds after running a command and run it again... and again...

I tried to use "sleep 60;", but that pauses the entire program, and it stop sending the sync packets, so the openkore disconects. How could i get the same behavior? It would be nice to stop only the plugin.

Also, i need this function to be running always, or at least from 60 to 60 seconds. I'm using the "mainLoop_post" hook, but probably isnt the best option. I would like to get some advices.

flashdbest
Plain Yogurt
Plain Yogurt
Posts: 61
Joined: 12 Nov 2012, 09:22
Noob?: Yes

Re: Perl Help

#2 Post by flashdbest »

EternalHarvest wrote: Spawning Task::Timeout with your function should do, like in Actor::setStatus.

beststorm
Noob
Noob
Posts: 5
Joined: 03 Nov 2009, 19:42
Noob?: No

Re: Perl Help

#3 Post by beststorm »

Can't use string ("mainLoop_post") as a HASH ref while "strict refs" in use line 104

line 104 -> $taskManager->add(Task::Timeout->new(
object => $self->{statuses}{$handle},
weak => 1,
,#now
seconds => $tick / 1000 - 1,
));


Sorry, i'm new to perl.

Kaspy
Halfway to Eternity
Halfway to Eternity
Posts: 398
Joined: 08 Jun 2012, 15:42
Noob?: No
Location: Brazil

Re: Perl Help

#4 Post by Kaspy »

It would be better to display the entire plugin, and not only a part...
But, because you are leaving a blank line, with only the comma?
line 104 -> $taskManager->add(Task::Timeout->new(
object => $self->{statuses}{$handle},
weak => 1,
,#now
seconds => $tick / 1000 - 1,
));
Image

Spherical
Human
Human
Posts: 29
Joined: 03 Jan 2013, 00:05
Noob?: No

Re: Perl Help

#5 Post by Spherical »

Well, lets see if you made the call correctly (since iRO is down at the moment I can't test to see if I'm right or not).
line 104 -> $taskManager->add(Task::Timeout->new(
object => $self->{statuses}{$handle},
weak => 1,
,#now
seconds => $tick / 1000 - 1,
));
The function Task::Timeout is as follows:

Code: Select all

sub new {
	my ($class, %args) = @_;
	
	my $function = $args{function};
	$args{function} = sub {
		&{$function};
		($_[0] && $_[0]->isa('Task') ? $_[0] : $_[1])->setDone;
	};
	
	my $self = $class->SUPER::new(tasks => [
		Task::Wait->new(%args),
		Task::Function->new(%args),
	], %args);
	
	$self->{stop} = $args{stop};
	
	$self;
}
Therefore you will need to define {function} and {stop} (optional), however this also calls: Task::Wait which as as follows:

Code: Select all

sub new {
	my $class = shift;
	my %args = @_;
	my $self = $class->SUPER::new(@_);

	$self->{wait}{timeout} = $args{seconds};
	$self->{inGame} = defined($args{inGame}) ? $args{inGame} : 1;

	return $self;
}
Meaning you will need to define {seconds} and {inGame} (optional). Finally, it also calls Task::Function which is as follows:

Code: Select all

sub new {
	my $class = shift;
	my %args = @_;
	my $self = $class->SUPER::new(@_);

	if (!$args{function}) {
		ArgumentException->throw("No function argument given.");
	}
	$self->{function} = $args{function};
	if ($args{object}) {
		$self->{object} = $args{object};
		Scalar::Util::weaken($self->{object}) if ($args{weak});
	}
	return $self;
}
This needs {object} to be defined and {weak} (optional) This also needs object to be placed first.

So I believe a correct call would be:

Code: Select all

$taskManager->add(Task::Timeout->new(
object => $self->{statuses}{$handle},
function => &somefunction,
seconds => 1,
inGame => 1,
weak => 1
));
note: if function isnt a method of a class you can set object to undef (I think anyway).

Kaspy
Halfway to Eternity
Halfway to Eternity
Posts: 398
Joined: 08 Jun 2012, 15:42
Noob?: No
Location: Brazil

Re: Perl Help

#6 Post by Kaspy »

Another way is to follow the example of the cases that the same function has been used or go read the comments in the file.
Image

Spherical
Human
Human
Posts: 29
Joined: 03 Jan 2013, 00:05
Noob?: No

Re: Perl Help

#7 Post by Spherical »

Here's how I am calling it in my current plugin:

Code: Select all

				$taskManager->add(Task::Timeout->new(
					object => undef,
					function => sub{ \&castSkill(272) },
					seconds => (((200-$char->{'attack_speed'})/50)+.05),
					inGame => 1,
					weak => 1
					));

Post Reply