Page 1 of 1

Auto-unload pickups mod

Posted: 06 Jan 2017, 19:34
by mkari37
Hi! I'd like to build myself a mod that automatically unloads and strips all weapons I pickup. That is, when I take a gun from a body (optionally also a stash), the gun, ammo, silencers, scopes and any other mods are separated, and placed in my inventory individually.

Where would I start?

Re: Auto-unload pickups mod

Posted: 06 Jan 2017, 22:56
by Alundaio
  • Create a new .script
  • Add an on_game_start function, it is automatically triggered if a script has one.

      Code: Select all

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

  • Then add a actor_on_item_take function.

      Code: Select all

      function actor_on_item_take(obj)
         -- If object is a Weapon but not a Knife
         local cls = obj:clsid()
         if (IsWeapon(nil,cls) and not cls == clsid.wpn_knife_s) then
            obj:unload_magazine()
         end
      end


Though I'm not sure if that will unload the weapon in actor's inventory or the inventory you are taking it from. If it's not unloading into actor's inventory then we can use a time event to do it after.

Code: Select all

function actor_on_item_take(obj)
   -- If object is a Weapon but not a Knife
   local cls = obj:clsid()
   if (IsWeapon(nil,cls) and not cls == clsid.wpn_knife_s) then
      CreateTimeEvent(0,"unload",0,try_unload_magazine,obj:id())
   end
end

function try_unload_magazine(id)
   local wpn = id and level.object_by_id(id)
   if (wpn) then
      wpn:unload_magazine()
   end
   return true
end



To detach addons automatically you can do this:

Code: Select all

         local addons = {
            "wpn_addon_scope",
            "wpn_addon_scope_x2.7",
            "wpn_addon_scope_detector",
            "wpn_addon_scope_night",
            "wpn_addon_scope_susat",
            "wpn_addon_scope_susat_x1.6",
            "wpn_addon_scope_susat_custom",
            "wpn_addon_scope_susat_dusk",
            "wpn_addon_scope_susat_night",
            "wpn_addon_silencer",
            "wpn_addon_grenade_launcher",
            "wpn_addon_grenade_launcher_m203"
            }
         for k,v in pairs(addons) do
            wpn:weapon_addon_detach(k)
         end

Re: Auto-unload pickups mod

Posted: 07 Jan 2017, 19:38
by mkari37
Wow, thanks Alundaio!

I really didn't mean for you to do it for me, but... thanks!

Re: Auto-unload pickups mod

Posted: 07 Jan 2017, 22:49
by mkari37
It's partially working, but if I could pick your brains a little more...

  • I couldn't get it to work until I replaced

    Code: Select all

    not cls == clsid.wpn_knife_s
    with

    Code: Select all

    cls ~= clsid.wpn_knife_s
    I'm not sure about LUA's operator precedence or associativity, but it's working now.
  • It's being called on every game load, so my primary weapons are disassembled, as well as my loot. Is there a way to avoid that, perhaps by checking where the object is being taken from, or only registering a few seconds after a game load?Fixed! (Where's strikethrough when you need it?)
  • In order to avoid hard-coding in addon lists, and to enable compatability with AO and OWR, is there a way to iterate through all object classes, and check for 'wpn_addon_.*'?

Thanks again for your help.

Re: Auto-unload pickups mod

Posted: 07 Jan 2017, 23:48
by Alundaio
It won't work with AO3 or STCOP anyway, because those are done in an extremely hacky way.

There is a script in place that changes the actual weapon with a new weapon with updated visual for these addons. Currently I have been investigating ways of changing the engine addon system to address this silly hack. But might be some time.

Re: Auto-unload pickups mod

Posted: 08 Jan 2017, 00:16
by mkari37
It won't work with AO3 or STCOP anyway, because those are done in an extremely hacky way.


Ah, thank you. I was tearing my hair out wondering why I couldn't detach any of the new scopes.