SA-MP Forums Archive
Is this a good idea? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Is this a good idea? (/showthread.php?tid=284049)



Is this a good idea? - iPLEOMAX - 17.09.2011

So I want OnPlayerUpdate to get called only ONCE per second and NOT when the player is AFK.
I know it gets called very often, but who cares? It will only continue the rest if g_switch is true, right?

This is what i did: (lol)

pawn Код:
#include <a_samp>

new bool:g_switch[MAX_PLAYERS];

public OnFilterScriptInit()
{
    SetTimer("SwitchVar", 1000, true);
    return true;
}

forward SwitchVar();
public SwitchVar()
{
    for(new i=0; i<MAX_PLAYERS; i++) //I don't wanna use foreach for this. :)
    if(IsPlayerConnected(i) && !IsPlayerNPC(i)) g_switch[i] = true;
    return true;
}

public OnPlayerUpdate(playerid)
{
    if(g_switch[playerid])
    {
        SendClientMessage(playerid, -1, "Update/sec..");
    }
   
    g_switch[playerid] = false;
    return true;
}
Seems to work JUST FINE. The way I want it to. And my functions work without any bug.

The question is: Is there a better way to do it?

Reply ONLY if you understand or else ignore it.

PS: Don't tell me to rename OnPlayerUpdate and use a timer+loop.


Re: Is this a good idea? - TheArcher - 17.09.2011

OnPlayerUpdate doesn't call the commands per second but per 100ms if i'm not wrong.

Edit: I saw your script after my answer.

pawn Код:
if(g_switch[playerid])
    {
        SendClientMessage(playerid, -1, "Update/sec..");
    }
1° I think this make no sense. In that case will update the message a lot every second when g_switch is called, but it must be called with true or false at the end.
2° return true / return false, is not equal to return 1 / return 0 .


Re: Is this a good idea? - wouter0100 - 17.09.2011

He want to call the sendclientmessage only when the player is active (walking etc) and not faster then 1 second, i think this is the best way.


Re: Is this a good idea? - iPLEOMAX - 17.09.2011

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
OnPlayerUpdate doesn't call the commands per second but per 100ms if i'm not wrong.
Read again..


Quote:
Originally Posted by wouter0100
Посмотреть сообщение
He want to call the sendclientmessage only when the player is active (walking etc) and not faster then 1 second, i think this is the best way.
Exactly.


Re: Is this a good idea? - TheArcher - 17.09.2011

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
Read again..
Watch my edit.


Re: Is this a good idea? - iPLEOMAX - 17.09.2011

Quote:
Originally Posted by TheArcher
Посмотреть сообщение
1° I think this make no sense. In that case will update the message a lot every second when g_switch is called, but it must be called with true or false at the end.
2° return true / return false, is not equal to return 1 / return 0 .
1. It does make sense only if you try to understand it.

OnPlayerUpdate will call the rest of it's functions if g_switch is true.
which means until the timer ticks after a second it won't continue.
Inshort it will make OnPlayerUpdate perform only per second but not when player is afk. (I know it cause it works.)

2. return true = return 1, return false = return 0....

I'm not here to solve my problem cause I don't have any. I just wanna see if there are better methods.


Re: Is this a good idea? - wouter0100 - 17.09.2011

Or this:

pawn Код:
#include <a_samp>

new bool:g_switch[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    g_switch[playerid] = true;
    return true;
}

forward SwitchVar(playerid);
public SwitchVar(playerid)
{
    if(IsPlayerConnected(playerid) && !IsPlayerNPC(playerid)) g_switch[playerid] = true;
    return true;
}

public OnPlayerUpdate(playerid)
{
    if(g_switch[playerid])
    {
        g_switch[playerid] = false;
        SetTimerEx("SwitchVar", 1000, false, "i", playerid);
        SendClientMessage(playerid, -1, "Update/sec..");
    }
    return true;
}
But with lots of players in your server, its laggy (i think)


Re: Is this a good idea? - iPLEOMAX - 17.09.2011

@wouter:

That can launch multiple timers (because of different players) which isn't good compared to a single timer..


Re: Is this a good idea? - wouter0100 - 17.09.2011

I know, what you have, is the best way.


Re: Is this a good idea? - TheArcher - 17.09.2011

Quote:
Originally Posted by iPLEOMAX
Посмотреть сообщение
1. It does make sense only if you try to understand it.

OnPlayerUpdate will call the rest of it's functions if g_switch is true.
which means until the timer ticks after a second it won't continue.
Inshort it will make OnPlayerUpdate perform only per second but not when player is afk. (I know it cause it works.)

2. return true = return 1, return false = return 0....

I'm not here to solve my problem cause I don't have any. I just wanna see if there are better methods.
I didn't see your Timer. LAWL srry.