Перейти к основному содержимому

NPC_IsValid

warning

This function was added in omp v1.5.8.3079 and will not work in earlier versions!

Description

Checks if an NPC ID is valid and the NPC exists.

NameDescription
npcidThe NPC ID to check.

Returns

Returns true if the NPC is valid, false otherwise.

Examples

public OnPlayerCommandText(playerid, cmdtext[])
{
if (!strcmp(cmdtext, "/checkvalid", true))
{
new npcid = PlayerNPC[playerid];
if (npcid == INVALID_NPC_ID)
return SendClientMessage(playerid, 0xFF0000FF, "You are not debugging a NPC.");

new bool:isValid = NPC_IsValid(npcid);

SendClientMessage(playerid, 0x00FF00FF, "NPC %d is valid: %s", npcid, isValid ? "Yes" : "No");
return 1;
}
return 0;
}

Notes

warning
  • Always check if an NPC is valid before performing operations on it.
  • An NPC becomes invalid when it is destroyed.
  • This prevents runtime errors and crashes.
  • Use this in loops when iterating through potential NPC IDs.