Notifications
Clear all

[Closed] Dynamic Rollout Accessing Functions

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 "%
" message )
  
  	local rollStream = stringStream ""
  
  	format "rollout rlt \"Rollout\"
" to:rollStream
  	format "(
" to:rollStream
  	format "button btn1 \"Print Message\"
" to:rollStream
  	format "on btn1 pressed do
" to:rollStream
  	format "(
" to:rollStream
  	format "printMessage()
" to:rollStream
  	format ")
" to:rollStream
  	format ")
" 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

13 Replies

Seems to work fine here in Max 9.

-Eric

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

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

I have no idea, what I did to make it work. Give JHN idea a try.

-Eric

 JHN

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

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

If you run a function outside a scope () it becomes global… so that explains why calling from the dialog worked…

-Johan

Thanks for the reply guys!

I’ll give JHN’s solution a go

Thanks for the solution. It works a treat!

Page 1 / 2