Page 1 of 1

About Global Variables

Posted: Wed Sep 25, 2013 2:30 pm
by MGervais
Hello everyone,

I am back to programming after several years of inactivity since the glory years of HyperCard in the early 90's. I thought it would be easy to get back into it but I realise that I forgot a lot of it since and now I have to get back into it with baby steps. Therefore my questions might look very basic to you.

The project

I have created a small stack containing one filled circle in black and 2 buttons (+ and -). when the stack starts, the circle is set to a certain size and position. If the user click on the "+" button, the circle will grow in size and the other way around if the user click on the "-" button, the circle will reduce in size.

There are two level of scripts in this stack:

one at the stack level to declare global variables and set the circle to its starting size (see below):

global PWidth, PHeight

On OpenStack
put 84 into PWidth
put 84 into PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
end OpenStack


The global variables PWiidth and PHeight are used to set the Width and Height of the circle (I realize that this might be obvious for some of you)

There is also a script hidden in each button that triggers the size change and reposition the circle at the center of the stack (see below the + button script):

global PWidth, PHeight

on mouseUp
add 1 to PWidth
add 1 to PHeight
set Width of graphic "Pion" to PWidth
set Height of graphic "Pion" to PHeight
set location of graphic "Pion" to 66,66
end mouseUp


The Problem:

As it is written here, the stack works fine, but there is something I don't understand. It was my assumption that once global variables were declared at the stack level, they didn't have to be declared again at the page or object level. However, If I remove the line declaring PWidth and PHeight in the button script, nothing happens. It's like if the global variables had to be declared at all level for the script to work properly.

The other problem I have, is that every time I run the stack from live code in preview mode, The OpenStack handler is bypassed and the circle doesn't reinitialize.
Can anyone explain to me what I'm doing wrong here for both problems or if it's just the way it is?

Note: Please understand that this exercise is meant to be part of an introduction course. I'm aware that many of you could have written this script much more efficiently. The aim here is to keep the structure simple for teaching not to play script wiz. suggestions to write this better are welcome, but please do it separately after you have answered my questions. Those issues have to be solved anyways.

Keep in mind that writing this post was time consuming and that solving those two issues are my main priority.
Thank you all in advance.

Michel

Re: About Global Variables

Posted: Wed Sep 25, 2013 2:45 pm
by dunbarx
Hi.

Just like in HC, globals have to be declared in all scripts they are used. They can be declared outside and above all handlers that need them, as you did, or more locally, within each handler.

Do check out script local variables, a new addition in LC that HC did not support, and which are intermediate between globals and true local variables. Also check out custom properties, which make globals somewhat obsolete, and persist between sessions.

You already have a head start. It will all come back to you soon, after you get over a couple of major structural changes like groups.

What is "preview" mode?

Craig Newman

Re: About Global Variables

Posted: Wed Sep 25, 2013 2:54 pm
by FourthWorld
Global variables must be declared within each handler that uses them, or outside of handlers at the top of each script that uses them.

Re: About Global Variables

Posted: Wed Sep 25, 2013 4:49 pm
by MGervais
Thanks to both of you. That resolves one of the two issues I had.

Craig if globals have to be declared in all scripts they are used, like you say, it makes them indeed obsolete because they have no added value against local variables.

As for my second issue, what I meant by "preview mode" was just in fact executing the stack clicking on the arrow in the tool box. When I do that, The OpenStack handler seems to be bypassed. If the circle was previously altered in a previous session, it doesn't reinitialize to its original size when I run the stack again.

Is OpenStack the right handler to use if you want to make sure the script will be executed every time you run the stack?

Thanks again

Michel

Re: About Global Variables

Posted: Wed Sep 25, 2013 6:19 pm
by dunbarx
Nope.

If you declare a global in script "A" and put data into it, then that global (declared again in a different script) will be available with that data in that distant script. This persists through the session.

Just like HC did.

Local variables need not be declared, though they can be as a matter of style, but are purged when the handler containing them ends. The data in local variables must be passed explicitly. Similarly, script local variables are also purged. The advantage to those is that they are available throughout the script, to all handlers that need them, as opposed to a specific handler.

So a custom property, besides being able to hold vastly different data types, is just like a global, and becomes part of the data file, like a field. The one disadvantage to using them that way is that they must be referenced to the object that has the property. It is easier to (declare and) load a global and read it everywhere than to say:

put the globalAsCustomProperty of stack "yourStack" into field "yourField". Or whatever.

Secondly. You must mean that you are in "Browse" mode. Similar to HC again. But in any case, between sessions an "openstack" handler will surely fire. Are you sure you are not "resuming" an already open stack? You have a glitch somewhere. I bet you will find it.

Craig

Re: About Global Variables

Posted: Wed Sep 25, 2013 7:09 pm
by MGervais
Thank you Craig for this information it was very usefull. And yes I meant "Browse mode" indeed.

So are you saying that everytime I toggle from "edit mode" to "browse mode" the OpenStack script should be launched again? If the answer is yes, then I have a problem because it doesn't.

Michel

Re: About Global Variables

Posted: Wed Sep 25, 2013 8:38 pm
by Simon
Hi Michael,
toggling the two modes will not trigger an openStack (as the stack is already open).
The first suggestion is to actually close your stack then double click (or other) to run your stack again.

The other way is to right click on your stack and scroll down to "Send Stack Message" there you will see your openStack and any other handlers that are related to the object.

Simon

Re: About Global Variables

Posted: Wed Sep 25, 2013 9:06 pm
by dunbarx
Michel.

Is that what you meant, that a tool change did not trigger "openstack"?

There are many messages sent when you do change tools, if you really need to know that. Usually changes like that made by the developer are not of interest, but they are there. Open the message watcher and see. Good luck.

Craig