Lesson 5: Basics of Functions

Back in Lesson 1 I gave you the challenge to make a simple interface and add 4 buttons which gave you MessageBox prompts when you pressed them. Hopefully you came up with something like this….

Try(DestroyDialog RL_Test)catch()
Rollout RL_Test "Test"
(
    button btn_A "A" across:2
     button btn_B "B"
     button btn_C "C" across:2
     button btn_D "D"
    on btn_A pressed do
     (
         MessageBox "Button pressed was A"
     )
    on btn_B pressed do
     (
         MessageBox "Button pressed was B"
     )

     on btn_C pressed do
     (
         MessageBox "Button pressed was C"
     )
    on btn_D pressed do
     (
         MessageBox "Button pressed was D"
     )
)
Createdialog RL_Test

Now, if you’re as lazy as I am, you might be thinking, damn I just had to write out almost the same thing 4 times, now wouldn’t that be annoying if it was a really long bit of script, especially if you kept having to fix bugs and update each of the 4 buttons. Well, you’ll be glad to know there’s a more logical way to deal with this. We create a function… Lets see how we create a Function.

fn myFunction =
(
    print "Some fool just ran my function!"
)
myFunction()

fn means we’re declaring a function, then we tell it what we want our function to be called follow by =. To run the function we simple ‘call’ it by whacking a () on the end of the name of the function so that maxscript knows we’re asking it to call a function and not defining a new variable. We could have a variable such as myFunction = “test” which confusingly wouldn’t break our script. So lets change our function to show our message box.

fn myFunction =
(
    Messagebox "Button pressed was "
)

We can then call our function on our button press.

on btn_A pressed do
(
     myFunction()
)

So it’s calling our function but we haven’t given any parameters to our function, which is this case is the ID of the button we pressed, so we need to declare an argument and pass that to our function and use it in our function. In this case we declare a parameter that we call ‘theID’ (you can call this whatever you like, as long as it’s not a reserved maxscript word, like do, then, if etc…) We can use it multiple times in our function, which means as our function grows in complexity we’re only work on one bit of code rather than 4 sections for each button.

fn myFunction theID =
(
    Messagebox ("Button pressed was " + theID)
    Print ("Button pressed was " + theID)
)

Confusingly we now ditch the () from our function call… because we’re calling our function with parameters specified, Maxscript is clever enough to know that we’re refering to a function call and not a variable.

on btn_A pressed do
(
 myFunction "A"
)

So lets put our function in all the buttons, and add our function into the ‘scope’ of our rollout, which means it’s only going to be callable from a control inside our script rollout, which is good as it’s not going to clash with someone else’s code. If you want a function accessible from other rollouts and other scripts, which you do sometimes, you need to declare it in global scope (outside of the brackets) but then you can get into more issue, I’ll leave that for another day.

Try(DestroyDialog RL_Test)catch()
Rollout RL_Test "Test"
(
    button btn_A "A" across:2
    button btn_B "B"
    button btn_C "C" across:2
    button btn_D "D"
    fn myFunction theID =
    (
       Messagebox ("Button pressed was " + theID)
       Print ("Button pressed was " + theID)
    )
    on btn_A pressed do
    (
       myFunction "A"
    )
    on btn_B pressed do
    (
       myFunction "B"
    )

    on btn_C pressed do
    (
       myFunction "C"
    )
    on btn_D pressed do
    (
       myFunction "D"
    )
)
Createdialog RL_Test
Advertisements

About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in Lessons, MaxScript. Bookmark the permalink.

One Response to Lesson 5: Basics of Functions

  1. Jaime says:

    Thanks for these….very helpful…determined to get some kind of grip on maxscript. 🙂

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s