Author Topic: How to wait for n seconds in a script?  (Read 809 times)

0 Members and 2 Guests are viewing this topic.

Offline Shivan Hunter

  • 210
  • FRED needs lambdas!
How to wait for n seconds in a script?
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?

 

Offline wookieejedi

  • 29
  • Intensify Forward Firepower
Re: How to wait for n seconds in a script?
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


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"}
)