Category: "Maxscript"

Initializing a struct in maxscript

September 5th, 2008

Here's a quick tip on how to initialize a struct in maxscript, when an instance of the struct is created:

(
    -- This is just a sample of a struct that will be initialized
    -- when an instance of it will be created.
    -- For this demo, we'll populate the someArray variable with
    -- some random numbers.
    struct s_InitializableStruct (
        someArray = #(),     -- The default value is an empty array.
        
        -- The init function is the function that determines what will
        -- be initilized.
        fn init =
        (
            someArray = for i = 1 to 10 collect (random 0 1000)
        ),
        
        -- And this is the trick: use another variable, and assign to it
        -- the "result" of the init function.
        _init = init()
    )
    
    -- Create an instance of the struct
    local structInstance = s_InitializableStruct()
    -- Print the value of someArray
    format "%\n" structInstance.someArray
    -- As you can see, the someArray variable has been initialized with 10 random numbers
)