open.mp forum
[Pawn] symbol already defined: "@yH_OnScriptInit@003" - Printable Version

+- open.mp forum (https://forum.open.mp)
+-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
+--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
+--- Thread: [Pawn] symbol already defined: "@yH_OnScriptInit@003" (/showthread.php?tid=1124)



symbol already defined: "@yH_OnScriptInit@003" - Zow - 2020-06-24

I tried to hook OnScriptInit
Here my main.pwn


Code:
#include <a_samp> #undef ? MAX_PLAYERS #define MAX_PLAYERS 100 #define YSI_NO_OPTIMISATION_MESSAGE #define YSI_NO_CACHE_MESSAGE #define YSI_NO_MODE_CACHE #define YSI_NO_HEAP_MALLOC #define YSI_NO_VERSION_CHECK #include <a_mysql> #include? ? <streamer> #include? ? <sscanf2> #include? ? <Pawn.CMD> // YSI-Include 5.x #include <YSI_Data/y_iterate> #include <YSI_Coding/y_timers> #include "test.pwn" #include "test1.pwn"


test.pwn


Code:
#include <YSI_Coding/y_hooks> hook OnScriptInit() { ? ? print("test1"); ? ? return 1; }


test1.pwn


Code:
#include <YSI_Coding/y_hooks> hook OnScriptInit() { ? ? print("test2"); ? ? return 1; }


F:\samp\gamemodes\test1.pwn:4 (error) symbol already defined: "@yH_OnScriptInit@003"
Install by sampctl package install pawn-lang/[email protected]


RE: symbol already defined: "@yH_OnScriptInit@003" - JustMichael - 2020-06-24

Everytime you hook a callback using the `hook` macro, the final name of the callback becomes `hook <callback>@<unique_id>`. Now everytime you include `y_hooks` it generates a new unique number to append to the final callback name. Now as you might be aware, you can not have callbacks/functions of the same name, this doesn't compile.



Now this is what is happening with your issue, you have included `y_hooks` and have hooked the same callback twice, without including `y_hooks` again after the first hook. So the unique generated ID, is the same for each hook, which is why it is not working here. So to fix this issue, include `y_hooks` before you hook the next callback.



So this code below is bad.

Code:
#include <YSI_Coding\y_hooks> hook OnPlayerConnect(playerid) // Final Callback Name -> OnPlayerConnect@001(playerid) { ? ?return 1; } // This causes an error due to having the same name hook OnPlayerConnect(playerid) // Final Callback Name -> OnPlayerConnect@001(playerid) { ? ?return 1; }



This is the fixed version that allows you to hook the same callback again

Code:
#include <YSI_Coding\y_hooks> hook OnPlayerConnect(playerid) // Final Callback Name -> OnPlayerConnect@001(playerid) { ? ?return 1; } #include <YSI_Coding\y_hooks> hook OnPlayerConnect(playerid) // Final Callback Name -> OnPlayerConnect@002(playerid) { ? ?return 1; }