Home Technical Talk

Maxcript Overload a struct-function

interpolator
Offline / Send Message
SimonT interpolator
As the documentation says, you can defined a function in a struct like
struct foo
(
a, b, c,
fn baz n = sqrt (n + 1),
fn bar x y = print (x ^ 2 + y ^ 2)
)

but is it possible, to overload this function later? So that you tell the function to do different things later in the code? This means, the script could always execute the same function, but the result would be different depending on the code content.

Is this possivble=

Replies

  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Do you mean function overloading where the script calls the function based on arguments? From what Im reading sounds like you want something like polymorphism.
  • SimonT
    Options
    Offline / Send Message
    SimonT interpolator
    I would like to define the function like above but also be able to do this later:
    fn foo.baz = (
    print "hello new world"
    )

    And at this moment the foo.baz() function shall not calculate sqrt(n+1) anymore but just write "hello new world".
  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Should be able to do this:
    struct coolStruct 
    (
    	oldFunc = (
    		fn structFunc =
    		(
    			myVal = 10
    			print myVal
    		)
    	)
    )
    b = coolStruct()
    b.oldFunc()
    newFunc = (fn test = (print "We changed"))
    b.oldFunc = newFunc
    b.oldFunc()
    

    Not sure if this would be legal way to do it though :P
  • The Flying Monk
    Options
    Offline / Send Message
    The Flying Monk polycounter lvl 18
    haiddasalami, I think thats right. You can change amy member of struct as long as you've instanced it.

    This works aswell:
    struct structBlah
    (
      foo,
      bar,
      x,
      y
    )
    blah = structBlah()
    
    blah.foo = fn foo =
    (
      print "foo"
    )
    

    You can assign any object to a struct member, Maxscript doesn't care if it is a variable, function or a struct.
  • SimonT
    Options
    Offline / Send Message
    SimonT interpolator
    Thank you guys! And if you want to define a FN directly in the struct it works that way (i had to predefine the fuction name "baz"...if i dont do this, i get errors...):
    struct testStruct 
    (
    	baz,
    	a = (	fn baz = print 1 )
    )
    
    b = testStruct()
    b.a()
    b.a = ( fn bla = print 2 )
    b.a()
    
Sign In or Register to comment.