Globals

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
macroman
Posts: 7
Joined: Thu Mar 11, 2010 12:20 am

Globals

Post by macroman » Wed Jul 28, 2010 11:35 pm

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

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Globals

Post by Curry » Thu Jul 29, 2010 6:15 am

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.
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

macroman
Posts: 7
Joined: Thu Mar 11, 2010 12:20 am

Re: Globals

Post by macroman » Thu Jul 29, 2010 4:49 pm

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.

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Globals

Post by Klaus » Thu Jul 29, 2010 5:00 pm

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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Globals

Post by Dixie » Thu Jul 29, 2010 5:04 pm

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

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Globals

Post by Klaus » Thu Jul 29, 2010 5:06 pm

HARRY???

Luv it! :D :D :D

macroman
Posts: 7
Joined: Thu Mar 11, 2010 12:20 am

Re: Globals

Post by macroman » Thu Jul 29, 2010 6:40 pm

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

Klaus
Posts: 14191
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Globals

Post by Klaus » Thu Jul 29, 2010 6:47 pm

Hi Mike,

you're welcome! :)

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Globals

Post by Curry » Fri Jul 30, 2010 12:34 am

Yes, code must be in a handler AND the handler has to be called. (Event-driven.)
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

Post Reply