Raised This Month: $ Target: $400
 0% 

Repeat set_task until a variable is true


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
justincase
Senior Member
Join Date: Dec 2008
Old 10-16-2009 , 14:20   Repeat set_task until a variable is true
Reply With Quote #1

Hello,
I have problems implementing a set_task, which gets repeated as long as a variable is false. This was what I tried first:
Code:
public func1(id, opid) {
    new params[2];
    params[0] = id;
    params[1] = opid;
    
    while (status == false) {
      set_task(5.0, "func2", 0, params, 2);
    }
}

public func2(id, opid) {
    client_print(0, print_chat, "IM IN FUNC2");
    client_print(0, print_chat, "IM IN FUNC2");
    client_print(0, print_chat, "IM IN FUNC2");
}
Result: CS freezes when calling func1().

Then I tried this:
Code:
public func1(id, opid) {
    new params[2];
    params[0] = id;
    params[1] = opid;
    
    set_task(5.0, "func2", 0, params, 2);
}

public func2(id, opid) {
    new params[2];
    params[0] = id;
    params[1] = opid;
    
    if (status == false) {
        set_task(5.0, "func2", 0, params, 2);
    }
    client_print(0, print_chat, "IM IN FUNC2");
    client_print(0, print_chat, "IM IN FUNC2");
    client_print(0, print_chat, "IM IN FUNC2");
}
Result: func2() did not get executed a single time.


The variable "status" is false when calling func1() and gets true in a specific event. The task should be repeated as long as status is false. I can't get it why my attempts are unsuccesfull for this.
justincase is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-16-2009 , 14:36   Re: Repeat set_task until a variable is true
Reply With Quote #2

Your second attempt is ok, but there is a small error:

Code:
public func2 (id, opid)

Should be:

Code:
public func2 (params[])
vitorrd is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-16-2009 , 15:35   Re: Repeat set_task until a variable is true
Reply With Quote #3

You are getting stuck in an infinite loop here:
Code:
    while (status == false) {
      set_task(5.0, "func2", 0, params, 2);
    }
The while statement is continually repeating itself. Try using an if statement, then in func2, do another if statement to do another set_task on func2.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
justincase
Senior Member
Join Date: Dec 2008
Old 10-16-2009 , 15:55   Re: Repeat set_task until a variable is true
Reply With Quote #4

@vitorrd: That was it. Thanks!
justincase is offline
vitorrd
Senior Member
Join Date: Jul 2009
Old 10-17-2009 , 09:33   Re: Repeat set_task until a variable is true
Reply With Quote #5

Quote:
Originally Posted by justincase View Post
@vitorrd: That was it. Thanks!
You're welcome. Read Emp`'s response if you want to know why your first attempt was wrong.
vitorrd is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 10-17-2009 , 09:39   Re: Repeat set_task until a variable is true
Reply With Quote #6

If func1 is also a task, see in TASK_func2 how you pass function params and in which order.

PHP Code:
#define TASKID1 195532
#define TASKID2 295532 // in case you use another task in the same plugin

public func1(idopid) {
    new 
params[2];
    
params[0] = id;
    
params[1] = opid;
    
    
set_task(5.0"TASK_func2"TASKID1params2);
}

public 
TASK_func2(params[], task_id)
{
    
params[0] = id;
    
params[1] = opid;
    
    if (
status == false)
    {
        
set_task(5.0"func2"TASKID1params2);
    }
    
client_print(0print_chat"IM IN FUNC2");
    
client_print(0print_chat"IM IN FUNC2");
    
client_print(0print_chat"IM IN FUNC2");


You can also set a looping task, and stop it when status is true :
PHP Code:
#define TASKID1 195532
#define TASKID2 295532 // in case you use another task in the same plugin

public func1(idopid) {
    new 
params[2];
    
params[0] = id;
    
params[1] = opid;
    
    
set_task(5.0"TASK_func2"0params2"b");
}

public 
TASK_func2(params[], task_id)
{
    
params[0] = id;
    
params[1] = opid;
    
    if (
status == true)
    {
        
remove_task(task_id)
    }
    
client_print(0print_chat"IM IN FUNC2");
    
client_print(0print_chat"IM IN FUNC2");
    
client_print(0print_chat"IM IN FUNC2");

__________________
- tired and retired -

- my plugins -

Last edited by ConnorMcLeod; 10-17-2009 at 09:41.
ConnorMcLeod is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:33.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode