Page 1 of 1

Globals

Posted: Wed Jul 28, 2010 11:35 pm
by macroman
Hi everyone,

Can anybody please tell me what's going wrong in the following snippet? Instead of getting the value returned by getApplicationPath(), I'm getting nothing. I've cut the script to bits to try to see what is happening and it is now driving me up the wall - there's not a lot left to cut anymore. The getApplicationPath() function just returns the path of the application in text and it is working fine.


global x
put getApplicationPath() into x

on mouseUp
put x into field "textArea"
end mouseUp


Thanks a mil,

Mike

Re: Globals

Posted: Thu Jul 29, 2010 6:15 am
by Curry
Your "put getApplicationPath() into x" needs to be in a handler; is it? Otherwise it will never happen.

Also, each script (not each handler in a script, but the script of each object) that uses your global needs the declaration "global x" which you can place at the top of the script, outside of any handlers.

Re: Globals

Posted: Thu Jul 29, 2010 4:49 pm
by macroman
Thanks.

I was of the opinion that I could initialise variables outside handlers and that it wasn't required to reuse the 'global' statement once the original declaration of the global is in the same script.

I've tried both changes and It still doesn't work though.

Re: Globals

Posted: Thu Jul 29, 2010 5:00 pm
by Klaus
Hi Mike,

it DOES work this way! Could you post your code again please?

You could also initialise the global when the stack opens, so the global is ready to use then! Do like this:

Code: Select all

on openstack
   global x
   put getApplicationPath() into x
   # More openstack stuf...
end openstack
Then you can use the global wherever in your app like in your mouseup handler:

Code: Select all

on mouseUp
   global x
   put x into field "textArea" 
end mouseUp
Best

Klaus

Re: Globals

Posted: Thu Jul 29, 2010 5:04 pm
by Dixie
macroman...

A silly example using your function name

Code: Select all

global x

function getApplicationPath harry
   put word 1 to 3 of the long name of this stack into harry
   return harry
end getApplicationPath

on mouseDown
   put getApplicationPath() into x
end mouseDown

on mouseUp
   put x 
end mouseUp
be well

Dixie

Re: Globals

Posted: Thu Jul 29, 2010 5:06 pm
by Klaus
HARRY???

Luv it! :D :D :D

Re: Globals

Posted: Thu Jul 29, 2010 6:40 pm
by macroman
Thanks guys, we're off again and it's working. I'll paste the rest of the code back in now and see if it explodes (c;

I think years of C, C++, Delphi, PHP and many others have taken their toll and I need to start thinking in terms of handlers.

Once again, thanks a mil!

Mike

Re: Globals

Posted: Thu Jul 29, 2010 6:47 pm
by Klaus
Hi Mike,

you're welcome! :)

Re: Globals

Posted: Fri Jul 30, 2010 12:34 am
by Curry
Yes, code must be in a handler AND the handler has to be called. (Event-driven.)