NPC at Spot + Create NPC

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

Moderators: marionline, SDHawk

Post Reply
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

NPC at Spot + Create NPC

Post by Willy Elektrix »

I am trying to write a script that checks to see if there if a copy of NPC 0 is adjacent to the hero. If there is, then the script creates a copy of NPC 1 where the copy of NPC 0 exists.

The purpose of this is to make walls (NPC 1) that appear around the hero as he walks.

Here is the flow of it in layman's terms.

1) Hero steps on NPC 2.
2) NPC 2 runs the script.
3) The script checks to see NPC 0 is north, east, south, or west of the hero.
4) If so, the script creates NPC 1 on top of NPC 0 wherever NPC 0 occurs.

Code: Select all

plotscript, r15mazewalls, begin
if (NPC at spot (hero x (0) + 1, hero y (0)) == 0) then (
	create npc (1, hero x (0) + 1, hero y (0))
) if (NPC at spot (hero x (0) - 1, hero y (0)) == 0) then (
	create npc (1, hero x (0) - 1, hero y (0))
) if (NPC at spot (hero x (0), hero y (0) + 1) == 0) then (
	create npc (1, hero x (0), hero y (0) + 1)
) if (NPC at spot (hero x (0), hero y (0) - 1) == 0) then (
	create npc (1, hero x (0), hero y (0) - 1)
) end
That is my script. It creates copies of NPC 1, but not where they should be. In fact, I'm not able to tell what the logic behind where it creates NPCs, but it is nearly everywhere but on top of NPC 0 where it should.
User avatar
SDHawk
Metal Slime
Posts: 673
Joined: Sun Apr 27, 2008 8:46 pm

Post by SDHawk »

NPC at spot returns an NPC reference rather than a type. You have to use Get NPC ID to convert it. ie:

Code: Select all

if (Get NPC ID(NPC at spot (hero x (0) + 1, hero y (0)) == 0) then
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

Post by Willy Elektrix »

I got it to work with the code below. NPC 3 occupies the empty tile where the wall should appear. NPC 1 is the wall itself.

However, now I want to destroy the copies of NPC 1 when the player is no longer adjacent to them, but I do not know how to begin with that.

The idea is that the NPC walls will appear when the player is adjacent to them and then disappear when they walk away from them.

Code: Select all

plotscript, r15mazewalls, begin
if (get npc id (NPC at spot ((hero x (0) +1), hero y (0))) == 3) then (
   create npc (1, (hero x (0) +1), hero y (0))
) if (get npc id (NPC at spot ((hero x (0) --1), hero y (0))) == 3) then (
   create npc (1, (hero x (0) --1), hero y (0))
) if (get npc id (NPC at spot (hero x (0), (hero y (0) +1))) == 3) then (
   create npc (1, hero x (0), (hero y (0) +1))
) if (get npc id (NPC at spot (hero x (0), (hero y (0) --1))) == 3) then (
   create npc (1, hero x (0), (hero y (0) --1))
) end
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Sounds like you should just destroy all existing copies of NPC 1 before placing the new ones. That's easy:

Code: Select all

variable(idx)
for (idx, 0, npc copy count(1) -- 1) do (
  delete npc(npc reference(1, idx))
)
Last edited by TMC on Sat Feb 21, 2015 4:23 am, edited 1 time in total.
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

Post by Willy Elektrix »

TMC:

I got your code to work but I had to make the loop count backwards. I assume this is because each time it deletes an NPC, it the reference numbers of the others changes.

Code: Select all

plotscript, r15mazewalls, begin
variable (idx)
variable (npccount)
npccount := NPC copy count(1)
for (idx, npccount, 0, -1) do (
	delete npc(npc reference(1, idx))
)
if (get npc id (NPC at spot ((hero x (0) +1), hero y (0))) == 3) then (
	create npc (1, (hero x (0) +1), hero y (0))
) if (get npc id (NPC at spot ((hero x (0) --1), hero y (0))) == 3) then (
	create npc (1, (hero x (0) --1), hero y (0))
) if (get npc id (NPC at spot (hero x (0), (hero y (0) +1))) == 3) then (
	create npc (1, hero x (0), (hero y (0) +1))
) if (get npc id (NPC at spot (hero x (0), (hero y (0) --1))) == 3) then (
	create npc (1, hero x (0), (hero y (0) --1))
) end 
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Doh! I totally forgot about that!

The start index for your loop is actually too high, it should be
for (idx, npccount -- 1, 0, -1) do (
You need to fix this, as "npc reference(1, idx)" with invalid idx returns 0, so you're doing "delete npc(0)": delete the first copy of NPC 0.
User avatar
Willy Elektrix
Liquid Metal Slime
Posts: 910
Joined: Sun Aug 15, 2010 11:30 pm

Post by Willy Elektrix »

TMC wrote:Doh! I totally forgot about that!

The start index for your loop is actually too high, it should be
for (idx, npccount -- 1, 0, -1) do (
You need to fix this, as "npc reference(1, idx)" with invalid idx returns 0, so you're doing "delete npc(0)": delete the first copy of NPC 0.
Ha! That explains another bug I was having trouble with.
Last edited by Willy Elektrix on Tue Feb 24, 2015 12:30 am, edited 1 time in total.
Post Reply