Creating definite NPC layering

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

Moderators: marionline, SDHawk

Post Reply
User avatar
TheMan
Red Slime
Posts: 31
Joined: Wed Jul 05, 2017 8:42 pm

Creating definite NPC layering

Post by TheMan »

I have a script that destroys a given NPC when the hero walks over it. I set the command "NPC at spot" to read the bottommost NPC of that tile, which is the one I want to destroy. However, sometimes there are multiple NPCs occupying that spot, because the NPCs move through each other with "Set NPC obstructs". In these cases, the layering of the NPCs seems random, and it will delete either, sometimes the one I don't want to destroy. So, is there a way to make sure an NPC is always above/below other NPCs?
User avatar
Spoonweaver
Liquid Metal King Slime
Posts: 6462
Joined: Mon Dec 08, 2008 7:07 am
Contact:

Post by Spoonweaver »

I would suggest that instead you detect the npc's ID or copy number.
If you only want to destroy 1 type of npc rather than another it would be a simply task.

here is example of what i generally do for collision. It checks the stacked npcs at a given pixel ( which is in the middle of the hero ) it checks up to a layer of 5 but can go higher if need be.

Code: Select all


variable(THING), variable(k)

for (k,0,5,1) do, begin
  THING:=NPC at pixel ( HERO pixel X(0)+10 , HERO pixel Y(0)+10 , k )
  if ( get NPC ID (THING)==2 ) THEN (destroy npc (THING) )
end

Last edited by Spoonweaver on Sat Dec 01, 2018 4:31 am, edited 2 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, it seems you don't actually want to control how NPCs are layered, you want to select the right NPC when there are multiple ones.

The way Spoonweaver uses "npc at pixel" is better than "npc at spot" if some of the NPCs are in motion so that they are part-way between different tiles. "npc at spot" considers an NPC to be on the tile that its top-left corner is over, even if that's by just one pixel.

However, there's a bug in Spoonweaver's script: "npc at pixel" will return 0 if k is out of range, so if you were looking for NPC ID 0, you'd see it everywhere.
Instead:

Code: Select all

for (k,0,5,1) do, begin
  THING:=NPC at pixel ( HERO pixel X(0)+10 , HERO pixel Y(0)+10 , k )
  if ( THING && get NPC ID (THING)==2 ) THEN (destroy npc (THING), k -= 1 )
end 
User avatar
TheMan
Red Slime
Posts: 31
Joined: Wed Jul 05, 2017 8:42 pm

Post by TheMan »

TMC wrote:Yes, it seems you don't actually want to control how NPCs are layered, you want to select the right NPC when there are multiple ones.

The way Spoonweaver uses "npc at pixel" is better than "npc at spot" if some of the NPCs are in motion so that they are part-way between different tiles. "npc at spot" considers an NPC to be on the tile that its top-left corner is over, even if that's by just one pixel.

However, there's a bug in Spoonweaver's script: "npc at pixel" will return 0 if k is out of range, so if you were looking for NPC ID 0, you'd see it everywhere.
Instead:

Code: Select all

for (k,0,5,1) do, begin
  THING:=NPC at pixel ( HERO pixel X(0)+10 , HERO pixel Y(0)+10 , k )
  if ( THING && get NPC ID (THING)==2 ) THEN (destroy npc (THING), k -= 1 )
end 
Thanks for the help, this seems to work perfectly.
Post Reply