Wait!

[Open Assets] Wait! 1

What permissions do you give others to modify and/or maintain your submission?
Modify: YES - Maintain: YES - I give permission for my entire submission to be modified by others or used in their own work. I give permission for my entire submission to be maintained by others as well.
A custom wait function In srb2! If you're too lazy to manually make a timer and decrease it every second, you can use this custom wait function

Wait:
  local function Wait(seconds)
    local totalTics = seconds * TICRATE
    local currentTic = 0
 
    return function()
        currentTic = currentTic + 1
        if currentTic >= totalTics then
            currentTic = 0  -- Reset currentTic to prevent acceleration
            return true
        end
        return false
    end
end
end
Basic Principles:
  1. The Wait function MUST be assigned to a variable

    Example:
    local myTimer = Wait(3)  -- CORRECT
    Wait(3)  -- INCORRECT, this will not work
  2. The timer function returns false until the time is complete

    Example:
    local timer = nil
    
    addHook("PlayerThink", function(player)
        if not timer then
            timer = Wait(3)
        end
    
        if timer() then
            -- This code runs ONLY after 3 seconds
            P_SpawnMobj(...)
        end
    end)


  3. You can continue writing code after calling the timer

    Example:
    local timer = nil
    
    addHook("PlayerThink", function(player)
        if not timer then
            timer = Wait(3)
        end
    
        -- Other code can run immediately
        print("Timer started")
    
        if timer() then
            -- Timed action
            P_SpawnMobj(...)
        end
    
        -- More code can run
        doSomethingElse()
    end)
  4. Multiple timers can exist simultaneously

    Example:
    local timer1 = nil
    local timer2 = nil
    
    addHook("PlayerThink", function(player)
        if not timer1 then
            timer1 = Wait(2)
        end
    
        if not timer2 then
            timer2 = Wait(5)
        end
    
        if timer1() then
            -- Runs after 2 seconds
            P_SpawnMobj(...)
        end
    
        if timer2() then
            -- Runs after 5 seconds
            P_SetObjectMomZ(player.mo, 10*FRACUNIT)
        end
    end)
  5. Resetting timers

    Example:
    local timer = nil
    
    addHook("PlayerThink", function(player)
        if not timer then
            timer = Wait(3)
        end
    
        if timer() then
            -- Reset timer
            timer = Wait(3)
     
            -- Perform action
            P_SpawnMobj(...)
        end
    end)

    Remember that
    -Timers are not paused, they continue counting(Unless you pause the game)
    -Each call to Wait() creates a new independent timer


    In a Hook:
    local firstTimer = nil
    local secondTimer = nil
    local state = 0
    
    addHook("PlayerThink", function(player)
        -- State Machine Pattern
        if state == 0 then
            -- Initialize first timer
            firstTimer = Wait(3)  -- Start 3-second timer
            state = 1
        end
    
        -- First timer check
        if state == 1 and firstTimer() then
            -- Action after first timer completes
            P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_THOK)
     
            -- Initialize second timer
            secondTimer = Wait(2)  -- Start 2-second timer
            state = 2
        end
    
        -- Second timer check
        if state == 2 and secondTimer() then
            -- Action after second timer completes
            P_SetObjectMomZ(player.mo, 10*FRACUNIT)
            state = 3  -- Prevent repeated execution
        end
    end)


    If this is just complex, you can just do Ol' reliable. This was just made for lazy people like you and me, if you are using multiple,

    Remember the function WILL NOT STOP until you implement something that stops it

    Yipes!:
    local timer = Wait(3)  -- 3-second timer
    addHook("PlayerThink", function(player)
        if timer() then
            print("3 seconds passed!")
            -- This will print EVERY TIC AFTER the initial 3-second wait
        end
    end)






    Exaple code but simple:
    local function Wait(seconds)
        local totalTics = seconds * TICRATE
        local currentTic = 0
    
        return function()
            currentTic = currentTic + 1
            if currentTic >= totalTics then
                currentTic = 0  -- Reset currentTic to prevent acceleration
                return true
            end
            return false
        end
    end
    
    local timer = nil
    local state = 0
    local ah = true
    
    addHook("PlayerSpawn", function(p)
        timer = nil
        state = 0
        ah = true
    end)
    
    addHook("PlayerThink", function(player)
        if ah == true then
            print("Hey! It's me! Average")
            ah = false
            state = 0
        end
    
        if state == 0 then
            timer = Wait(3)
            state = 1
        end
    
        if state == 1 and timer() then
            print("Have fun using the mod you lazy bums")
            timer = Wait(3)
            state = 2
        end
    
        if state == 2 and timer() then
            print("I am Muy Intelligente")
            timer = Wait(4)
            state = 3
        end
    
        if state == 3 and timer() then
            print("I am also nice too")
            timer = Wait(4)
            state = 4
        end
    
        if state == 4 and timer() then
            print("See how (not) easy this it")
            timer = nil
            state = 5
        end
    end)

    1737692112855.png
  • Cool!
Reactions: santiXDSRB2
Author
Average
Downloads
83
Views
1,059
Extension type
lua
File size
267 bytes
MD5 Hash
1dee8d4e87e40de7136b04d29532295a
First release
Last update

More resources from Average

Share this resource

Back
Top