How to use public functions in an include??
#1

Hello,

I was wondering how I can add public functions, like for example OnPlayerConnect() or OnPlayerStreamin() in an include?

I can add them, but the problem is that I get this error:
Код:
error 021: symbol already defined: "OnPlayerConnect"
what can I do about it, except for adding stock OnPlayerConnectEx() or something like that and using this

gamemode:
Код:
public OnPlayerConnect(playerid)
{
    OnPlayerConnectEx(playerid);
}
include:
Код:
stock OnPlayerConnectEx(playerid)
{
    //code you want to execute
}
so this is what I don't want.
I want it to work without needing to change Anything to the gamemode (only adding #include ...)
Reply
#2

This is for example possible with the forum search include

Or with this: https://sampforum.blast.hk/showthread.php?tid=166016
Reply
#3

ok, so now I have this in my Include:


Код:
public OnPlayerConnect(playerid)
{
    if(funcidx("FOnPlayerConnect") != -1)
    {
        return CallLocalFunction("F_OnPlayerConnect", "i", playerid);
    }
    return 1;
}
    
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect F_OnPlayerConnect
forward F_OnPlayerConnect(playerid);

stock F_OnPlayerConnect(playerid)
{
	//code...
}
but I still get this error:
Quote:

\gamemodes\script.pwn(3013) : error 021: symbol already defined: "F_OnPlayerConnect"

Reply
#4

This is the best way to do it:
pawn Код:
static
    bool:gHasOPC = false;

public OnGameModeInit()
{
    gHasOPC = funcidx("F_OnPlayerConnect") != -1;
    if(funcidx("F_OnGameModeInit") != -1)
    {
        return CallLocalFunction("F_OnGameModeInit", "");
    }  
    return 1;
}

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

public OnPlayerConnect(playerid)
{
    //code here
    if(gHasOPC) //faster than using funcidx to check if have OnPlayerConnect callback in your gamemode
    {
        return CallLocalFunction("F_OnPlayerConnect", "i", playerid); //this calls OnPlayerConnect in your gamemode
    }
    //or here
    return 1;
}
   
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect F_OnPlayerConnect
forward F_OnPlayerConnect(playerid);
Now you don't need to change anything in gamemode.
Reply
#5

Or do for example:
- In your script:
pawn Код:
public OnPlayerConnect(playerid)
{
    OnPlayerConnectEx(playerid);
    // Other connect stuff in the script here
    return 1;
}
- In the include:
pawn Код:
forward OnPlayerConnectEx(playerid);
public OnPlayerConnectEx(playerid)
{
    // Do your include-stuff here
}
I'm ensured this also works
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
You don't use F_OnPlayerConnect - that basically renames the NEXT OnPlayerConnect which MAY be the one in the gamemode or one in another library.
I don't really understand what you mean,

I have this now:
Код:
public OnPlayerConnect(playerid) //yes, this is also in the include!
{
	//code
}
public OnPlayerConnect(playerid)
{
    if(funcidx("F_OnPlayerConnect") != -1)
    {
        return CallLocalFunction("F_OnPlayerConnect", "i", playerid);
    }
    return 1;
}
    
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect F_OnPlayerConnect
forward F_OnPlayerConnect(playerid);
that's in the include

but now I get error 021: symbol already defined: "OnPlayerConnect" (error occurs in the include)
Reply
#7

Are you sure you have defines ALS_OnPlayerConnected?

Try my way btw.
Reply
#8

You will need to use the next OnPlayerConnect after the first one + it's definitions.

Also, you're only supposed to use the callback ONCE, the #define thingies are there to not generate errors the next time the CB gets called. And make sure the code gets reached ofc.

pawn Код:
public OnPlayerConnect(playerid)
{
    if(funcidx("F_OnPlayerConnect") != -1)
    {
        return CallLocalFunction("F_OnPlayerConnect", "i", playerid);
    }
    return 1;
}
   
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect F_OnPlayerConnect
forward F_OnPlayerConnect(playerid);

//In a different script (Or at least not the include)

public OnPlayerConnect(playerid)
{
    //code
}
Reply
#9

oh thanks! I get it now, thank you very much

it's working perfectly, but I don't understand yet what _ALS_OnPlayerConnect means
where does ALS stand for?
It seems to work without defining _ALS_OnPlayerConnect so I don't understand why it should be added



and it also works without
Код:
#if defined _ALS_OnPlayerConnect
    #undef OnPlayerConnect
#else
    #define _ALS_OnPlayerConnect
#endif
so with only this:
Код:
#define OnPlayerConnect F_OnPlayerConnect
forward F_OnPlayerConnect(playerid);
because the new #define automaticly overwrites OnPlayerConnect, so it doesn't need to be undefined first?
Reply
#10

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
Or do for example:
- In your script:
pawn Код:
public OnPlayerConnect(playerid)
{
    OnPlayerConnectEx(playerid);
    // Other connect stuff in the script here
    return 1;
}
- In the include:
pawn Код:
forward OnPlayerConnectEx(playerid);
public OnPlayerConnectEx(playerid)
{
    // Do your include-stuff here
}
I'm ensured this also works
so... this basically hooks the OnPlayerConnect(playerid) callback to the include right..?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)