Raised This Month: $32 Target: $400
 8% 

Optimizing Code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Whitez
Member
Join Date: Apr 2016
Location: London, UK
Old 05-31-2016 , 14:04   Optimizing Code
Reply With Quote #1

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


Last edited by Whitez; 05-31-2016 at 14:07.
Whitez is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 05-31-2016 , 15:33   Re: Optimizing Code
Reply With Quote #2

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.

Last edited by siriusmd99; 05-31-2016 at 15:33.
siriusmd99 is offline
Whitez
Member
Join Date: Apr 2016
Location: London, UK
Old 05-31-2016 , 17:16   Re: Optimizing Code
Reply With Quote #3

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

Last edited by Whitez; 05-31-2016 at 17:17.
Whitez is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-01-2016 , 04:33   Re: Optimizing Code
Reply With Quote #4

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

siriusmd99 is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 06-01-2016 , 09:58   Re: Optimizing Code
Reply With Quote #5

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...
__________________

Last edited by Napoleon_be; 06-01-2016 at 09:59.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-01-2016 , 10:25   Re: Optimizing Code
Reply With Quote #6

Quote:
Originally Posted by Napoleon_be View Post
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.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-01-2016 , 10:39   Re: Optimizing Code
Reply With Quote #7

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;


Last edited by klippy; 06-01-2016 at 10:40.
klippy is offline
Whitez
Member
Join Date: Apr 2016
Location: London, UK
Old 06-01-2016 , 12:34   Re: Optimizing Code
Reply With Quote #8

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;
    }

Whitez is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-01-2016 , 12:50   Re: Optimizing Code
Reply With Quote #9

Quote:
Originally Posted by Whitez View Post
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.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-01-2016 , 13:52   Re: Optimizing Code
Reply With Quote #10

Quote:
Originally Posted by siriusmd99 View Post
Using set_task is a bad idea.
It's not. If it was, it wouldn't exist.

Quote:
Originally Posted by siriusmd99 View Post
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.

Last edited by klippy; 06-01-2016 at 13:52.
klippy 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 12:46.


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