So far not a fan...

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

Klaus
Posts: 13866
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: So far not a fan...

Post by Klaus » Wed Jul 29, 2015 8:20 pm

bigal60 wrote:Klaus,

This worked, however, I had to close and reopen the Stack before the change would take effect. What's up with that?
yep, that's the way LC works! :D

As the name suggests this will only get executed when the stack opens but before it appears on screen.

But the message box is your friend, it is the LC "command line" tool. Open the message box -> CMD-M and enter:
send "preopenstack" to stack "name of oyur stack here"
then hit ENTER!

This way you "generate" the "preopenstack" event manually and the code will get executed.


Best

Klaus

SparkOut
Posts: 2857
Joined: Sun Sep 23, 2007 4:58 pm

Re: So far not a fan...

Post by SparkOut » Wed Jul 29, 2015 8:29 pm

All scripts are event (or message) driven. The stack script holds handlers which respond to appropriate messages, just the same was as a button script holds handlers such as on mouseUp. A stack script can have a mouseUp handler just the same as a button and according to the message path may well respond to all mouseUp events when anything is clicked - but this will depend on whether the mouseUp message is trapped by another handler lower down (or higher up, depending on your viewpoint) the message path, and whether another handler may pass the message. Typically a stack script will hold other handlers than an on mouseUp, such as on preOpenStack or on openStack. These handlers will trap the said messages in the stack script. Once saved and compiled the stack script is ready to respond to events (messages). If you put the statement to change the value of the global variable in a preOpenStack script you will need to open the stack for that message to be generated, so that the script will receive it and the handler will trap it. If you don't open the stack again then the message will not be generated, so the handler will not react. Merely editing the script to say put 1063 into deckimage will not make a message for the script to handle.[edit]Use the message box to send messages, as Klaus said[/edit]
It seems that you are trying to set the global variable as if it were a property. Now this is more like what you are trying. Custom properties have already been mentioned by Craig. You already seem to be using custom properties (the iconImg of the button). Forget the global variable.

Code: Select all

on mouseUp
  if the icon of me <> the cDeckImg of this stack then
    set the icon of me to the cDeckImg of this stack
  else
    set the icon of me to the iconImg of me
  end if
end mouseUp

Code: Select all

on preOpenStack
  set the cDeckImg of this stack to 1063
end preOpenStack
Custom properties will save and persist and can be accessed from anywhere, so as accessible as a global variable but with some extra features.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: So far not a fan...

Post by jacque » Thu Jul 30, 2015 6:26 pm

@bigal60, I think these discussion stacks might help you understand the structure and behavior of the LC engine:

http://www.hyperactivesw.com/revscriptc ... ences.html

The stacks were written many years ago for an older version of the LC engine but they are still accurate. In particular, the stacks about scripting syntax and the message path would be helpful for what you're dealing with right now. LC has a different paradigm than what you may be used to, but once the details fall in place it all makes sense.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: So far not a fan...

Post by phaworth » Thu Jul 30, 2015 8:24 pm

One interesting thing about this is that apparently including any text outside of handler block in a script does not result in compile errors. I guess it's pretty obvious you shouldn't do that once you understand how LC works but in this case,it would have alerted the OP to the root cause of the problem.
Pete

bigal60
Posts: 10
Joined: Tue Jul 28, 2015 3:49 pm

Re: So far not a fan...

Post by bigal60 » Thu Jul 30, 2015 8:51 pm

Yes I saved the Stack. I have a nervous habit of saving a lot.

BTW, after quite a few hours of work, LiveCode corrupted the Stack file. I shan't be using LiveCode anymore.

Regards.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: So far not a fan...

Post by jacque » Thu Jul 30, 2015 9:05 pm

BTW, after quite a few hours of work, LiveCode corrupted the Stack file. I shan't be using LiveCode anymore.
That's almost unheard of. But if you save a stack in LC 7.x and then try to open it in LC 6.x it can report corruption because the file formats are different. The fix is to open the stack in 7.x and either work on it there, or do a "save as" and save the copy in 6.x file format. There's a menu at the bottom of the Save dialog that lets you specify which file format to use.

If you are doing all your work in LC 7.x then I vaguely recall a dp release that did cause a problem. That was corrected, so make sure you're using the latest stable version.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: So far not a fan...

