Struct: call member function from another member function for same instance

Struct: call member function from another member function for same instance

Anonymous
Not applicable
431 Views
5 Replies
Message 1 of 6

Struct: call member function from another member function for same instance

Anonymous
Not applicable
I need to call one member function from another inside struct, but it dosen't work just like that. When i try to call another function it says it's "undefined".
I did few google searches and found this forum: http://forums.cgsociety.org/archive/index.php/t-272425.html
On that forum same problem was addressed and resolved with this snippet:

struct objectStruct
(
--Function Definitions, those two are the magic lines 🙂
Function1=fn Function1(), --define functions
Function2=fn Function2(),

--locals
--Functions
fn Functions1=
(
Function2()--this would not work if they were not defined as empty above
),
fn Function2=
(
--function2 stuff.
)
)

Looks like all i needed were function definitions in the top of a struct. Well, sounds right...
But it didn't work for me!! MaxScript parsed these definitions as they were struct functions and therefore spited out
syntax errors about missing "=" signs :)). It looks like it doesn't even know about function definitions :(.
Maxscript documentation doesn't say about function definitions ether, why is that?

So if this solution didn't work how else do i call one function from another inside same instance of a struct?
0 Likes
432 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
In a similar context (SimpleObject plugin) I found that an early declaration
local function1
worked.
I could then have a method that called function1,
and finally make the actual function definition. fn

I think this should work for you as well.
0 Likes
Message 3 of 6

Anonymous
Not applicable
Well, i tried it many different ways and couldn't make it work.
If i declare it in such way, at least, it doesn't throw a parsing error now:
struct myStruct
(
f1 = myStruct.function1(),
fn function1 = ( print "Test" ),
fn function2 = ( f1() )
)
However when i call function2 it gives me an error "Unknown property: 'f1' "
When i wright something like:
f1 = myStruct.function1() -- Does it assign link to the function, or function's result value?

How did you accomplish early declaration? Can you post some sample code?
0 Likes
Message 4 of 6

Anonymous
Not applicable
Hi, my example was irrelevant as the rules of struct are somewhat different from the rules for plugin class.
However, try the following ...

-- without forward references
struct myStructA
(
fn f1 = ( print "in f1"; 55 ),
fn f2 = ( print "in f1"; f1()+11 ),
v1 = f2()
)
rrA = myStructA()

-- with forward references
struct myStructB
(
fn f1 = (),
fn f2 = (),

v1 = f2(),
fn f2 = ( print "in f1"; f1()+11 ),
fn f1 = ( print "in f1"; 55 )
)
rrB = myStructB()
0 Likes
Message 5 of 6

Steve_Curley
Mentor
Mentor
This works, without any forward references, locals or anything.
struct Person
(
Name, Age,
fn GetAge = ( Age ),
fn NameAge = ( Name + " " + (GetAge() as string))
)

Bill = Person "bill" 45
Bill.NameAge() --> "bill 45"

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 6 of 6

Anonymous
Not applicable
Thanks for replies!
Looks like it will work without forward references only if members were declared in order they were used, just like in C++. i didn't know that same rule applies to structs.
s_todd, your example worked for me and it does exactly what i want, thankyou. I don't know why i didn't think of that :).
Now i can make my structs look more like classes!
0 Likes