Initializing a struct in maxscript
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
)
2 comments
Comment from: rotem Visitor
great trick, I was just wondering how I could do that the other day :)
10/15/10 @ 12:06 am
Comment from: o.z Member
Actually, since 3ds Max 2010, it is also possible to use the on create event handler:
on create do [expression]
10/15/10 @ 05:30 am