PDA

View Full Version : Is it not possible to have a struct definition within a struct definition?


Jon-Huhn
01-07-2008, 11:24 AM
As the title says. Thanks for your help!

vScourge
01-07-2008, 12:32 PM
I don't believe so. Although you can have struct instances as members inside other struct defs. Example:

struct s1 (
fn f1 = (
format "f1\n"
)
)

struct s2 (
var1 = s1(),

fn f2 = (
format "f2\n"
)
)

testInst = s2()
testInst.f2()
testInst.var1.f1()

That prints:
f2
f1

As far as substitutes for real classes go, structs aren't all that great. But they're definitely better than nothing. :)

drdubosc
01-07-2008, 12:50 PM
Maybe off-topic, but I guess you could implement inheritance in MaxScript structs .. using something like Perl's 'bless' function (tagging with a class name), @ISA array (a member array of ancestors), AUTOLOAD ( on failed fn call, look for fn in @ISA), etc, etc? Has anybody had a shot at this? Or would it be more effort than it's worth? I suppose a lot of it would be pretty slow, unless you were very clever.

Jon-Huhn
01-07-2008, 02:11 PM
Alright, that confirms what I was finding as well. Thanks!

PEN
01-07-2008, 03:04 PM
You can't build a struct in a struct but the data in one property of a struct can be an instance of a struct. You can define a new struct in a function that is in a struct and then place that instance in a property of the stuct.

Jon-Huhn
01-07-2008, 03:39 PM
Hey, Paul, that's a really sneaky trick, and it effectively gives me what I wanted. Thanks!

apocalypse2012
01-09-2008, 07:24 PM
You can define a new struct in a function that is in a struct and then place that instance in a property of the stuct.

Note: doing this will add the name of the struct definition to maxscripts global namespace. You can't define a struct definition as being in local scope. Struct definitions are always global. As a result, sticking a struct definition in a function has no benefit, but it does greatly complicate debuging global name collisions... Just something to think about.

PEN
01-10-2008, 03:28 PM
There is a test. On the first pass localstruct returns undefined. When evaled again it is defined.

How ever there is good reason to do this just for clean coding. Why put this struct out side of the function? why not define it if you need it only. I do this sort of thing all the time with structs that are used for sorting and storing data.


struct test
(
localStructure=undefined,

fn createLocalStructure=
(
struct localStruct (obj,name,wireColor)
localStructure=localStruct()
)
)
test=test()
test.createLocalStructure()

b=box()
test.localStructure.obj=b
test.localStructure.name=b.name
test.localStructure.wireColor=b.wireColor

format "This is the global struct: %\n" test
format "This is the local struct: %\n" localStruct
format "This is the instanced local struct: %\n" test.localStructure

apocalypse2012
01-10-2008, 04:42 PM
There is a test. On the first pass localstruct returns undefined. When evaled again it is defined.

Interesting. You are right. I see where my results went wrong.

If you test a name in global scope, you end up declaring it at the same time. As a result, if you reevaluate, the code that uses that struct name locally, it ends up global. Declaring the structDef name as local before the definition eliminates the ambiguity.

I should have realized that. Cool. Struct defs CAN be local.

CGTalk Moderation
01-10-2008, 04:42 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.