Random message pops up but within constraints

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

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Random message pops up but within constraints

Post by snappy » Tue Oct 08, 2013 10:12 am

Hi, I'm a newbie to Livecode and programming in general.

I have a question but I'm not even sure where to look to try and learn how to do this.

What I'd like to do is:

1) Have a message appear at random intervals.
2) I'd like to have the following parameters available to the user:
a) Allow the user to set a minimum number of times the message can appear in that one hour period...
b) And also the maximum number of times the message can appear in that same one hour period.

I'm ready for the homework and learning. I'm just not sure where to begin.

Thanks!
snappy

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Random message pops up but within constraints

Post by dunbarx » Tue Oct 08, 2013 2:06 pm

Snappy.

Welcome to the most fun you will ever have. There is no substitute for practice and experimentation, so you are doing this right. Ask questions often.

I hope you are already at a level where you can create objects and write simple handlers. This should get you started. In a new stack, with a single button and a single field, place this in the button script:

Code: Select all

on mouseUp
   displayMessage
end mouseUp

on displayMessage
   if the optionKey is down then --press and hold this to get out of the process
      answer "End of sequence"
      exit to top
   end if
   put "This is your new random number:" && random(99) into fld 1
   wait random(4) seconds
   displayMessage
end displayMessage
Please try this, and start to modify it as you see fit. Some homework:

1- Can you make this into a single "mouseUp" handler? Just for kicks, but it is more robust as two.
2- Can you use the "send in time" construct instead of the "wait" construct? This is really much better, and only a little more advanced.
3- Once you have done all that, can you then start to keep track of the number of times the display changes? You may want to use a custom property to store that data.
4- You will want to set a start time. Where should this go, the "mouseUp" handler or the "displayMessage" handler?

Get going.

Craig Newman

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Wed Oct 09, 2013 10:05 am

Fantastic, Craig!

Thank you so much for the information.

I'm working on it right now...!

-Sean

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Wed Oct 09, 2013 10:41 am

Okay, I did the first instructions successfully. Before I go into the homework projects, can I bounce this off of you just to make sure I'm clear on what everything's doing? (and the "999" is me goofing around with the original "99" number)
Attachments
Screen Shot 2013-10-09 at 2.16.08 AM.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Random message pops up but within constraints

Post by dunbarx » Wed Oct 09, 2013 1:58 pm

Hi.

Your analysis is correct. And the random number message is just that, a random message.

If you set a breakpoint and step into each line (Do you know haw to do that?), you will see that the line "displayMessage" is nothing more than a call to a message with that name. In this case, it calls itself. The message is always sent first to the object containing the running handler, in this case the button itself, where it is trapped.

That handler could be placed higher up in the message path, but that is another story. You will need to deal with that soon, however. This is one way LC manages this, simply to state the name of a message (there are others). You might try writing another mouseUp handler that sends another message to the card, say, just for practice.

So the setup is a recursive one, and the only way out of it is with the option key, as you surmised.

I am still not sure where you are with the basics. This is a nice project, but it assumes novice (not rank beginner) capabilities. Your enthusiasm is most important, and many here talk about a minimum amount of effort to achieve a "eureka" moment, when the structure and methodology of LiveCode hits home. This is, perhaps, 100 hours. You already have a bunch, and your energy is, as I mentioned, the best tool you have.

Please look up the "wait" command, especially its "with messages" variant. Look up the "send" command, especially its "in time" variant (a homework topic). This is important since you may want to allow the user access to the program while it is running. As it stands, the process is blocking. Write back often. You will be welcomed always with your questions and complaints.

Craig

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Thu Oct 10, 2013 12:48 pm

Thanks, Craig.

I was able to attach the handler to the card so that the button passes it to the card. It worked successfully. I'm starting to understand the nesting idea, too, of the instructions.

I appreciate the 100 hour description. That's very do-able. Being new (probably closer to the rank amateur title than the novice), that gives me a general sense of the investment of time it'll take before it'll start to click.

I'm starting to formulate how this might go looking at your homework and with the commands you mention. I'm going to tackle more of it later today and see if I can get a little further.

