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

ponderitus

  • Newbie
  • *
  • Posts: 25
    • 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
  1. local meeting_quest_1 = false
  2.  
  3. function meeting_sensor:on_activated()
  4.    if game:get_value("meeting_quest_1") then
  5.    worker_npc:remove()
  6.    else
  7.       game:start_dialog("meeting.1")
  8.       hero:freeze()
  9.       local path_movement = sol.movement.create("path")
  10.       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 })
  11.       path_movement:set_ignore_obstacles(true)
  12.       worker_npc:is_traversable()
  13.       path_movement:set_speed(64)
  14.       path_movement:start(worker_npc, function()
  15.       worker_npc:remove()
  16.       game:set_value("meeting_quest_1", true)
  17.       hero:unfreeze()  
  18.       end)
  19.    end
  20. end
  21.  

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
  1. map:on_started(function()
  2. if game:get_value("meeting_quest_1") then
  3. worker_npc:remove()
  4.  
« Last Edit: May 02, 2017, 10:53:26 pm by ponderitus »

ponderitus

  • Newbie
  • *
  • Posts: 25
    • 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
  1. game:start_dialog("meeting.1", function()
  2.    if answer == 2 then -- No
  3.    game:start_dialog("meeting.1")

llamazing

  • Jr. Member
  • **
  • Posts: 73
    • 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
  1. function map:on_started(destination)
  2.    if game:get_value("meeting_quest_1") then
  3.       worker_npc:remove()
  4.    end
  5. end
  6.  
  7. function meeting_sensor:on_activated()
  8.    if not game:get_value("meeting_quest_1") then
  9.       game:start_dialog("meeting.1")
  10.       hero:freeze()
  11.       local path_movement = sol.movement.create("path")
  12.       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 })
  13.       path_movement:set_ignore_obstacles(true)
  14.       worker_npc:is_traversable()
  15.       path_movement:set_speed(64)
  16.       path_movement:start(worker_npc, function()
  17.          worker_npc:remove()
  18.          game:set_value("meeting_quest_1", true)
  19.          hero:unfreeze()  
  20.       end)
  21.    end
  22. end

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

ponderitus

  • Newbie
  • *
  • Posts: 25
    • 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.