AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimizing Code (https://forums.alliedmods.net/showthread.php?t=283339)

Whitez 05-31-2016 14:04

Optimizing Code
 
Is there any better way to do this without using 3 set tasks?
Note each user has 3 personal channels

PHP Code:


new UserChannelUsed[33][3]

public 
FunctionCalled(id)
{
    if(!
UserChannelUsed[id][0])
    {
        
send message idchannel 0
        
        UserChannelUsed
[id][0] = true
        set_task
(1.5"reset-channel0"id
    }
    else if(!
UserChannelUsed[id][1])
    {
        
send message idchannel 1
        
        UserChannelUsed
[id][1] = true
        set_task
(1.5"reset-channel1"id
    }
    else if(!
UserChannelUsed[id][2])
    {
        
UserChannelUsed[id][2] = true
        send message id
channel 2
        set_task
(1.5"reset-channel2"id
    }
    else return
}

public 
reset-channel0(id)
{
    
UserChannelUsed[id][0] = false
}

public 
reset-channel1(id)
{
    
UserChannelUsed[id][1] = false
}

public 
reset-channel2(id)
{
    
UserChannelUsed[id][2] = false



siriusmd99 05-31-2016 15:33

Re: Optimizing Code
 
why not using unidimenaional array?

new g_channel[33]

then you just do:

if(g_channel[id] < 3){
//do something
g_channel[id]++
set_task(1.5, "reset_channel", id)
}

public reset_channel(id){
if(g_channel[id])
g_channel[id]--;
}

Every time player uses command it will add 1 ,and when it will be 3 then he won't be able to use , he will wait for task to decrease the value.

Whitez 05-31-2016 17:16

Re: Optimizing Code
 
Because if channel two is in use and channel one is free, i want channel one to be used instead of channel three

Channels are actualy coordinates where the message to be displayed :)

siriusmd99 06-01-2016 04:33

Re: Optimizing Code
 
PHP Code:


#define C1 5363
#define C2 3422

new UserChannelUsed[33][3]

public 
FunctionCalled(id)
{
     new 
tmp[2]
      
tmp[0]=id
    if(!
UserChannelUsed[id][0])
    {
        
send message idchannel 0
        UserChannelUsed
[id][0] = true
        set_task
(1.5"reset-channel"idtmp2
    }
    else if(!
UserChannelUsed[id][1])
    {
        
send message idchannel 1
        tmp
[1]=1;
        
UserChannelUsed[id][1] = true
        set_task
(1.5"reset-channel"id+C1,tmp,2
    }
    else if(!
UserChannelUsed[id][2])
    {
        
UserChannelUsed[id][2] = true
        send message id
channel 2
        tmp
[1]=2;
        
set_task(1.5"reset-channel"id+C2tmp2
    }
    else return
}

public 
reset-channel(info[])
{
    
UserChannelUsed[info[0]][info[1]] = false



Napoleon_be 06-01-2016 09:58

Re: Optimizing Code
 
What you could do is set just 1 task in your public FunctionCalled(id)

Then in your task function, you could check everything in there and set everything to true or false.

Anyways, there should indeed be a more proper way but i have no clue how to do that, guess you gotta work with constants & for loops instead.

@siriusmd99, you're still using 3 tasks...

siriusmd99 06-01-2016 10:25

Re: Optimizing Code
 
Quote:

Originally Posted by Napoleon_be (Post 2423786)
What you could do is set just 1 task in your public FunctionCalled(id)

Then in your task function, you could check everything in there and set everything to true or false.

Anyways, there should indeed be a more proper way but i have no clue how to do that, guess you gotta work with constants & for loops instead.

@siriusmd99, you're still using 3 tasks...

It cannot be done without using 3 tasks , i just made 1 public function.

When you use one channel a task has been set, now if you use it again after 1.0 second with the same task then first channel won't be cleared because first task has been removed due to setting second task.
There shall work 3 tasks anyway.

klippy 06-01-2016 10:39

Re: Optimizing Code
 
This should do it. It's much less hardcoded so you can change the channel count just by changing one constant.
PHP Code:

#define CHANNEL_COUNT    3
new g_isChannelUsed[33][CHANNEL_COUNT];

public 
MyCallback(id)
{
    for(new 
0CHANNEL_COUNTi++)
    {
        if(!
g_isChannelUsed[id][i])
        {
            
// Send message using channel 'i', or whatever
            
g_isChannelUsed[id][i] = true;
            
            new 
taskData[2];
            
taskData[0] = id;
            
taskData[1] = i;
            
set_task(1.5"Task_ResetChannel"0taskDatasizeof(taskData));
            break;
        }
    }
}

public 
Task_ResetChannel(taskData[])
{
    
g_isChannelUsed[taskData[0]][taskData[1]] = false;



Whitez 06-01-2016 12:34

Re: Optimizing Code
 
What about this?

PHP Code:

public my_function(id)
{
    
CurrentTime get_gametime()
    
    if(
UserChannelUsed[id][0] < CurrentTime)
    {
        
send message idchannel 0
        
        UserChannelUsed
[id][0] = CurrentTime 1.5)
    }
    else if(
UserChannelUsed[id][1] < CurrentTime)
    {
        
send message idchannel 1
        
        UserChannelUsed
[id][1] = CurrentTime 1.5)
    }
    else if(
UserChannelUsed[id][2] < CurrentTime)
    {
        
send message idchannel
        
        UserChannelUsed
[id][2] = CurrentTime 1.5)
    }
    else
    {
        return;
    }



siriusmd99 06-01-2016 12:50

Re: Optimizing Code
 
Quote:

Originally Posted by Whitez (Post 2423839)
What about this?

PHP Code:

public my_function(id)
{
    
CurrentTime get_gametime()
    
    if(
UserChannelUsed[id][0] < CurrentTime)
    {
        
send message idchannel 0
        
        UserChannelUsed
[id][0] = CurrentTime 1.5)
    }
    else if(
UserChannelUsed[id][1] < CurrentTime)
    {
        
send message idchannel 1
        
        UserChannelUsed
[id][1] = CurrentTime 1.5)
    }
    else if(
UserChannelUsed[id][2] < CurrentTime)
    {
        
send message idchannel
        
        UserChannelUsed
[id][2] = CurrentTime 1.5)
    }
    else
    {
        return;
    }




Yes, this is the best way.

Using set_task is a bad idea.
Klippy , your plugin is not correct.
You set a task with the same id ( 0 ) and previous task will be reseted ..

Whitez, use get_gametime instead, nice idea.

klippy 06-01-2016 13:52

Re: Optimizing Code
 
Quote:

Originally Posted by siriusmd99 (Post 2423847)
Using set_task is a bad idea.

It's not. If it was, it wouldn't exist.

Quote:

Originally Posted by siriusmd99 (Post 2423847)
Klippy , your plugin is not correct.
You set a task with the same id ( 0 ) and previous task will be reseted ..

We just had a thread recently where we talked about task IDs. If you don't intend to remove a task via remove_task() native, task ID doesn't matter at all, no task will get reset.


All times are GMT -4. The time now is 18:43.

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