Sean

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Fri Oct 11, 2013 9:54 am

Hi, again.

Well, I'm stuck.

I'm not understanding how to implement anything. I've been Googling and finding related subjects but I can't seem to back-engineer the logic to what I'm trying to do.

I was trying to do the "send in time" command rather than "wait". I'm not having much luck and I don't know why.

Here's what I've tried:

Code: Select all

on mouseUp
   displayMessage
end mouseUp

on displayMessage
   if the optionKey is down then --press and hold this to get out of the process
      answer "End of sequence"
      exit to top
   end if
   send "This is your random number:" && random(99) to fld 1 in random(5) seconds
   displayMessage
end displayMessage
It's all still pretty opaque. I understand the first exercise and can break it down but I can't figure out how to experiment inside it and get anything to work.

Hmmm....

Sean

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

Re: Random message pops up but within constraints

Post by SparkOut » Fri Oct 11, 2013 10:45 am

Hi Sean
Your displayMessage handler is a, well, handler. Events move through the message path until they meet a handler which deals with it (and may then be passed on, or trapped at that point). When you click a button, several events are generated, but the most typical to capture for action is the mouseUp message.
In this case your mouseUp handler generates one displayMessage event which is caught immediately as it is in the same script as the mouseUp. Each time the displayMessage handler is invoked, it should display one random message. So just use the line

Code: Select all

