28.07.2012, 17:42
ConnectRNPC does not always return the correct ID when used in OnGameModeInit or main.
Fix: Create NPCs slightly delayed with a timer when the gamemode is fully loaded.
This happens because there's a delay to the NPC connects to the server. Try this:
As you see, OnPlayerConnect is called 250 ~ 300ms after we call ConnectRNPC.
ConnectRNPC basically checks for a empty slot and returns it. But a slot is only occupied when OnPlayerConnect is called.
I'm sayin that if we do this:
A and B will have the same ID value, because the function will return the same empty slot and will probably screw with your code when you call MoveRNPC, for example.
Ok, so how to fix that ?!
I've modified the rnpc.inc. Look:
It works, returns the correct ID, you need to call RNPC_OnPlayerConnect and RNPC_OnPlayerDisconnect on the respective native SA:MP callbacks [I don't know how to use ASL], but still have a problem - if you connect a lot of NPCs, like 7 or more at the same time, g_bConnected will be set to true but SA:MP will block the connection of the NPCs because of a false 'attack'; OnPlayerDisconnect will be not called and g_bConnected will be defined as true, when it shouldn't.
So, we know what's the reason of the bug, but we haven't a solution yet.
Sorry for my bad english g.g
Fix: Create NPCs slightly delayed with a timer when the gamemode is fully loaded.
This happens because there's a delay to the NPC connects to the server. Try this:
Code:
new g_iTickCount = -1; public OnGameModeInit( ) { g_iTickCount = GetTickCount( ); ConnectRNPC( "Test" ); return 1; } public OnPlayerConnect( playerid ) { printf( "Delay: %ims.", GetTickCount( ) - g_iTickCount ); return 1; }
ConnectRNPC basically checks for a empty slot and returns it. But a slot is only occupied when OnPlayerConnect is called.
I'm sayin that if we do this:
Code:
A = ConnectRNPC( "Test1" ); B = ConnectRNPC( "Test2" );
Ok, so how to fix that ?!
I've modified the rnpc.inc. Look:
Code:
static bool: g_bConnected[MAX_PLAYERS char]; stock ConnectRNPC(name[]) { new slotid = -1; for (new i = 0; i < MAX_PLAYERS; i++) { if (!g_bConnected{i}) { ConnectNPC(name, "RNPC"); g_bConnected{i} = true; slotid = i; break; } } return slotid; } stock RNPC_OnPlayerConnect(playerid) g_bConnected{playerid} = true; stock RNPC_OnPlayerDisconnect(playerid) g_bConnected{playerid} = false;
So, we know what's the reason of the bug, but we haven't a solution yet.
Sorry for my bad english g.g