Alternative to sleep?

All resolved question will be found here. It is recommended that you browse / search through this section first and see if your question has been answered before

Moderators: Moderators, Documentation Writers

Message
Author
gamenikko
The Way Of Human
The Way Of Human
Posts: 192
Joined: 16 Aug 2009, 03:47
Noob?: Yes
Location: Gonryun

Alternative to sleep?

#1 Post by gamenikko »

sleep pauses everything including the console,
I couldnt find what kore uses to act as a delay.
or something like and event loop to act as a pause, simillar to what macro plugin uses.
Just like old times.

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

Re: Alternative to sleep?

#2 Post by Spherical »


gamenikko
The Way Of Human
The Way Of Human
Posts: 192
Joined: 16 Aug 2009, 03:47
Noob?: Yes
Location: Gonryun

Re: Alternative to sleep?

#3 Post by gamenikko »

I appreciate it, but its rather like spawing a new process with delay rather than using it as a pause,
what if i hooked it to ai_pre? how can i control the timeouts of triggers

ex:

Code: Select all

sub ai_pre {
 Commands::run("c Test");
}
result:
Foo: Test
Foo: Test
Foo: Test
Foo: Test
Foo: Test
Foo: Test
Foo: Test
I need some delay, or timeout if you will. :D
but thanks to your code about Timeout hook, I used it on something but not on this,
I saw someone using
if (timeOut(time, 2))
but I can't get it, I can't find the "timeOut" hook.
Just like old times.

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

Re: Alternative to sleep?

#4 Post by Spherical »

Timeout isnt a hook, I think it's either in utils or tasks, but im not sure. The method in that thread I linked is much better and you should use it, trust me.

gamenikko
The Way Of Human
The Way Of Human
Posts: 192
Joined: 16 Aug 2009, 03:47
Noob?: Yes
Location: Gonryun

Re: Alternative to sleep?

#5 Post by gamenikko »

Spherical wrote:Timeout isnt a hook, I think it's either in utils or tasks, but im not sure. The method in that thread I linked is much better and you should use it, trust me.
nonetheless it's still not working on a sub hooked in ai_pre, it still spams.

Code: Select all

sub act {

	message ("try");
	
}

sub main {
	$taskManager->add(Task::Timeout->new(
               function => sub{act},
               seconds => 5,
               ));
}
result:
try
try
try
try
try
Just like old times.

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

Re: Alternative to sleep?

#6 Post by Spherical »

I don't understand what you're asking for then. No matter what you call in a loop it's going to be called very rapidly you could do something like the following:

Code: Select all

my $control;

//add hook for ai_pre or some other way to get an initial call of the function.

sub AIPre {
	if ( !$control ) {
		&sayHi;
		control = 1;
	}
}

sub sayHi {
	$taskManager->add(Task::Timeout->new(
		object => undef,
		function => \&callSayHi,
		seconds => 1,
		inGame => 1,
		weak => 1
	));
	Commands::run("c Test");
}
However, you would never want to pause one of the main loops (it would cause some major issues such as lack of responsiveness etc. So "pausing" isn't really an option.

gamenikko
The Way Of Human
The Way Of Human
Posts: 192
Joined: 16 Aug 2009, 03:47
Noob?: Yes
Location: Gonryun

Re: Alternative to sleep?

#7 Post by gamenikko »

However, you would never want to pause one of the main loops (it would cause some major issues such as lack of responsiveness etc. So "pausing" isn't really an option.
Yeah I mean I need and event_loop that could be easily used, by the way what I'm trying to do is to add some delay so the bot won't respond to PM's to quickly, kinda words per minute thing.
Just like old times.

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

Re: Alternative to sleep?

#8 Post by Spherical »

gamenikko wrote:
However, you would never want to pause one of the main loops (it would cause some major issues such as lack of responsiveness etc. So "pausing" isn't really an option.
Yeah I mean I need and event_loop that could be easily used, by the way what I'm trying to do is to add some delay so the bot won't respond to PM's to quickly, kinda words per minute thing.
Then you need to be hooking packet_privMsg, don't do anything with the loops. When packet_privMsg is called create a timer with a random delay and have that call your respond function.

by the way packet_privMsg has the following variables:
Plugins::callHook('packet_privMsg', {
privMsgUser => $privMsgUser,
privMsg => $privMsg,
MsgUser => $privMsgUser,
Msg => $privMsg
});

gamenikko
The Way Of Human
The Way Of Human
Posts: 192
Joined: 16 Aug 2009, 03:47
Noob?: Yes
Location: Gonryun

Re: Alternative to sleep?

#9 Post by gamenikko »

Spherical wrote:
gamenikko wrote:
However, you would never want to pause one of the main loops (it would cause some major issues such as lack of responsiveness etc. So "pausing" isn't really an option.
Yeah I mean I need and event_loop that could be easily used, by the way what I'm trying to do is to add some delay so the bot won't respond to PM's to quickly, kinda words per minute thing.
Then you need to be hooking packet_privMsg, don't do anything with the loops. When packet_privMsg is called create a timer with a random delay and have that call your respond function.

by the way packet_privMsg has the following variables:
Plugins::callHook('packet_privMsg', {
privMsgUser => $privMsgUser,
privMsg => $privMsg,
MsgUser => $privMsgUser,
Msg => $privMsg
});
Well thanks :D
Problem solved now,
Just like old times.

Locked