02.12.2009, 17:31
These methods are essentially doing the exact same thing that Peter's function does, only his version finds the native's address automatically so that you don't have to (however, the native must be present in an AMX interface for this to work). Here is a snippet based on his code:
Of course, it gets slightly more complex if you want to pass strings or values by reference (you need to allocate and free memory).
I suppose this is a question of whether you want to compromise a little speed for convenience, because remember that the addresses will change if the server is recompiled.
Код:
typedef int (* amx_Function_t)(AMX * amx, cell * params); int IsPlayerConnected(int playerID) { int amx_idx; amx_FindNative(pAMX, "IsPlayerConnected", &amx_idx); if (amx_idx != 2147483647) { AMX_HEADER * amx_hdr = (AMX_HEADER *)(pAMX)->base; unsigned int amx_addr = (unsigned int)((AMX_FUNCSTUB *)((char *)amx_hdr + amx_hdr->natives + amx_hdr->defsize * amx_idx))->address; if (!amx_addr) { return 0; } amx_Function_t amx_Function = (amx_Function_t)amx_addr; cell params[2]; params[0] = 4; params[1] = playerID; return amx_Function(pAMX, params); } return 0; }
I suppose this is a question of whether you want to compromise a little speed for convenience, because remember that the addresses will change if the server is recompiled.