Loading Dependencies

From GMod Wiki

Jump to: navigation, search
Lua: Loading External Dependencies ( AKA other scripts )
Page white text.png Description:Loading functions from other scripts, even when the script isn't immediately available.
link=User:Joudoki Original Author:Joudoki
Calendar.png Created:20th December, 2006

Preface

Ok, so you have a script, and it needs to load something from another script, but you can't always make sure that the other script loads before yours; What do you do?

The Script

Here's a simple little script I wrote that'll go around the loading order issue:

 
    function FunctionToRunIfAvailable()
        if ( otherscript ) then
            Interact with external script
        end
    end
 
    if ( ! ( otherscript ) ) then
        // Try again in 1 second
        timer.Simple( 1, FunctionToRunIfAvailable );
    else
        FunctionToRunIfAvailable( );
    end
 

How does it work?


1) It checks for a variable that is set in the other script.
2) If the variable isn't there, it sets up a timer, which will [b]try again[/b] in 1 second.
( Or if it is already available, load it now ).
3) When the timer expires, all lua should be loaded ( unless you have a LOT of lua ), and it will try again; If it's there, it will load it.
Cheers!

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox