Beaten by a global variable...

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
MrCrussell
Posts: 23
Joined: Wed Nov 05, 2014 11:30 am

Beaten by a global variable...

Post 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
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Beaten by a global variable...

Post 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
MrCrussell
Posts: 23
Joined: Wed Nov 05, 2014 11:30 am

Re: Beaten by a global variable...

Post 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
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Beaten by a global variable...

Post 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
MrCrussell
Posts: 23
Joined: Wed Nov 05, 2014 11:30 am

Re: Beaten by a global variable...

Post by MrCrussell »

Hey Klaus, odd then.. once I made the change, everything started to work just fine. I'm using 7.0.5 ...
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Beaten by a global variable...

Post 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
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10502
Joined: Wed May 06, 2009 2:28 pm

Re: Beaten by a global variable...

Post 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
MrCrussell
Posts: 23
Joined: Wed Nov 05, 2014 11:30 am

Re: Beaten by a global variable...

Post 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
dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Beaten by a global variable...

Post 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 :)
"...this is not the code you are looking for..."
Post Reply