Global Procedure

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
carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Global Procedure

Post by carel » Mon Aug 12, 2013 5:22 pm

How can I create a global procedure that I can call later from other cards?

And global variables?

Thanks,

Carel

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Global Procedure

Post by dunbarx » Mon Aug 12, 2013 6:33 pm

Hi.

You must read the user guide and play with the lessons and tutorials. And the dictionary. Not cover to cover, but if you had looked up "global", you would have had considerable information about your second question. Global variables are explicitly declared in a script or handler, and are thereafter available anywhere at any time in the current session.

"Global procedures" are, I think you must mean, handlers that reside in scripts that are high in the message hierarchy, perhaps at the stack or backscript level. They may also reside in other stacks put "into use". These handlers may then be called from object handlers, and can be thought of as "libraries".

But this information is useless unless you have a sense of the message path. This is a fundamental idea in LC. Write back with any questions or problems you may ever have, but you must get going by yourself until you learn how to drive. We will give you directions anywhere at all once that happens.

Craig Newman

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Global Procedure

Post by carel » Tue Aug 13, 2013 8:28 pm

Thanks for the info Craig.

I looked up global variables - easy.

Just a quick question: So for my public "procedure"

I can use:

command MyCommand
do some things
end MyCommand

or function MyFunction
return something
end MyFunction

can I just put this on the stack script and then access it from any card?

Gonna try it now :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Global Procedure

Post by dunbarx » Wed Aug 14, 2013 2:41 am

Right on. You will be exhilarated.

Again, get hold of the message hierarchy. This construct is utterly fundamental. It gives enormous power to you, and is very simple to grasp.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Global Procedure

Post by FourthWorld » Wed Aug 14, 2013 3:22 am

For an overview of the options for extending the message path, this may help:


Extending the LiveCode Message Path
An introduction to using Libraries, FrontScripts, BackScripts and Behaviors
in RunRev's LiveCode Programming Language
http://www.fourthworld.com/embassy/arti ... _path.html
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Global Procedure

Post by carel » Thu Aug 15, 2013 6:20 pm

Thanks for the input guys - I'm finally starting to enjoy livecode now.

I have this conundrum though:

I have this in my stack script:

global question, answer1, answer2, answer3, correctanswer, picturenumber, GlobalQuestionNumber
command Populate
answer "test"
put question[GlobalQuestionNumber] into field questionbox on card 2
put answer1[GlobalQuestionNumber] into field answer1box on card 2
put answer2[GlobalQuestionNumber] into field answer2box on card 2
put answer3[GlobalQuestionNumber] into field answer3box on card 2
end Populate

and this in a button script on card 1:

put 1 into GlobalQuestionNumber
Populate -- answer "test" executes but nothing else
put answer2[GlobalQuestionNumber] into field answer2box on card 2 -- this also executes correctly

That indicates that my command Populate runs, but why does only the answer "test" executes and not the other code?

Thanks again,

Carel

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Global Procedure

Post by dunbarx » Thu Aug 15, 2013 7:03 pm

Did you declare the global in the button script? Globals have to be declared in each script (or handler) they will be used.

Craig Newman

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Global Procedure

Post by carel » Thu Aug 15, 2013 7:40 pm

aaahhhhh - thank you! - I read somewhere that if you declare it in the stack script you don't have to declare it again - maybe I just understood wrong.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Global Procedure

Post by dunbarx » Thu Aug 15, 2013 8:15 pm

Half wrong.

Declarations may be made in the script of any object, outside of any handler. In such a case, all handlers FOLLOWING the declaration will have access. But only in that script, not beyond. The shouting is why most declarations are made at the top of the script. Similarly, script local (not global) variables are declared, er, similarly.

However, if you declare a global in the stack script, and also declare it in the script of a control somewhere, that global is available by virtue of it being so declared.

If the declaration is made in a handler, it must be made in each handler that needs it, global or not. These are truly local.

Craig Newman
Last edited by dunbarx on Thu Aug 15, 2013 11:17 pm, edited 1 time in total.

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Global Procedure

Post by carel » Thu Aug 15, 2013 9:42 pm

Thanks again Craig.

This is something I have to get used to - back in the day when I learned Turbo Pascal (from a book!) I used to keep most of my variables global and that habit filtered down to just about every programming language I used. (Yes I know it is bad practise)

I'm glad you guys are online when I'm programming (at night) :) - going to bed just now though, keep well.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10364
Joined: Wed May 06, 2009 2:28 pm

Re: Global Procedure

Post by dunbarx » Thu Aug 15, 2013 11:19 pm

No sweat.

Since you are a programmer but new to xTalk, think about abandoning globals altogether in favor of custom properties. Do you know about these?

Craig

Post Reply