Hard Light Productions Forums

Modding, Mission Design, and Coding => The Scripting Workshop => Topic started by: Shivan Hunter on August 23, 2025, 07:44:50 am

Title: How to wait for n seconds in a script?
Post by: Shivan Hunter on August 23, 2025, 07:44:50 am
I'm sure this is a total noob question but the documentation is abysmal.

I want a missile that temporarily shuts off a fighter's shields for n seconds. I need an On Weapon Collision hook that shuts off the shields, waits n seconds, then turns the shields back on again. How do I do the wait in a lua script?
Title: Re: How to wait for n seconds in a script?
Post by: wookieejedi on August 23, 2025, 08:49:56 am
Good question! Overall you'll want to use coroutines. The short pseudo code would be this:
         
Also, feel free to ask on our scripters Discord, as commonly that has more folks around to quickly answer things :)
https://discord.gg/EYtHHkE7tv (https://discord.gg/EYtHHkE7tv)


function SpecialFunction(ship)

   if ship == nil then return end
   if not ship:isValid() then return end

   async.run(function()
      --shut off shields (write code here)
      local n_seconds = 5
      async.await(mn.waitAsync(n_seconds))
      if ship ~= nil and ship:isValid() then
         --re enable shields  (write code here)
      end
   end)

end


engine.addHook("On Weapon Collision", function()
       SpecialFunction(hv.Ship)
   end,
   {State="GS_STATE_GAME_PLAY", ["Weapon class"]="WEAPONCLASSNAME_from_weapons.tbl"}
)