Home Technical Talk

maxscript - function and rollout

polycounter
Offline / Send Message
rollin polycounter
Ok this drives me nuts and I have no idea why it is how it is.. I think I'm doing something wrong with the rollout stuff but not sure

this is the code

fn A1 = ( C1(); )
fn C1 = (return true;)

rollout teststuff "test stuff" (
	button buttonTestFunction1 "bla";
	on buttonTestFunction1 pressed do (
		print (A1());
	)
)

objEd = newRolloutFloater "mgto EAW scripts" 175 600 10 100
addrollout teststuff objEd;

when I run this script for the first time I get an maxscript error, telling me that C1 is undefined

when I run the script for the 2nd time I get true (like expected)



I found already out that a function can only be called after it was created in the script

but

my rollout stuff is at the end of the script.. so after my logic the script should already have found all functions

and WHY does it work after I've closed and restarted the script??

Replies

  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Is it because you're trying to call C1 before it's been defined? A1 is trying to call it, maybe the error would go away if you defined C1 before A1?
  • rollin
    Options
    Offline / Send Message
    rollin polycounter
    yes it would.. but thats not helping me :)

    I need all function above the rollout stuff to be ready bc I will call functions from inside function and they call functions...


    So the question is .. Am I doing something wrong or this simply the way maxscript works..

    bc if I can't change anything about it I have to try to find an order in all my function where all functions are ready when needed

    But if I'm doing something wrong I better learn how to do it right
  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, the problem is, as MoP said, the order of function C1 and A1 definition.
    (
        fn A1 = ( C1() )
        fn C1 = ( return true )
    	
        print (A1()) -- ERROR C1 is undefined for A1
    )
    

    Generates an error because A1 doesn't know what C1() is. The solution is to swap function definitions so C1 is known before A1 call.
    (
        fn C1 = ( return true )
        fn A1 = ( C1() )
    	
        print (A1()) -- OK C1 is defined before A1 call
    )
    

    Or use a forward declaration
    (
        local C1 -- forward declaration, filled at evaluation time
    
        fn A1 = ( C1() )
        fn C1 = ( return true )
    	
        print (A1()) -- OK C1 is declared before A1 call
    )
    

    As a general advice, wrap your test code in brackets. They define a code block avoiding every variable and function definition to clutter the default global scope, thus leading to false behaviours, like working code at second evaluation, but not at first.
  • rollin
    Options
    Offline / Send Message
    rollin polycounter
    forward declaration is exactly what I was looking for !!

    thx!
Sign In or Register to comment.