View Full Version : Why do my functions not work?

03-12-2006, 04:20 PM
I've been having problems with calling functions from within a rollout. Please can someone help me work out what I am doing wrong! :)
Whats weird is, that the function works fine.... then after working on other bits of code, the function stops working, apparently for no good reason :(

for example:
the function:-
fn editmat mat =
if (mat != undefined) do
if mateditor.isopen() == false do
( ()
activeslot = medit.getActiveMtlSlot()
setMeditMaterial activeSlot mat
catch (messagebox "There was a problem putting the material to the editor")

called from the following rollout

local CreateMatRollout
fn CreateMatRollout myCollection =
( MLister.vrayMatRC = rolloutCreator "vrayMatsRollout" "Materials"
fn CreateControls =
MLister.vrayMatRC.addControl #Button (("MatEdit" + MLister.count as string) as name) "E" \
paramStr:(" offset:[70,-18] align:#left width:10 height:13 filter:on tooltip:\"Edit material in material editor\" ")
MLister.vrayMatRC.addHandler (("MatEdit" + MLister.count as string) as name) #'pressed' \
codeStr:("editMat MLister.matindex[" + MLister.count as string + "]")


Yet, if I use the same code from the function instead of calling the function, ie
MLister.vrayMatRC.addHandler (("MatEdit" + MLister.count as string) as name) #'pressed' \
codeStr:("try(if (MLister.matindex[" + MLister.count as string + "] != undefined) do (if (mateditor.isopen() == false) do ( ())\n local activeslot = medit.getActiveMtlSlot()\n setMeditMaterial activeSlot MLister.matindex[" + MLister.count as string + "]))catch (messagebox @There was a problem putting the material to the editor@)")

The button works fine!

Can anyone explain what I am doing wrong? I know its something to do with scope of variables, or the way the function is called.... but I just can't figure it out.

The error I am getting is :-

-- Error occurred in MMatEdit12.pressed()
-- Frame:
-- editmat: undefined
>> MAXScript Rollout Handler Exception: -- Type error: Call needs function or class, got: undefined <<

The above code is incomplete, so dont try to copy-paste to test it. If you want to view the full code, please download it from here. (

Thanks in advance for any assistance.

03-13-2006, 01:21 AM
As usual, after posting the question on cgtalk.... I fix the problem soon after!

Looks like I needed to make the function part of the struct...

ie after the function, I needed to have :-

MLister.curmatoff = curmatoff

Can anyone explain the reason for this? Why do I need to pass functions into the struct? I am a bit confused about the processes going on here that dictate that I need to do this. :cry:

Anyway, I'm glad to finally have fixed the problem, and learnt a little more about the dark arts of maxscript!

CGTalk Moderation
03-13-2006, 01:22 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.