[Include] OnPlayerUpdate by state
#1

Hi, this is my very first release, it's nothing from another world but maybe someone finds it useful.

Usually many code lines are executed in OnPlayerUpdate, but many times some of those lines are to be executed only for determined player states, like when the player is on foot, or when he's driving, or both... For example, if you want to check if the player has hacked a jetpack you only need to do it when he's on foot, or if you want to search for a hacked minigun you only need to do it when he's on foot or passenger because he won't be able to shoot it when driving.
This usually forces us to put some conditionals depending on the state of the player, but if we have separate callbacks for the player updating in each state we can have our script clearer to see and more organized, so this is what this include allows us to.
It adds 6 new callbacks:
  • OnPlayerUpdateOnFoot(playerid)
  • OnPlayerUpdateDriver(playerid)
  • OnPlayerUpdatePassenger(playerid)
  • OPUOnFootOrDriver(playerid)
  • OPUOnFootOrPassenger(playerid)
  • OPUDriverOrPassenger(playerid)
To use them, you only need to include this file in your gamemode/filterscript, and add the callbacks you want as you would with a native one. IMPORTANT NOTE: the lines you add inside these custom callbacks will be executed BEFORE those you have inside OnPlayerUpdate.

Just copy the following code and paste it into a new file, and save it with the .inc extension in your include folder.

pawn Код:
#if defined _OPUByState_included
    #endinput
#endif
#define _OPUByState_included

#include <a_samp>

forward OnPlayerUpdateOnFoot(playerid);
forward OnPlayerUpdateDriver(playerid);
forward OnPlayerUpdatePassenger(playerid);
forward OPUOnFootOrDriver(playerid);
forward OPUOnFootOrPassenger(playerid);
forward OPUDriverOrPassenger(playerid);

static gOPUBS_HasCB[7];

public OnGameModeInit()
{
    gOPUBS_HasCB[0] = funcidx("OPUBS_OnPlayerUpdate") != -1;
    gOPUBS_HasCB[1] = funcidx("OPUBS_OnPlayerUpdateOnFoot") != -1;
    gOPUBS_HasCB[2] = funcidx("OPUBS_OnPlayerUpdateDriver") != -1;
    gOPUBS_HasCB[3] = funcidx("OPUBS_OnPlayerUpdatePassenger") != -1;
    gOPUBS_HasCB[4] = funcidx("OPUBS_OPUOnFootOrDriver") != -1;
    gOPUBS_HasCB[5] = funcidx("OPUBS_OPUOnFootOrPassenger") != -1;
    gOPUBS_HasCB[6] = funcidx("OPUBS_OPUDriverOrPassenger") != -1;
    if (funcidx("OPUBS_OnGameModeInit") != -1)
    {
        return CallLocalFunction("OPUBS_OnGameModeInit", "");
    }
    return 1;
}

#if defined _ALS_OnGameModeInit
    #undef OnGameModeInit
#else
    #define _ALS_OnGameModeInit
#endif
#define OnGameModeInit OPUBS_OnGameModeInit
forward OPUBS_OnGameModeInit();

