Author Topic: [Solved ]Trying to make an NPC disappear  (Read 1345 times)

ponderitus

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
[Solved ]Trying to make an NPC disappear
« on: April 30, 2017, 02:02:35 am »
Hi guys, I've been trying to make a sensor which on activated a dialog will load from an NPC who will then walk off and vanish, I've tried to make it a save game variable so that the sensor and the NPC are gone forever after this. I can't seem to get my NPC to disappear when the map reloads, I realize the below will make him disappear when the sensor is activated again (which is what it is doing). Can anyone show me how to make it do what I need it to do. I tried lots of things but I can't seem to get it to work

Code: ( lua) [Select]
local meeting_quest_1 = false

function meeting_sensor:on_activated()
   if game:get_value("meeting_quest_1") then
   worker_npc:remove()
   else
      game:start_dialog("meeting.1")
      hero:freeze()
      local path_movement = sol.movement.create("path")
      path_movement:set_path({ 2, 2, 2, 2, 2, 1, 1, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0 })
      path_movement:set_ignore_obstacles(true)
      worker_npc:is_traversable()
      path_movement:set_speed(64)
      path_movement:start(worker_npc, function()
      worker_npc:remove()
      game:set_value("meeting_quest_1", true)
      hero:unfreeze() 
      end)
   end
end

I kept getting these errors when i tried to get it working outside the sensor:on_activated
Error: attempt to index global 'worker_npc' (a nil value)
Error: attempt to index global 'meeting_sensor' (a nil value)
but as those are names of entities on my map...isnt that enough to use them just as : worker_npc:remove() ?

as in
Code: ( lua) [Select]
map:on_started(function()
if game:get_value("meeting_quest_1") then
worker_npc:remove()
« Last Edit: May 02, 2017, 10:53:26 pm by ponderitus »

ponderitus

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Trying to make an NPC disappear
« Reply #1 on: April 30, 2017, 02:21:33 am »
i also forgot that i needed to change the dialog to a function with
Code: ( lua) [Select]
game:start_dialog("meeting.1", function()
   if answer == 2 then -- No
   game:start_dialog("meeting.1")

llamazing

  • Full Member
  • ***
  • Posts: 210
    • View Profile
Re: Trying to make an NPC disappear
« Reply #2 on: April 30, 2017, 04:00:16 am »
To get the NPC to disappear when the map reloads, you should use the map:on_started() event.

I think you want something like this:
Code: (lua) [Select]
function map:on_started(destination)
   if game:get_value("meeting_quest_1") then
      worker_npc:remove()
   end
end

function meeting_sensor:on_activated()
   if not game:get_value("meeting_quest_1") then
      game:start_dialog("meeting.1")
      hero:freeze()
      local path_movement = sol.movement.create("path")
      path_movement:set_path({ 2, 2, 2, 2, 2, 1, 1, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0 })
      path_movement:set_ignore_obstacles(true)
      worker_npc:is_traversable()
      path_movement:set_speed(64)
      path_movement:start(worker_npc, function()
         worker_npc:remove()
         game:set_value("meeting_quest_1", true)
         hero:unfreeze() 
      end)
   end
end

you can probably delete line 1 as well because that doesn't look like it is doing anything.

ponderitus

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Trying to make an NPC disappear
« Reply #3 on: April 30, 2017, 05:11:23 am »
That's perfect, Once again amazing! I managed to get the extra dialog in myself. I did try using map:on_started() but i didn't put destination inside the brackets though, that wasn't my only mistake :P You're so good at these things.