global variable seems to be a part-time global

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
kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

global variable seems to be a part-time global

Post by kevin11 » Wed Aug 24, 2011 9:30 am

Hi,

I'm having a little problem with a global, but probably have misunderstood globals.

My problem is a stack, say, with one card.

In the stack :

global G
on openstack
put 1 into G
go card "X"
end onopenstack

and in card X
global G
on opencard
answer G
end opencard

When I have made my morning coffee, and start livecode from scratch, and start the stack, card X sees G as empty.
If I use "Close and Remove from memory", and then open the stack again, card X thinks G is 1
If I then quit livecode completely, and restart livecode and open the stack, card X thinks G is empty.

It's behaving as though the stack script is running off to open card X before it has quite finished squeezing 1 into G, and so X doesn't see the global G. Then eventually it seems that G does become 1, but too late for card X. When I remove the stack from memory and reload, G has retained its value anyway so card X can use it.
If I quit livecode and restart, G is again empty when card X opens.

The global does seem to be shared by the stack and card, otherwise the card X would never see 1 in G. My problem seems to be that the card X is getting opened before G gets set.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: global variable seems to be a part-time global

Post by jmburnod » Wed Aug 24, 2011 9:59 am

Hi Kewin,

It work for me (with an old powerbook G4 for the first test)
I see you have a "end onopenstack" instead "end openstack" in your script

Code: Select all

on openstack
put 1 into G
go card "X"
end onopenstack
It is the same in your stack ?

Best regards from Geneva after tea time

Jean-Marc
https://alternatic.ch

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: global variable seems to be a part-time global

Post by kevin11 » Wed Aug 24, 2011 10:13 am

Hi Jean-Marc

That's just a typo when I posted the message, the stack script is correct !

Regards

Kevin

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: global variable seems to be a part-time global

Post by jmburnod » Wed Aug 24, 2011 10:29 am

Hi Kewin,

I tested it and it work

stack script

Code: Select all

global G
on openstack
   put 1 into G 
   go to card "X"
end openstack
Card script

Code: Select all

global G
on opencard
answer G
end opencard
Best

Jean-Marc
https://alternatic.ch

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: global variable seems to be a part-time global

Post by Dixie » Wed Aug 24, 2011 11:11 am

Kevin

Try declaring the global in a 'preOpenStack' handler instead of an 'openStack' handler... I've sometimes seen the same and going down the 'preOpenStack' road seems to solve the problem...

be well,

Dixie

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: global variable seems to be a part-time global

Post by kevin11 » Fri Aug 26, 2011 8:57 am

Dixie wrote: Try declaring the global in a 'preOpenStack' handler instead of an 'openStack' handler... I've sometimes seen the same and going down the 'preOpenStack' road seems to solve the problem...
Hi,

Yes, this solves it, and I think I have found out why.

When you start livecode, it goes to the last card you were on previously (X, say), and then it executes openstack.
If card X is relying on globals set up in openstack, card X fails.

If I do what you suggest, it works. I think what happens is when livecode starts, it executes preopenstack (setting up the globals), then it jumps to the last card that was in use, X, which now works, and then it executes openstack.

So, preopenstack (set globals), display global-needy card X (forced by livecode), openstack is the order that works.

Thank you Dixie

Kevin

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: global variable seems to be a part-time global

Post by jmburnod » Fri Aug 26, 2011 10:59 am

Hi Kewin,

I tested with livecode on an iMac and i have the same result of you. "go to cd x jump the opencad of the first cd"
(the first test was with rev 4.0 on a powerbook)

It work for me if you set the global G to 1 at preopenstack but i think it is better using preopencard in the script of the first cd instead preopenstack

I put a little stack to see the messages history

Best regards

Jean-marc
Attachments
HistoryMsg.livecode.zip
(2.1 KiB) Downloaded 227 times
Last edited by jmburnod on Fri Aug 26, 2011 11:45 am, edited 1 time in total.
https://alternatic.ch

kevin11
Posts: 101
Joined: Thu Aug 11, 2011 5:02 pm

Re: global variable seems to be a part-time global

Post by kevin11 » Fri Aug 26, 2011 11:20 am

Hi Jean-Marc,

Thanks for this.

My problem is that my openstack script was trying to set globals by reading a history log, and then going to the correct card.
Livecode was taking me to card X, and then executing openscript, which was too late, because by then card X had failed.

If I use preopenstack, it of course works ok.

I cannot set globals up in the preopen card, as the card is only called as a result of a decision made elsewhere, and has to populate itself according to those globals. If it set up its own globals, it would always populate itself the same way.

Thanks

Kevin

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: global variable seems to be a part-time global

Post by jmburnod » Fri Aug 26, 2011 11:33 am

Hi Kewin,

Thank for explanations
my last message contains one error.
I changed it

best

Jean-Marc
https://alternatic.ch

Post Reply