AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Repeat set_task until a variable is true (https://forums.alliedmods.net/showthread.php?t=106555)

justincase 10-16-2009 14:20

Repeat set_task until a variable is true
 
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.

vitorrd 10-16-2009 14:36

Re: Repeat set_task until a variable is true
 
Your second attempt is ok, but there is a small error:

Code:
public func2 (id, opid)

Should be:

Code:
public func2 (params[])

Emp` 10-16-2009 15:35

Re: Repeat set_task until a variable is true
 
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.

justincase 10-16-2009 15:55

Re: Repeat set_task until a variable is true
 
@vitorrd: That was it. Thanks!

vitorrd 10-17-2009 09:33

Re: Repeat set_task until a variable is true
 
Quote:

Originally Posted by justincase (Post 964094)
@vitorrd: That was it. Thanks!

You're welcome. Read Emp`'s response if you want to know why your first attempt was wrong.

ConnorMcLeod 10-17-2009 09:39

Re: Repeat set_task until a variable is true
 
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");




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

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