Page 1 of 1

Global Procedure

Posted: Mon Aug 12, 2013 5:22 pm
by carel
How can I create a global procedure that I can call later from other cards?

And global variables?

Thanks,

Carel

Re: Global Procedure

Posted: Mon Aug 12, 2013 6:33 pm
by dunbarx
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

Re: Global Procedure

Posted: Tue Aug 13, 2013 8:28 pm
by carel
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 :)

Re: Global Procedure

Posted: Wed Aug 14, 2013 2:41 am
by dunbarx
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

Re: Global Procedure

Posted: Wed Aug 14, 2013 3:22 am
by FourthWorld
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

Re: Global Procedure

Posted: Thu Aug 15, 2013 6:20 pm
by carel
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

Re: Global Procedure

Posted: Thu Aug 15, 2013 7:03 pm
by dunbarx
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

Re: Global Procedure

Posted: Thu Aug 15, 2013 7:40 pm
by carel
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.

Re: Global Procedure

Posted: Thu Aug 15, 2013 8:15 pm
by dunbarx
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

Re: Global Procedure

Posted: Thu Aug 15, 2013 9:42 pm
by carel
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.

Re: Global Procedure

Posted: Thu Aug 15, 2013 11:19 pm
by dunbarx
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