put "Here is your number:" && random(99) into field 1
So if we have generated just one displayMessage event with our click, how can we cycle it? We send the same event to the handler again to catch itself. So (as long as you don't end the cycle with your escape check) each time the handler runs, it calls itself just one more time

Code: Select all

send "displayMessage" to me in random(5) seconds
I hope that helps, I'd answer with a bit more example, but I'm using mobile phone connection on the train atm.

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Sat Oct 12, 2013 12:39 am

Hi SparkOut,

Thanks a lot for your reply. I think I got it.

With the "send" command, you used send ___ to ME... In this case, what defines "me"? Versus, say, send to an object? What does "me" mean exactly? It sounds super-obvious. Maybe I'm over-thinking it. It just sounds really general.

-Sean

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Random message pops up but within constraints

Post by dunbarx » Sat Oct 12, 2013 1:11 am

Snappy.

Ah. So we are really new. And really eager. Good.

LC is an environment, as Sparkout said, where messages are generated and trapped. Pretty much period. These messages have origins and targets, that is, they come from somewhere and go somewhere. The keyword, "me" is a reference to the object that originates a message. So if you have a button that you click on, the system generates a "mouseUp" message when you click on it, it knows that the button was the originating object. That object becomes "me".

Conversely, the recipient of a message is the "target".

Pretty conversational, eh? In 1987, when this was all developed, another quaint keyword, "it" was created, and you will run into this right away. It is a local variable that is the default container for the results of many common processes. So you can "put it into field 1", and this is even more conversational. But it is nonetheless a formally defined syntactical statement. It means something, and must be just so.

But all this leads me to ask you the following: do you know what (conceptually in the LC context) a variable is? A message? A container? These are basic language elements without which you cannot advance. Or at least cannot communicate with us, and ultimately, with yourself.

This is only to remind you that you still have a bit of learning to do. Please keep at it, and do not hesitate to ask questions as you go, even if these occur several times a day, or literally line by line in your handler composition. There are several people lurking here who are racing to be the first to answer you. You will learn to tolerate that.

Craig

Edit. Think about what kinds of messages there are. "MouseUp" is self-explanatory, and simple to understand. It is a system message, something LC generates as a natural consequence of your clicking thing. When I gave you "displayMessage", that was a custom message made up by me. And therein lies a glimpse of the power yet to come...

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Sat Oct 12, 2013 1:32 am

Wow. This forum and you guys are fantastic. Thanks, Craig. And, yes... I'm the very definition of "noob".

Thank your for the descriptions of "me", "target", and "it". I believe I understand what they are now. And I get that they are very specific now.

Okay, testing my knowledge:

"variable" - I'm guessing it's a predefined number (or color, word, etc.) or one chosen "on the fly" that can then be re-purposed throughout a program.

"message" - A series of instructions that, when combined, define the behavior of the message. The message can then be called-on throughout a program. They can be defined once and then reused whenever needed. In the example from above, the displayMessage we defined with an if/then statement, a put statement (using a random variable), a wait statement (using a random variable), and a loop statement to start back at the top of the message.

"container" - This one I'm not so sure about. Is a container like an html wrapper? For example: is on mouseUp <---> end mouseUp a container? Is that what's meant when you say "trapped"? Like something is trapped inside the container?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Random message pops up but within constraints

Post by dunbarx » Sat Oct 12, 2013 2:18 am

Snappy.

You have to think more concretely, that is, think more that the elements of LC are physical objects, expressed in software. This is very helpful.

A "container" is an object that can hold data. A field, a button or a variable are containers.

A variable is created on the fly, usually, at the time it is loaded:

put "25" into yourFirstVariable.

There are a handful of different variable types. Play around with them.

A message is always a single word: "mouseUp" or "displayMessage". Soon you will add parameters to these, enhancing their power mightily. In a button:

Code: Select all

on mouseup
  send "myFirstMessage" && random(999) to this card
end mouseup
In the card script:

Code: Select all

on myFirstMessage myPassedParameter
  answer myPassedParameter
end myFirstMessage
Click on the button until you get bored.

Homework: Change those handlers so that the random number is generated in the "myFirstMessage" handler, not in the "mouseUp" handler.

Have you noticed that I like to test with random numbers? Did I mention your enthusiasm is gratifying? Oh, oh, and you have much to do?

Craig

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Mon Oct 14, 2013 7:12 am

Thanks, Craig. Got it re: Thinking more concretely. Which is good because that's more of the way I tend to think of stuff. I'm a visual learner mostly.

Okay, my enthusiasm is definitely alive and well. But so is my ignorance! I'm learning... At the risk of taking this... sooooo... slowly... I have a few questions:

"container" - an object like a field or button that holds stuff. Got it.

"variable" - created on the fly usually at the time it's loaded. Got it.

A question about the name of a variable: Is it usually like "somethingSomethingVariable"?

And you say play around with different variables types. I'm not sure what that means. What kind of different variables? How do I play around with them?

"message" - always a single word that I can add parameters to. Got it.

In the case of your example, I plugged in both the mouse message and the card script and tested it. I saw the "answer" style pop up box but I didn't see the random number anywhere.

And myPassedParameter is an example of passing something from a container to a card?

Yes... Yes... Much to do!

-Sean

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

Re: Random message pops up but within constraints

Post by SparkOut » Mon Oct 14, 2013 8:16 am

Naming conventions are a subjective matter. You can name a variable whatever you like, subject to certain restrictions (it must not be the same as a LiveCode reserved word, it must begin with a letter, and may only contain a combination of letters, digits and underscore).
Look at section 5.10 in the User Guide, Tips for Writing Good Code, and http://www.fourthworld.com/embassy/arti ... tml#Naming (read that whole article). It's not critical that you do the same as everyone else, but you will find that many of us follow the same sort of guidelines, because it makes sense, and you will get used to seeing it in most code snippets you find on these forums.

Different variable types are "local", "script local" and "global". This means they have a different "scope" - in other words the value may be referenced only in the local handler, or in any handler or function of the script for the current object, or globally.

A "message" is not "always a single word you can add parameters to". A "message" is an internal reference to an event which is triggered. The message is handled when it meets a script section of the same name in the message path. The script section is called a handler and is usually indicated with the word "on" - ie, on mouseUp - begins a handler script to deal with the mouseUp message which is generated when you let go of the mouse button. (see section 5.2 and 5.3 in the User Guide)
Last edited by SparkOut on Mon Oct 14, 2013 8:28 am, edited 3 times in total.

snappy
Posts: 19
Joined: Tue Oct 08, 2013 10:02 am

Re: Random message pops up but within constraints

Post by snappy » Mon Oct 14, 2013 8:21 am

Thanks, SparkOut. I'm reading the link right now...

Post Reply