AwaitLoad

The SD.AwaitLoad function is designed to handle asynchronous waiting scenarios in Lua, where it's necessary to wait for a specific condition to be met before proceeding. This function includes timeout management to avoid infinite loops and ensure responsive code execution.

Overview

Purpose: Waits for a specified condition to be met, returning a value if successful within a given timeout, or throws an error if the timeout expires.

Parameters

  • callback (fun(): T|nil): A callback function that checks the desired condition. This function should return a non-nil value once the condition is met. If the condition is not met, it should return nil.

  • errorMessage (string, optional): The message to display if the timeout is exceeded. Defaults to "Operation timed out".

  • timeoutDuration (number|nil, optional): The maximum duration to wait for the condition to be met, specified in milliseconds. If nil, the default of 1000 milliseconds is used.

Returns

  • (T|nil): Returns the value obtained from the callback function if the condition is met within the specified timeout period. If the timeout is exceeded, it throws an error.

Usage Example

SD.LoadAnim = function(animDict)
    if type(animDict) ~= "string" then error("Animation dictionary identifier must be a string.") end

    if not HasAnimDictLoaded(animDict) then
        RequestAnimDict(animDict)

        -- Use sd.awaitLoad to wait for the animation dictionary to load with a timeout.
        SD.AwaitLoad(function()
            if HasAnimDictLoaded(animDict) then
                return true -- Return true indicating the dictionary is loaded.
            end
        end, ("Failed to load animation dictionary '%s'").format(animDict), 5000) -- 5000 ms timeout.
    end
end

return SD.LoadAnim

Last updated