how to remove/add items to npc properly?

luppolo
Scavenger
Posts: 37
Joined: 04 Nov 2016, 21:58

how to remove/add items to npc properly?

Postby luppolo » 31 May 2017, 00:30

i'm trying to make all the npc equipment script sided in order to have dynamic npc loadouts depending on various factors

this is my current script so far, it partially works at stripping all npc of their weapons/ammo set in character_desc_general and giving them only one of the weapons in the rookie_w array (except actor starting weapons and npc i reincarnate into with azazel mode) however sometimes i get random crashes with unhandled exceptions and empty stack trace in the crash log, i wonder if im removing/giving items to npc correctly

Code: Select all

function on_game_start()
   RegisterScriptCallback("npc_on_update",npc_on_update)
end

local function npc_on_update(npc,st)
   if not (npc:alive()) then
      return
   end
   
   if(utils.se_obj_load_var(npc:id(),npc:name(),"strelocc_de_first_update_done")) then
      return
   end
   
   remove_weapons_and_ammo(npc)
   
   give_weapon_and_ammo(npc)
   give_grenades(2, npc)
   
   utils.se_obj_save_var(npc:id(),npc:name(),"strelocc_de_first_update_done",true)
end

local rookie_w = {
--smg
"wpn_bizon",
--shotguns
"wpn_bm16","wpn_toz194","wpn_toz34","wpn_mp133",
--pistols
"wpn_aps","wpn_fort","wpn_oc33","wpn_pb","wpn_pm","wpn_pmm"
}

--todo, find out which ammo the spawned weapon needs and give only that
function give_weapon_and_ammo(npc)
   alife():create(rookie_w[math.random(#rookie_w)],npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id())
   alife():create("ammo_9x18_fmj",npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id())
   alife():create("ammo_12x70_buck",npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id())
end

local ammo_prefix = "ammo_"
local weapon_prefix = "wpn_"
function remove_weapons_and_ammo(npc)
   local function itr(npc,itm)
      if(string.find(itm:name(), ammo_prefix)~=nil or string.find(itm:name(),weapon_prefix)~=nil) then
         local sim = alife()
         local se_obj = sim:object(itm:id())
         alife():release(alife():object(itm:id()), true)
      end
   end
   
   npc:iterate_inventory(itr,npc)
end

function give_grenades(n, npc)
   local i = 0
   while i<n do
      alife():create("grenade_rgd5",npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id())
      i = i+1
   end
end


User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1372
Joined: 26 May 2012, 22:26

Re: how to remove/add items to npc properly?

Postby Alundaio » 31 May 2017, 18:55

I don't see anything immediately wrong, but you may want to switch to using squad_on_npc_creation callback, but the requires you to have empty spawn supplies sections in the character description xmls.

1.5 is going to have more control over npc loadout from the lua scripts and it will be slightly randomized.
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

luppolo
Scavenger
Posts: 37
Joined: 04 Nov 2016, 21:58

Re: how to remove/add items to npc properly?

Postby luppolo » 31 May 2017, 20:35

Alundaio wrote:you may want to switch to using squad_on_npc_creation callback

right now i skip after the first update but in the future i'd want to be able to change the equipment not only of "new" npc but also of already spawned ones

for example let's say i make all loners spawn with only pistols and shotgun, but then add a dialog to Nimble where i ask him to issue better weapons to rookies for a price, after i pay him i set a "give better weapons to rookies" tag and at the next on_actor_update if that flag is true i update weapons of npc that are of the loner faction and rank lower than a certain value


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest