Page 1 of 1
Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 12:28 am
by gwildbur
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
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 12:35 am
by sturgis
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.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 1:42 am
by gwildbur
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.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 2:05 am
by sturgis
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.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 2:10 am
by gwildbur
I have attached a zip file of my stack. If you could have a look it would be greatly appreciated.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 2:33 am
by sturgis
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.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 9:50 am
by AtoZ
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.
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 5:14 pm
by gwildbur
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
Re: Problem with Global Variables in my stack
Posted: Thu Apr 26, 2012 5:32 pm
by mwieder
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.
Re: Problem with Global Variables in my stack
Posted: Fri Apr 27, 2012 12:02 am
by sturgis
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!