public OnPlayerUpdate(playerid)
{
    switch( GetPlayerState(playerid) )
    {
        case PLAYER_STATE_ONFOOT:
        {
            OnPlayerUpdateOnFoot(playerid);
            OPUOnFootOrDriver(playerid);
            OPUOnFootOrPassenger(playerid);
        }
        case PLAYER_STATE_DRIVER:
        {
            OnPlayerUpdateDriver(playerid);
            OPUOnFootOrDriver(playerid);
            OPUDriverOrPassenger(playerid);
        }
        case PLAYER_STATE_PASSENGER:
        {
            OnPlayerUpdatePassenger(playerid);
            OPUOnFootOrPassenger(playerid);
            OPUDriverOrPassenger(playerid);
        }
    }

    if (gOPUBS_HasCB[0])
    {
        return CallLocalFunction("OPUBS_OnPlayerUpdate", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUpdate
    #undef OnPlayerUpdate
#else
    #define _ALS_OnPlayerUpdate
#endif
#define OnPlayerUpdate OPUBS_OnPlayerUpdate
forward OPUBS_OnPlayerUpdate(playerid);

public OnPlayerUpdateOnFoot(playerid)
{
    if (gOPUBS_HasCB[1])
    {
        return CallLocalFunction("OPUBS_OnPlayerUpdateOnFoot", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUpdateOnFoot
    #undef OnPlayerUpdateOnFoot
#else
    #define _ALS_OnPlayerUpdateOnFoot
#endif
#define OnPlayerUpdateOnFoot OPUBS_OnPlayerUpdateOnFoot
forward OPUBS_OnPlayerUpdateOnFoot(playerid);

public OnPlayerUpdateDriver(playerid)
{
    if (gOPUBS_HasCB[2])
    {
        return CallLocalFunction("OPUBS_OnPlayerUpdateDriver", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUpdateDriver
    #undef OnPlayerUpdateDriver
#else
    #define _ALS_OnPlayerUpdateDriver
#endif
#define OnPlayerUpdateDriver OPUBS_OnPlayerUpdateDriver
forward OPUBS_OnPlayerUpdateDriver(playerid);

public OnPlayerUpdatePassenger(playerid)
{
    if (gOPUBS_HasCB[3])
    {
        return CallLocalFunction("OPUBS_OnPlayerUpdatePassenger", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OnPlayerUpdatePassenger
    #undef OnPlayerUpdatePassenger
#else
    #define _ALS_OnPlayerUpdatePassenger
#endif
#define OnPlayerUpdatePassenger OPUBS_OnPlayerUpdatePassenger
forward OPUBS_OnPlayerUpdatePassenger(playerid);

public OPUOnFootOrDriver(playerid)
{
    if (gOPUBS_HasCB[4])
    {
        return CallLocalFunction("OPUBS_OPUOnFootOrDriver", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OPUOnFootOrDriver
    #undef OPUOnFootOrDriver
#else
    #define _ALS_OPUOnFootOrDriver
#endif
#define OPUOnFootOrDriver OPUBS_OPUOnFootOrDriver
forward OPUBS_OPUOnFootOrDriver(playerid);

public OPUOnFootOrPassenger(playerid)
{
    if (gOPUBS_HasCB[5])
    {
        return CallLocalFunction("OPUBS_OPUOnFootOrPassenger", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OPUOnFootOrPassenger
    #undef OPUOnFootOrPassenger
#else
    #define _ALS_OPUOnFootOrPassenger
#endif
#define OPUOnFootOrPassenger OPUBS_OPUOnFootOrPassenger
forward OPUBS_OPUOnFootOrPassenger(playerid);

public OPUDriverOrPassenger(playerid)
{
    if (gOPUBS_HasCB[6])
    {
        return CallLocalFunction("OPUBS_OPUDriverOrPassenger", "i",playerid);
    }
    return 1;
}
#if defined _ALS_OPUDriverOrPassenger
    #undef OPUDriverOrPassenger
#else
    #define _ALS_OPUDriverOrPassenger
#endif
#define OPUDriverOrPassenger OPUBS_OPUDriverOrPassenger
forward OPUBS_OPUDriverOrPassenger(playerid);
Reply
#2

Isn't using OnPlayerUpdate callback easier than using a specific player update callback ?
Reply
#3

Quote:
Originally Posted by xkirill
Посмотреть сообщение
Isn't using OnPlayerUpdate callback easier than using a specific player update callback ?
Depends: if you have some pieces of code that should be executed only when the player is in a certain state, you can have your script better organized if you have these callbacks. For example, if you have a piece of code that bans a player if he uses a jetpack, you better put it in OnPlayerUpdateOnFoot because he won't be using one while driving or being a passenger.
Reply
#4

Quote:
Originally Posted by Gryphus One
Посмотреть сообщение
Depends: if you have some pieces of code that should be executed only when the player is in a certain state, you can have your script better organized if you have these callbacks. For example, if you have a piece of code that bans a player if he uses a jetpack, you better put it in OnPlayerUpdateOnFoot because he won't be using one while driving or being a passenger.
he can spawn jetpack while being in vehicle.
anyway,nice include.
Reply
#5

This is just going to cause severe lag because CallLocalFunction is incredibly slow! To whom it may concern: I do not recommend using this.
Reply
#6

Quote:
Originally Posted by xkirill
Посмотреть сообщение
he can spawn jetpack while being in vehicle.
Wow is that possible? but I guess in that case he would be ejected from the vehicle and his state would be on foot.
Anyway, jetpack was just an example.

Quote:
Originally Posted by xkirill
Посмотреть сообщение
anyway,nice include.
Thanks.

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is just going to cause severe lag because CallLocalFunction is incredibly slow! To whom it may concern: I do not recommend using this.
Well I know ****** released recently a new way to hook callbacks without using ALS, but I'm not used to it yet so I preferred to use a way that I know a bit better. In any case, ALS has been widely used for years with no problems.
Btw I agree with your signature about knowledge and money.
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is just going to cause severe lag because CallLocalFunction is incredibly slow! To whom it may concern: I do not recommend using this.
It ain't THAT slow, but this code definitely IS slow. You call a callback by a direct function call, and then use CallLocalFunction.
+ The hooks aren't needed(except for the OnPlayerUpdate one).
Reply
#8

Quote:
Originally Posted by wups
Посмотреть сообщение
It ain't THAT slow, but this code definitely IS slow. You call a callback by a direct function call, and then use CallLocalFunction.
+ The hooks aren't needed(except for the OnPlayerUpdate one).
Well one of the reasons why I released this is to get opinions and learn from other people who may see wrong things in my scripts. So how would you do this?
Reply
#9

Quote:
Originally Posted by wups
Посмотреть сообщение
It ain't THAT slow, but this code definitely IS slow. You call a callback by a direct function call, and then use CallLocalFunction.
+ The hooks aren't needed(except for the OnPlayerUpdate one).
CallLocalFunction is part of ALS, and as I said before, ALS has been widely used for years with no problems.
Also, I just tried a new version of my include in which I hook only OnPlayerUpdate and not the other callbacks, and unless I put all the callbacks in the gamemode, when I try to compile I get a full load of errors, all of them of the same type:

Код:
: error 004: function "OnPlayerUpdateOnFoot" is not implemented
: error 004: function "OPUOnFootOrDriver" is not implemented
: error 004: function "OPUOnFootOrPassenger" is not implemented
: error 004: function "OnPlayerUpdateDriver" is not implemented
: error 004: function "OPUOnFootOrDriver" is not implemented
: error 004: function "OPUDriverOrPassenger" is not implemented
: error 004: function "OnPlayerUpdatePassenger" is not implemented
: error 004: function "OPUOnFootOrPassenger" is not implemented
: error 004: function "OPUDriverOrPassenger" is not implemented
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


9 Errors.
I don't want people to be forced to add all the 6 callbacks in their gamemodes if they aren't gonna use all of them, so all those hooks are necessary, unless someone posts here how to optimize this a bit better.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)