Post by MaxV » Fri Jul 31, 2015 3:45 pm

Please, don't use global variables in your code, here the reason:

http://newsletters.livecode.com/may/iss ... etter3.php




:evil: :evil: :evil: :evil: :evil: :evil: :evil: :evil: :evil:
It's my war against bad livecoding.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9857
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: So far not a fan...

Post by FourthWorld » Fri Jul 31, 2015 5:25 pm

MaxV wrote:Please, don't use global variables in your code, here the reason:

http://newsletters.livecode.com/may/iss ... etter3.php




:evil: :evil: :evil: :evil: :evil: :evil: :evil: :evil: :evil:
It's my war against bad livecoding.
And yet LiveCode supports them, as do most of the world's great languages.

An English teacher I once had used to enjoy the irony of telling us, "Never use absolutes". :)

A more balanced recommendation might be:

Please use global variables mindfully - here's the reason:

Global variables are supported in most programming languages because they serve a uniquely valuable role. Yes, it's possible to abuse them, but abuse of language features is not limited to global variables. Any language feature should be considered in light of the task at hand to determine the best fit for the problem being solved.

First, it's important to note that Mr. Kenyon's article isn't titled "Never Use Global Variables", but merely "Avoiding Global Variables". The difference is important, as he never recommends LiveCode stop supporting them, nor has any engineer who's ever worked on the code base.

The four reasons he outlines there why globals may sometimes be problematic are good things to keep in mind, but with all due respect to the author his recommended alternatives are not without tradeoffs of their own:

Use Local Variables Instead: This is probably the best alternative, but requires a bit more code and a few extra clock cycles to execute. After all, locals are, well, local, so the only way to make their values available to handlers residing in other scripts is to write accessors for them, simple commands and functions to get and set their values. The execution overhead of calling a separate function or command is low enough that accessors are usually a very good alternative in those cases where globals may be problematic.

That said, if one has developed the discipline to write accessors as a matter of habit that same discpline lends itself equally well to using globals smartly in ways that avoid the problems outlined in the article to begin with. So while I often do this myself I see it almost as a wash within my own code; accessors don't really begin to shine as an unquestionable advantage until you're writing code designed to be used by others.


Use Custom Properties: This is so frequently recommended as an alternative, and so much less useful, even problematic, when compared to accessors or even just using globals directly, that it really merits very careful consideration.

Custom properties are very useful for binding data with a specific object, and esp. useful when persistence is needed. But for data unrelated to any specific object they require much more cumbersome syntax to use:

put the uCustomPropName of btn 1 of stack "MyArbitraryValueStore" into fld 1

vs.

put gSomeVal into fld 1

The persistence inherent with custom properties can be either good or bad, depending on the needs of the problem at hand. When used in a temporary stack that will only reside in memory and never be saved persistence is never in question. But when used in any stack that will be saved, you'll want to carefully consider whether the values it has when you save are the values you'll want next time the stack is opened, and if not you'll need to write additional code to establish the correct values on init. Not back-breaking, but once again anyone with the discipline to do that faithfully probably isn't having a problem with globals either.


Use Virtual Custom Properties: getProp and setProp are two of the most exciting features in the language, but their current implementation makes them almost unusable in a great many cases where you may not have complete control over message locking.

Calling them requires the same relatively verbose syntax used for working with custom properties directly, but with the added advantage of being able to overload or override what happens when those properties are accessed.

And like custom properties, they're ideal in code where there's a logical relationship between the value and the object the value resides in.

But there's a serious drawback to relying on those triggers: currently they're among the system messages blocked with lockMessages, so if any script has set the lockMessages to true at anytime during the sequence in which your code may need those triggers, they simply won't fire.

This introduces a scope management requirement not all that different from just using a global: everyone who writes any code used in an app that depends on getProp or setProp must at all times remain aware of when those triggers are needed, and make sure not to lock messages when they are.

Fortunately, in another thread here Mark Waddingham (for newcomers, the lead engineer for LiveCode) has expressed his opinion that getProp and setProp should be recategorized from system messages to user-defined messages, which would make them immune to any effects of the state of lockMessages.

