Page 1 of 1

Beaten by a global variable...

Posted: Fri Sep 25, 2015 4:13 pm
by MrCrussell
Hi, I have a situation where I want to pass a value between cards in the same stack, so I used a global to do it, but the value of the global doesn't seem to be recognised and I'm confused about why...

My global is named gVidModule, in the target card script it's declared as a global and it carries the value through as it should - so if, in the first card, I give it the value "pirate001" I see that value is present in the debugger (so far so good).

In the target script I want to check if "pirate001" is one of the list items in a variable called strBoughtItems. In the debugger, I see strBoughtItems is populated as I expect. Then I hit this code and it all goes wonky...

Code: Select all

      if gVidModule is among the items of strBoughtItems then
         answer( "Video is owned" )
      end if
Even though the debugger shows the following:
gVidModule has the value pirate001
strBoughtItems has the value item1,item2,item3,pirate001,item5

If I set the value of gVidModule directly before the condition is reached i.e.

Code: Select all

put "pirate001" into gVidModule
the condition is met as I expect it to be, but ordinarily, it is not.

I'm clearly missing something, but can someone help me out with what it is?

Regards

Chris

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 4:52 pm
by Klaus
Hi Chris,

in Livecode global variables have to be declared in EVERY script they are being used!
See also: http://forums.livecode.com/viewtopic.ph ... lit=global


Best

Klaus

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 4:53 pm
by MrCrussell
On a scale of 1 - 10, how big of an idiot am I?

If anyone is interested (or at least not horrified by my ignorance), it seems that "if gVidModule..." should actually be "if the value of gVidModule..", by adding "the value of" it seems to be working now.

I'll get my coat...

Chris

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 5:01 pm
by Klaus
Hi Chris,
MrCrussell wrote:On a scale of 1 - 10, how big of an idiot am I?
These go to eleven! https://www.youtube.com/watch?v=4xgx4k83zzc :D
MrCrussell wrote:... it seems that "if gVidModule..." should actually be "if the value of gVidModule..", by adding "the value of" it seems to be working now.
Hm, should not make a difference actually!?


Best

Klaus

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 5:05 pm
by MrCrussell
Hey Klaus, odd then.. once I made the change, everything started to work just fine. I'm using 7.0.5 ...

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 5:12 pm
by Klaus
Hi Chris,

we already have a stable 7.0.6 and 7.1, try them!
Bookmark this page: http://downloads.livecode.com/livecode/


Best

KLaus

Re: Beaten by a global variable...

Posted: Fri Sep 25, 2015 6:27 pm
by dunbarx
Chris.

Note that globals can be declared anywhere in a script, and there are sometimes reasons to NOT place tham at the top> But they must be above any handler that needs them.

Check out custom properties. Once set, these need not be declared at all, and survive sessions. I use then wherever I can, avoiding globals, well, wherever I can.

Craig Newman

Re: Beaten by a global variable...

Posted: Mon Sep 28, 2015 10:01 am
by MrCrussell
Hi Craig,

I looked at custom props, but I only need to hold values (a SKU for a shop item say) between card flips so custom props seemed a bit OTT.

Thanks for the advice though, I need to do some serious reading on the proper use of custom properties :)

Regards

Chris

Re: Beaten by a global variable...

Posted: Mon Sep 28, 2015 1:44 pm
by dave.kilroy
Hi Chris

Custom Props are easy peasy - and in your situation sound like they would work a treat.

In most ways custom props are 'better' than globals, in some ways not - although there are people who will tell you ALL globals should be sent to the fires of hell :)