Extremely Baffling NPC:kill() Problem

Description: Archive discussion
============================
Original description:
Official subforum for LunaLua - an extension framework for SMBX Engine Version 1.3 (which a core of SMBX 2.0). Share your creations that require the use of LunaLUA. You may also share info/tutorials on how to use LunaLUA.

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 25
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #1by Sambo » 30 Oct 2015, 6:35

I'm having a problem with the NPC:kill function. It is successfully able to kill the targeted NPC, but then it throws an "Invalid NPC-pointer" error as if it thinks it didn't work. Here is the code that causes this:

Code: Select all

if springCooldown > 0 then -- Prevents 1000s of springs from spawning every time a springBox is hit.
      springCooldown = springCooldown - 1
   elseif springCooldown == 0 then
      if springs then
         for k,v in pairs(springs) do
               v:kill()
         end
      end
      for k,v in pairs(springBox) do
         if (colliders.collide(player, v)) then
            NPC.spawn(26, v.x, v.y, player.section)
            springs = NPC.getIntersecting(v.x, v.y, v.x + 32, v.y + 32)
            springCooldown = 15
         end
      end
   end

This code is supposed to spawn a spring NPC whenever a springBox collider is touched by the player and then kill that NPC after 15 frames. The springCooldown variable is defined in an earlier place, and the code is running in onLoop(). I have the latest available LunaLua installed.
Image
Current Project:
Image

Kevsoft M
Angry developer
Angry developer
Avatar
Kevsoft M
Angry developer
Angry developer
Age: 24
Reputation: 93
Posts: 379
Joined: 26 Apr 2014
Location: Austria
Website

Post #2by Kevsoft » 30 Oct 2015, 9:19

You cannot save up a NPC refernece for multiple frames. Either you need to get the NPC refernce right before killing it or you use pNPC API.
▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬ Signature.start() ▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬
▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬ Signature.end() ▬▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 25
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #3by Sambo » 30 Oct 2015, 21:55

Hmm. That's really weird that it would work at all, then. It killed the NPC at the same time as it threw the error. I'll just look into the pNPC API later today and see if I can get that to work.

EDIT: No luck:
wrap error.PNG

Here is the edited code:

Code: Select all

if (colliders.collide(player, spawn1)) then
      triggerEvent("delayed spawn")
   end
   if springCooldown > 0 then -- Prevents 1000s of springs from spawning every time a springBox is hit.
      springCooldown = springCooldown - 1
   elseif springCooldown == 0 then
      if springs then
         for k,v in pairs(springs) do
            v:kill()
         end
      end
      local springs = {}
      local s = 0
      -- Spawn a spring when the player touches a springBox collider, then create a wrapped table of
      -- all springs created this way.
      for k,v in pairs(springBox) do
         if (colliders.collide(player, v)) then
            NPC.spawn(26, v.x, v.y, player.section)
            springs[s] = pNPC.wrap(NPC.getIntersecting(v.x, v.y, v.x + 32, v.y + 32))
            s = s + 1
            springCooldown = 15
         end
      end
   end
Image
Current Project:
Image

Hoeloe F
Nice citizen
Nice citizen
Hoeloe F
Nice citizen
Nice citizen
Reputation: 5
Posts: 16
Joined: 3 Nov 2014

Post #4by Hoeloe » 10 Nov 2015, 3:42

Sambo wrote:

Code: Select all

            NPC.spawn(26, v.x, v.y, player.section)
            springs[s] = pNPC.wrap(NPC.getIntersecting(v.x, v.y, v.x + 32, v.y + 32))

This won't work. First of all, getIntersecting returns a table on NPCs, not just one NPC, so you can't just wrap it. Secondly, while you can't use a reference to an NPC over different frames, you can do it within the same run on onLoop, so really you want to do this:

Code: Select all

springs[s] = pNPC.wrap(NPC.spawn(26, v.x, v.y, player.section))


Which will spawn your NPC and wrap it, rather than spawning it and then trying to grab it from the coordinates, which was never going to work (what would happen if there were two NPCs in that space, for example?)

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 25
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #5by Sambo » 18 Nov 2015, 5:05

Now it gives me the same problem as when I started. It kills the NPC, then throws an error saying it was an invalid access to the pNPC pointer.

Here's the code:

Code: Select all

for l,w in pairs(springs) do
   w:kill()
end

It calls every NPC in the table, then kills them one by one. I did a small test and confirmed that it errors the very first time it loops, despite the fact it is killing the spring.
Image
Current Project:
Image

Hoeloe F
Nice citizen
Nice citizen
Hoeloe F
Nice citizen
Nice citizen
Reputation: 5
Posts: 16
Joined: 3 Nov 2014

Post #6by Hoeloe » 18 Nov 2015, 14:37

I suggest trying this:

Code: Select all

for l,w in pairs(springs) do
  if(w.isValid) then
    w:kill()
  end
end


It would be nice if pNPC did that check for you when accessing things, but it doesn't. Basically, this just makes sure the NPC isn't already dead.


Return to “LunaLua - Extension Framework”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest