Page 1 of 1

global variable seems to be a part-time global

Posted: Wed Aug 24, 2011 9:30 am
by kevin11
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.

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

Posted: Wed Aug 24, 2011 9:59 am
by jmburnod
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

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

Posted: Wed Aug 24, 2011 10:13 am
by kevin11
Hi Jean-Marc

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

Regards

Kevin

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

Posted: Wed Aug 24, 2011 10:29 am
by jmburnod
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

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

Posted: Wed Aug 24, 2011 11:11 am
by Dixie
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

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

Posted: Fri Aug 26, 2011 8:57 am
by kevin11
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

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

Posted: Fri Aug 26, 2011 10:59 am
by jmburnod
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

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

Posted: Fri Aug 26, 2011 11:20 am
by kevin11
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

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

Posted: Fri Aug 26, 2011 11:33 am
by jmburnod
Hi Kewin,

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

best

Jean-Marc