But until that change is implemented (recommended in RQCC #226), getProp and setProp are exciting options but ones which require at least as much discpline as using globals well.


TL/DR:

Yes, globals need to be used mindfully to be used well, but so do most other language features. When used well, they provide a simplicity and performance that's hard to beat. Use them sparingly, but enjoy the freedom that LiveCode offers to choose them when they're a good fit for what you're doing.

There has never been a plan to remove them from LiveCode or any of the other hundreds of languages that offer them, and as far as I know they'll remain an option we can choose to use or not as we like in all engine versions going forward.

After all, the author of that article was a member of the LiveCode IDE team, and the LiveCode IDE uses more than two dozen globals. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9751
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: So far not a fan...

Post by dunbarx » Fri Jul 31, 2015 11:45 pm

I had the feeling from early on that this OP would not get LC and would abandon it; apparently he has.

I have always tried to figure out why certain aspects of x-talks seem just not to fit either the personalities of new users, or the expectations of experienced users of other languages.

I simply assume that if any of the helpers here spent an hour with such people, one on one, they would have to say, "Oh, I see what it is all about now. Cool. Let me at it"

But I first learned the term "fanboy" only a couple of years ago. Anyone remember amthonyBlack? He taught it to me. I would be distressed, though, if as Jacque pointed out, he was turned off by a buggy v7.x, and not by LiveCode per se.

Craig
Last edited by dunbarx on Wed Mar 08, 2017 9:55 pm, edited 1 time in total.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: So far not a fan...

Post by Simon » Sat Aug 01, 2015 12:12 am

amthonyBlack
Oh I do!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9857
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: So far not a fan...

Post by FourthWorld » Sat Aug 01, 2015 12:29 am

dunbarx wrote:I had the feeling from early on that this OP would not get LC and would abandon it; apparently he has.
He may be back. Or not. There are enough programming languages that we all have plenty of opportunities to find one that fits our tastes well.

Although he chose a confusing name for a thread about an actual usage question, at least he had an actual question. It was difficult for him at first because he'd had experience with other language that work very differently, but he hung out long enough to get good info on the global thang, and hopefully he'll find an answer to the seeming corruption problem (though I think Jacque's hunch is probably spot-on given how rare true file corruption is, that he just had a version mismatch between v6 and v7). Whatever his background I'd wager it's not in Python or he would already have been accustomed to deep changes between versions. ;)

As much as I enjoy LiveCode, I have no illusions that it will replace all other languages. Many different jobs to do, many different personalities to do them, and enough options for everyone to have exactly what they want.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9751
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: So far not a fan...

Post by dunbarx » Sat Aug 01, 2015 1:41 pm

I am a fanboy. I do not see the need for any other language at all.

Along the lines of an old British mindset, certainly apocryphal, that when colonizing officers would first confromt natives, wherever in the world they found them, they assumed that if they spoke slowly and clearly in English, there was no reason that those natives should not undertand them, and if they did not, they were simply being recalcitrant.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7258
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: So far not a fan...

Post by jacque » Sat Aug 01, 2015 4:56 pm

I was given a stack recently to "fix" that was written by someone who had never used LiveCode before. I was supposed to add two buttons to it. It took me 10 hours. Nine of those were to figure out the scripts, and then only about 45 minutes to integrate the new functionality.

The author didn't know how to pass parameters so he used globals for everything. In this 4 card stack there were 47 globals. I hit every one of the problems Oliver describes in his article. I spent most of those hours replacing globals with parameters or script locals. I wrote a couple of accessor handlers for others. I got it down to just under 20 globals when the difficulty of tracking down all the repercussions got to me and I gave up. I settled for changing only those that impacted the changes I needed to make.

Oliver had it exactly right. Globals work just fine until you hand your stack over to someone else. Or until you come back to it yourself a year later.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: So far not a fan...

Post by sritcp » Sat Aug 01, 2015 9:05 pm

Aside:
How does one access old newsletters?
The old link to the portal doesn't work anymore.

Regards,
Sri

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9857
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: So far not a fan...

Post by FourthWorld » Sun Aug 02, 2015 1:10 am

sritcp wrote:Aside:
How does one access old newsletters?
The old link to the portal doesn't work anymore.Sri
Apparently it takes a meeting with their lead Web developer, which I have scheduled for Thursday. :) Making the older newsletters more easily findable is already in item #2 on my agenda. They've been migrating CMSes, and that's been at the root of a lot of the frustration users have reported finding things. Hopefully as we go through the checklist we'll be able to ensure all the important items get sorted out well.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”