2

I'm having the age old problem of Maxscripts not working the first time they are run (from a cold start) because the functions need to be declared before they are used.

The following script will fail the FIRST time it is run:

fOne()
function fOne = 
(
    fTwo()
)

function fTwo = 
(
    messageBox ("Hello world!")
)

We get the error : "Type error: Call needs function or class, got: undefined". Second time around, the script will run fine.

However, adding a forward declaration to the script, we no longer get an error. Horrah! BUT the function is no longer called. Boo!

-- declare function names before calling them!
function fOne = ()
function fTwo = ()

fOne()
function fOne = 
(
    fTwo()
)

function fTwo = 
(
    messageBox ("Hello world!")
)

So, how does forward declaration really work in Maxscript?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125

3 Answers3

2

you cannot call something before declaring it... it's not actionscript... it works the second time you run the code because it can find the function...

struct myFunc (
    function fOne =  (
        fTwo()
    ),
    function fTwo =  (
        messageBox ("Hello world!")
    )
)
myFunc.fOne()
RappyBMX
  • 29
  • 2
  • Ah you found the [example](http://districtf13.blogspot.co.uk/2011/04/maxscript-function-pre-declaration.html) that I did. There's got to be another way other than encasing functions in more brackets and commas. – Ghoul Fool Apr 04 '13 at 08:13
2

To my future self: Keep everything local. Declare the section function as a (local) variable. Be aware of whereabouts in the code you define the functions

( -- put everything in brackets

    (
    -- declare the second function first!
    local funcTwo

    -- declare function names before calling them!
    function funcOne = ()
    function funcTwo = ()

    funcOne()

    function funcOne = 
    (
    funcTwo()
    )

    function funcTwo = 
    (
    messageBox ("Hello world")
    )
)
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
1

the "::" is the key. Sadly this isn't a well known or documented feature. http://lotsofparticles.blogspot.ie/2009/09/lost-gems-in-maxscript-forcing-global.html

::fOne() -- this will error if forward declaration is not working.

function fOne = 
(
    ::fTwo()
)

function fTwo = 
(
    messageBox ("Hello world!")
)
  • I don't know why this got downvoted. It's the actual, correct answer. Only thing - I don't know why the auther included the inital ::fOne statement. Works fine without it for me (Max 2018). This is damn useful - Please upvote this answer to make it the top one – FrozenKiwi Apr 27 '18 at 13:48
  • just to show it was all working. present me would be terser :) – CaptainBumbleFudge Dec 19 '19 at 02:25