Problem with Global Variables in my stack

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
gwildbur
Posts: 14
Joined: Wed Apr 25, 2012 11:57 pm

Problem with Global Variables in my stack

Post by gwildbur » Thu Apr 26, 2012 12:28 am

Could someone tell me why my global variable tStartHour is not being passed onto card 3? As you can see it works as we move to card 2 but by card 3 the output from the variable is it's name.I have attached my stack. What I am trying to do is confirm the start and end times for a timer I am programming. My logic does not appear to agree woth Livecode's logic. I am a complete newbie and would appreciate any constructive feedback. Thanks.

I set the following variables at the Stack level:

global tStartHour, tStartMinute, tStartAMPM, tEndHour, tEndMinute, tEndAMPM

In the first card of the stack I pass a field called "StartHour" to a global variable called tStartHour and then as a test I answer tStartHour and the value is 4 which is correct. When I get to the third card and answer tStartHour the value I get is tStartHour. Does the value of a global variable not continue from card to card?

I wish I could upload my stack so you could see the code but when I try to attach the stack I get an error message saying livecode is not a valid extension.

Any Help would be appreciated

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Problem with Global Variables in my stack

Post by sturgis » Thu Apr 26, 2012 12:35 am

You have to declare a global in each script where it will be used. If your answer dialog script is running from the card script for card 3, declaring your global at the top of the script should solve the issue.

gwildbur
Posts: 14
Joined: Wed Apr 25, 2012 11:57 pm

Re: Problem with Global Variables in my stack

Post by gwildbur » Thu Apr 26, 2012 1:42 am

I have declared all of the variables as global on each card and still on the third card the variable tStartHour answers as tStartHour.

Any idea how i can upload my stack so you can see what I am doing.

I appreciate you help here.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Problem with Global Variables in my stack

Post by sturgis » Thu Apr 26, 2012 2:05 am

If you have a dropbox account you can put it in your dropbox public folder and post the link. You can attach .zip files here but I think you need more posts before you can do so. If you have a website and can post a url to the stack that will work too.

gwildbur
Posts: 14
Joined: Wed Apr 25, 2012 11:57 pm

Re: Problem with Global Variables in my stack

Post by gwildbur » Thu Apr 26, 2012 2:10 am

I have attached a zip file of my stack. If you could have a look it would be greatly appreciated.
Attachments
Data Entry Stack.rev.zip
(3.05 KiB) Downloaded 255 times

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Problem with Global Variables in my stack

Post by sturgis » Thu Apr 26, 2012 2:33 am

ok, from a quick glance it looks as though the text "tStartHour" on card 3 is leftover from an earlier time (probably before setting up your globals and such) After digging a bit more, the group of fields on that card have no script, but the field for the start hour does have an opencard script which is supposed to put the contents of the hour field from card 1. This would work fine except opencard is not sent to fields it is sent to the card in question and then if there is no opencard handler available on that card it goes up to stack looking for the handler until the message is either handled or the end of the message path is reached.

If you move that opencard script from the field to the card things should start working and since you are directly referring to the field on card 1 you don't really need the global variable. (though you could use it instead of a field reference and accomplish the same thing) Either way, to get your opencard handler to work it needs to be in either the card script or the stack script. Based on what you are trying to accomplish, the card script is probably the best solution.

EDIT: Just noticed, the opencard script refers to the field containing the start hour from card 1, but you have to be more explicit when using a field reference.

put field "fieldname" of card "cardname" ..... (if it is a multistack project of course you could add 'of stack "stackname"' to point more directly to the correct field. Without changing to point to the correct card the script on card 3 won't be able to find a field on card 1.

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: Problem with Global Variables in my stack

Post by AtoZ » Thu Apr 26, 2012 9:50 am

Just a side note: I notice you have named all of your globals beginning with the letter t. You are, of course, free to name them anything you want (as long as you don't use certain non-alphanumeric characters or words reserved by LiveCode), but the convention many programmers use is to prefix their globals with the letter g (obviously for global), while prefixing local variables with the letter t (t for temporary -- the lower-case l is avoided as a prefix because of it's possible confusion with the number 1 -- in some fonts they are indistinguishable).

Being new to LiveCode you may have see the prefix t used in other scripts and not realized that they were being used only for local variables.

There is a brief description of the common naming conventions on p. 165 of the LiveCode User Guide, and a more complete discussion at this link:

http://www.fourthworld.com/embassy/arti ... tml#Naming

Good luck with your endeavors with LiveCode and welcome to the community.
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

gwildbur
Posts: 14
Joined: Wed Apr 25, 2012 11:57 pm

Re: Problem with Global Variables in my stack

Post by gwildbur » Thu Apr 26, 2012 5:14 pm

Thanks to both of you for your feedback.

It is wierd that even though I declare the global variables on the stack and each card, I still have to declare the same global variables in the script as well.

I also changed the naming of my glob al variables to g------- instead of t-------- .

Regards,

Geoff

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Problem with Global Variables in my stack

Post by mwieder » Thu Apr 26, 2012 5:32 pm

It is wierd that even though I declare the global variables on the stack and each card, I still have to declare the same global variables in the script as well.
I agree. Globals should be global. I filed a bug on this many years ago and it has yet to change.

Nonetheless, that's the way it is. Best just to grumble about it and move on. Or better yet, find a way to minimize the use of global variables.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Problem with Global Variables in my stack

Post by sturgis » Fri Apr 27, 2012 12:02 am

Agreed, global should be global.

The problem though was that the field in question at some point had the name of the variable placed in it. Then things got changed around and the field was not being updated when the card was opened. (the opencard handler was in the field script not the card script)

As mark says, minimizing use of globals is a good thing, and in this case since the value in question is already in a field, seems a little redundant to have the global anyway. Just access the value of the field directly and the need for the global disappears entirely!

Post Reply