[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


Messages In This Thread
OnPlayerUpdate by state - by Gryphus One - 13.08.2012, 17:12
Re: OnPlayerUpdate by state - by Kaperstone - 13.08.2012, 20:09
Respuesta: Re: OnPlayerUpdate by state - by Gryphus One - 13.08.2012, 20:15
Re: Respuesta: Re: OnPlayerUpdate by state - by Kaperstone - 13.08.2012, 20:36
Re: OnPlayerUpdate by state - by Vince - 13.08.2012, 20:52
Respuesta: Re: OnPlayerUpdate by state - by Gryphus One - 13.08.2012, 20:58
Re: OnPlayerUpdate by state - by wups - 13.08.2012, 21:09
Respuesta: Re: OnPlayerUpdate by state - by Gryphus One - 13.08.2012, 21:14
Respuesta: Re: OnPlayerUpdate by state - by Gryphus One - 14.08.2012, 14:22

Forum Jump:


Users browsing this thread: 2 Guest(s)