PDA

View Full Version : Dynamic Rollout Accessing Functions


vasquez888
01-11-2008, 12:00 AM
Hi,

I'm having a problem at the moment where I cannot get a dynamically created rollout to execute a function that isn't also defined in the executed string.

Below is an example of what I mean


(
local message = "print this message"

function printMessage = ( format "%\n" message )

local rollStream = stringStream ""

format "rollout rlt \"Rollout\"\n" to:rollStream
format "(\n" to:rollStream
format "button btn1 \"Print Message\"\n" to:rollStream
format "on btn1 pressed do\n" to:rollStream
format "(\n" to:rollStream
format "printMessage()\n" to:rollStream
format ")\n" to:rollStream
format ")\n" to:rollStream

createDialog (execute (rollStream as string))
)


PrintMessage is undefined in the dynamic rollout. How can I make the function and also variables (e.g. 'message') available to the dynamic rollout?

Thanks

PiXeL_MoNKeY
01-11-2008, 12:18 AM
Seems to work fine here in Max 9.

-Eric

vasquez888
01-11-2008, 12:27 AM
Thanks for the reply Pixel_Monkey

I just tried it in Max 9 and I still get the same problem. Did you press the Print Message button?

When I press it, it says:

-- Type error: Call needs functon or class, got: undefined

Thanks

JHN
01-11-2008, 10:39 AM
You're simply out of scope. A dialog is in a scope of it's own...
If you'd added the function to the rollout it would work.
The only way to fix it now is to assign the function globally.

I would build a struct with functions you would need and make the struct global.
After that you could simply access the functions through the struct, from any place. Without making every function global and possibly create errors.


global mystruct
mystruct(
myfun1 =(),
myfun2 = ()
)

--from the rollout call:

mystruct.myfun1()


-Johan

PiXeL_MoNKeY
01-11-2008, 03:21 PM
I have no idea, what I did to make it work. Give JHN idea a try.

-Eric

JHN
01-11-2008, 06:37 PM
Did it run the first time you tried or the second time? Possibly it knew the function on second run. Or maybe you removed the braces from the code? That changes the scope too.

-Johan

PiXeL_MoNKeY
01-11-2008, 07:59 PM
I pulled it apart and ran some of the functions and variables by themselves to make sure it wasn't something in the functions or variables causing the problems.

-Eric

JHN
01-12-2008, 08:12 AM
If you run a function outside a scope () it becomes global... so that explains why calling from the dialog worked...

-Johan

vasquez888
01-13-2008, 02:36 AM
Thanks for the reply guys!

I'll give JHN's solution a go

vasquez888
01-13-2008, 02:51 AM
Thanks for the solution. It works a treat!

PiXeL_MoNKeY
01-13-2008, 04:21 AM
I could have swore I ran a gc(), but either I didn't or it didn't clear out the globals.

*SHRUGS*
-Eric

vasquez888
01-13-2008, 09:41 AM
Could someone explain to me how it is out of scope please?

If I create a normal rollout, one that isn't dynamic and then try to run the function without it being inside a global struct, it can access it properly.

However, if it is dynamically generated, it cannot "see" the function.

To illustrate my point, here is a piece of code where the non-dynamically created rollout can access the function whilst the dynamic rollout cannot


(
local message = "print this message"

function printMessage =
(
format "%\n" message
)


local rollStream = stringStream ""

format "rollout rlt \"Rollout\"\n" to:rollStream
format "(\n" to:rollStream
format "button btn1 \"Print Message\"\n" to:rollStream
format "on btn1 pressed do\n" to:rollStream
format "(\n" to:rollStream
format "printMessage()\n" to:rollStream
format ")\n" to:rollStream
format ")\n" to:rollStream

createDialog (execute (rollStream as string))

rollout testRollout "Test Rollout"
(
button btn2 "Print Message\n"

on btn2 pressed do
(
printMessage()
)
)

createDialog testRollout
)


From the above, testRollout has no problems whilst rlt does.

Thanks

JHN
01-13-2008, 10:24 AM
From the helpfile:
The scope of variables used in the evaluated string is global and not the scope in effect when the execute() method is executed.

Lookup execute() in the manual, it has more info on this as well. So again if you declare the function and the message global it would work for both rollouts. Hence my suggestion for a global struct with your functions.. you can access it from any scope then.

-Johan

vasquez888
01-13-2008, 11:08 AM
Thanks again JHN!

CGTalk Moderation
01-13-2008, 11:08 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.