[Closed] Using array/object like object.variable=value
I was wondering, is it possible to create an array or object in maxscript and assign variables to it? So like:
global someObject;
someObject.firstVar = "first variable"
someObject.secondVar = "second variable"
I’ve been searching and trying for quite some time now, and I can’t seem to get it done.
If it isn’t possible, has anyone got any suggestions on how to efficiently store a number of variables so that they can be accessed quickly? (I want to be able to retrieve the variable name itself too).
I was thinking of using a multidimensional array, but that’s not very efficient, because you still need the index numbers to access the values, rather than variable names.
Thanks.
If you are referring to nodes, you can save data to them using either Custom Attributes or setAppData/getAppData. AppData is much simpler than Custom Attributes for a beginning scripter.
Variables are simply pointers to specific memory addresses that hold values, which can be anything from a controller, to a text string, to a GeoSphere, to an integer, etc. You can have several variables with different names and scopes all pointing to the same value, and any of these can be essentially a temporary alias. If you want to access them using more intuitive names but still store them in arrays, then simply create temporary locals that point to them when needed. See the example below, which shows how to reference a variable held in an array from within a button event handler:
global MyArray = #(33, "MyTextString", 41.27) --Array declared and defined
MyArray[4] = (Teapot segments:8) --Teapot created and appended to existing array
on MyButton pressed do --An event handler for MyButton
(
local Var1 = MyArray[4] --A local variable referring to the Teapot
local NewRad = MyArray[3] --A local variable referring to the float value of 41.27
Var1.radius = NewRad --Teapot radius changed to 41.27
MyArray[2] = Var1.name --"MyTextString" value in array overwritten with "Teapot01"
MyArray[1] = Var1.segments --33 value in array overwritten with the integer 8
)
Arrays are unavoidable in maxscript and they are actually very efficient, but it does take a while to wrap your head around them when you’re first learning. It takes some time to start thinking in arrays, loops, etc…but you’ll get used to it.
Sure that is easy:
struct foo
(
first,
second
)
global something = foo()
-- or
global anotherthing = foo first: "first" second: "second"
-- or
global yetAnotherThing = foo()
yetAnotherThing.first = "first"
yetAnotherThing.second = "second"
Chris Johnson
Ah great, thanks a lot!
That’s sometimes the stupid thing of starting with a new scripting/programming language, you can search for hours for something that is working or named just a bit different than what you’re used to.
The maxscript docs are a goldmine of info on this sort of stuff.
However what you might need is a maxscript buddy who you can communicate with in real time via instant messenger (Or some other similar program).
Thanks for your replies.
I’m not so much a beginner when it comes to scripting in general, I’ve been working for quite some time with scripting languages like php, javascript, actionscript and that sort of stuff. But I’ve only started playing with maxscript a couple of days back.
So the ‘problems’ I’m now facing with maxscript aren’t so much in the ways of thinking, but more syntax wise. And this is a good example I think I know what I want to achieve and how, but I just didn’t know how to write it down exactly so that maxscript knows what I want too
Oh and the maxscript reference is indeed a goldmine, but sometimes you’ve got to know what to search for, like the structure definitions. I’ve spent about two hours looking in the reference for a solution to my problem, but I just didn’t hit on the struct command
Anyways, thanks a lot for your fast and helpful replies!
Definitely…I’ve been scripting heavily for 2 years and I still stumble across gems. Sorry for missing your question, but I thought you were totally new to scripting…not the case at all.
The maxscript docs are a goldmine of info on this sort of stuff.
I must admit, that I haven’t read the docs from start to end, but never found a clue that
stuff like bla.foo is legal maxscript or used widely.
Until I saw the code of pointcachewriter.ms that uses that syntax a few days ago.
Even in this forum this style is barely used.
Could you please tell me where in the docs this is referred to?
Maybe there is another gold vein hidden I missed so far.
Georg
No problem, I should have mentioned it.
I’ve got the struct working flawlessly now by the way
globals!! locals!!? i use structs for everthing.
struct test
(
testA,
testB,
fn testC =
(
return (testA + testB)
),
fn testD =
(
return (testC + (testA+testB))
),
endofstruct
)
test_c = test()
test_c.testA = 1
test_c.testB = 2
test_c.testC()
test_c.testD()
test_c.testA + test_c.testC() * test_c.testD()
ah fun! p.s remember to instance your struct.
ok – no excuse – I forgot the page about structs.
Maybe the example eek posted above or the examplesf97ao posted some timeago can find their way into the documentation?
Or are they allready there?
Georg