makeGlobal

  how to create globals on the fly /from variables and an example of a parent script which creates and voids its own global reference.
globals DO




  globals on the fly
At 14:50 15/03/99 -0800, Stephane Comeau wrote:
>This works in D7, probably not D6
>
>setaprop the globals, #foo, "someValue"
>put foo
>-- "someValue"
>
>So if you want to define a global on the fly you could do something like
>
>on makeGlobal, strGlobalName, flxTheValue
>  symGlobalName = symbol(strGlobalName)
>  setaprop the globals, symGlobalName, flxTheValue
>end
>
>You just have to make sure "symGlobalName" in the setaprop argument is a
>symbol.  Using symbol() you can easily create whatever global name you need
>with string operations beforehand.
>
>Steph
>(only uses globals as a last resort)
>
>

which of course looks so much better than the old way to do it:

on makeGlobals maxNum
  repeat with num = 1 to maxNum
    set cmd = "global globalNumber" & num & RETURN
    set cmd = cmd & "set globalNumber" & num & " = " & num
    do cmd
  end repeat
end

You can easily abuse the above handler to test what global soup is all about or 
where the extreme limits of director may be ( I crashed d5 with 29k globals once). 

But you can also use this, w/o the naughty repeat loop, to give an object the power 
to create (and void) its own global reference, as in:

------ parent script "test"
property myObjVar

on new me,objVar
  set myObjVar = objVar
  makeGlobal me
end


on makeGlobal me
  set cmd = "global " & myObjVar & RETURN
  set cmd = cmd & "set " & myObjVar & " = (me)" & RETURN
  do cmd 
end

on demolish me
  set cmd = "global " & myObjVar & RETURN
  set cmd = cmd & "set " & myObjVar & " = value(VOID)" & RETURN
  do cmd 
end


on test me
  return "yabbadabbadoo"
end

-------- end of script

-- Welcome to Director --
-- "This computer is running in  16-bit color depth."
new script "test","test"

showglobals

-- Global Variables --
version = "6.5"
test = <offspring "test" 1 1ae0072>

put test(test)
-- "yabbadabbadoo"

demolish test

showglobals

-- Global Variables --
version = "6.5"


I even use this do construct sometimes 
to make a repeat loop run in the message window, if only out of decadence ...


Home shock + cgi Bits 'n pieces Director Lingo Shocklets Contact










pageID=l_makeGlobal