NPC reference is invalid error

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

NPC reference is invalid error

Post by Foxley »

Getting this error in a script:

getnpcslice: invalid npc reference -3
(maybe the NPC was deleted?)

My question is, how do you get a true/false value for if an NPC reference is an existent NPC or not? "NPC reference" requires the NPC ID, which my scripts don't deal with, they use the reference. Is there another way to do this so I can check to make sure the NPC hasn't been killed/deleted before trying to run a script on it?
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

You can either use "npc is disabled" which returns true either if the npc doesn't exist or if it is hidden due to a tag condition, or you can use "get npc id", which returns -1 if the NPC doesn't exist. Strangely, "get npc id" even works if you give it an NPC ID, testing whether any NPCs with that ID exist.

Code: Select all

if &#40;get npc id&#40;npc&#41; <> -1&#41; then &#40;stuff...&#41;
However, beware! After an NPC is deleted another one might be created (with "create NPC") which has the same NPC reference! If this is a concern you could put a special value in one of the NPC extra data slots and test that. (However, if the NPC was created in the map editor its reference won't be reused unless you have close to 300 NPCs on the map).
Last edited by TMC on Mon Dec 12, 2016 5:00 am, edited 2 times in total.
Post Reply