A second answer window?

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
-Tommy-
Posts: 9
Joined: Thu Dec 05, 2013 6:40 pm

A second answer window?

Post by -Tommy- » Thu Dec 05, 2013 6:50 pm

How's it going forum? I'm having a bit of trouble with the 'answer' prompt, and I'd like to know if anybody would be able to help me, regardless of whether or not this is the place to ask. (Personally, I think it is, seeing as I'm completely new to live code)

Anyway, for my first assignment in school, I decided to use the code of the Shakespeare insult machine to develop a name generator of sorts.
The coding is sound, the dictionary for the first and last name is packed, and I can successfully run the prompt by clicking a button, so I'm rather proud of how it works so far.

I do have a slight problem, however. After I click the button, which runs the card script, I get the answer window I'm looking for, complete with the randomized name I was looking for. But after closing that window, a second answer window opens up, this one totally blank, save for a button. It's not a huge issue, but it's kind of annoying and I'm unsure why it keeps appearing.

So what I guess I'm asking is "Why is this second window showing up, and how do I fix it?"

I'm running on a deadline, and I'd like to solve this as soon as possible, but I'm happy to help clarify the situation with images later on, if necessary.

Thanks so much in advance!
Image

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

Re: A second answer window?

Post by dunbarx » Thu Dec 05, 2013 6:57 pm

Hi.

Another "answer" command was written somewhere. Can you find it? Can you step through the handlers until that second one appears? Otherwise, you need to post your scripts.

Craig Newman

-Tommy-
Posts: 9
Joined: Thu Dec 05, 2013 6:40 pm

Re: A second answer window?

Post by -Tommy- » Thu Dec 05, 2013 7:07 pm

dunbarx wrote:Hi.

Another "answer" command was written somewhere. Can you find it? Can you step through the handlers until that second one appears? Otherwise, you need to post your scripts.

Craig Newman
As far as I can tell, there is only one "answer" command, and it's situated on a card script, just like the "Shakespeareinsult()"

I can post the code here, if it helps.

This is the card script:

function ponynamer
answer "Hi! My name is" \
&& any word of field "one"\
&& any word of field "two" & "!"
end ponynamer

And this is the code for the button

on mouseUp
answer ponynamer()
end mouseUp

(PS, yes, this code is for naming ponies. I doubt that's the problem, though :roll: )
Image

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: A second answer window?

Post by Klaus » Thu Dec 05, 2013 7:23 pm

Hi Tommy.

1. welcome to the forum! :D

2. Looking at your script, this (2 dialogs) is correct behavior! :D
This is a FUNCTION, yet does not RETURN anything, BUT this will display the FIRST ANSWER dialog!

Code: Select all

function ponynamer
  answer "Hi! My name is" &&  any word of field "one" && any word of field "two" & "!"
end ponynamer
This will ANSWER (the second ANSWER dialog!) the non-existing RESULT of the above mentioned function and that is EMPTY 8)

Code: Select all

on mouseUp
  answer ponynamer()
end mouseUp
To solve this problem do this:
1. Make "ponynamer" a handler:

Code: Select all

COMMAND ponynamer
    answer "Hi! My name is" && any word of field "one" && any word of field "two" & "!"
end ponynamer
Then simply call that handler

Code: Select all

on mouseUp
    ponynamer
end mouseUp
Best

Klaus

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

Re: A second answer window?

Post by dunbarx » Thu Dec 05, 2013 7:37 pm

Hi.

Couple of things.

NEVER name a field with a reserved word. Like "1" or "one". Trust me. OR rather, keep doing so, and you will learn a great deal about how LC parses object references.

Sorry to be so abrupt. Tough love.

You are calling a function, but using that function as a command handler. Functions should be used to return a value, using the "return" command.

Try this:

Code: Select all

on mouseUp
  ponynamer
end mouseUp

on ponynamer
answer "Hi! My name is" \
&& any word of field "one"\
&& any word of field "two" & "!"
end ponynamer
Couple of more things. Though there is nothing wrong with doing so, why call a separate handler at all? Can you rewrite your original handlers to actually use a function call correctly? This would be a good exercise.

Craig Newman

-Tommy-
Posts: 9
Joined: Thu Dec 05, 2013 6:40 pm

Re: A second answer window?

Post by -Tommy- » Thu Dec 05, 2013 8:53 pm

dunbarx wrote:Hi.

Couple of things.

NEVER name a field with a reserved word. Like "1" or "one". Trust me. OR rather, keep doing so, and you will learn a great deal about how LC parses object references.

Sorry to be so abrupt. Tough love.

You are calling a function, but using that function as a command handler. Functions should be used to return a value, using the "return" command.

Try this:

Code: Select all

on mouseUp
  ponynamer
end mouseUp

on ponynamer
answer "Hi! My name is" \
&& any word of field "one"\
&& any word of field "two" & "!"
end ponynamer
Couple of more things. Though there is nothing wrong with doing so, why call a separate handler at all? Can you rewrite your original handlers to actually use a function call correctly? This would be a good exercise.

Craig Newman
Thanks for the advice (and I understand why I shouldn't name it something as obscure as "one". Going to change it to something else) The second solution did, in fact, work, and the program runs fine now.

I don't completely understand what you mean by calling a separate handler, seeing as my knowledge of code goes about as far as basic html code, meaning that this project is totally new territory to me.

So correct me if I'm wrong, but what you're asking of me is that I write the command on the card script into the button's handler?

If so, then my command would look like this, right?

Code: Select all

on mouseUp
answer "Hi! My name is" \
         && any word of field "one"\
   && any word of field "two"  & "!"
    
end mouseUp
 
If that's the case, I'll gladly do it, even if it's just a matter of simplifying the code.
Image

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: A second answer window?

Post by Klaus » Thu Dec 05, 2013 9:51 pm

Hi Tommy,
if so, then my command would look like this, right?

Code: Select all

on mouseUp
   answer "Hi! My name is" \
         && any word of field "one"\
         && any word of field "two"  & "!"  
end mouseUp
Yes :D

"Outsourcing" handlers makes sense if you need to call it from more that one specific object,
like if you would need more than one button on the card that "names the pony".

In that case you would place the "ponynamer" handler into the card script and can
have multiple buttons or other object with the script:

Code: Select all

on mouseup
  ponynamer
end mouseup
You get the picture :D


Best

Klaus

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

Re: A second answer window?

Post by dunbarx » Fri Dec 06, 2013 12:07 am

Tom.

Another reason to place a section of code in another handler is to condense your main line. I suggested that you did not need it in this case because the whole handler was only a few lines, and separating it into two actually increases the total. There are times when you might have several pages of handlers in a single script. If you can pack, say a well-defined section somewhere else, say the stack script, or even just to the bottom of the working script, it might make for easier reading:

Code: Select all

on mouseUp
answer "Hi! My name is" \
         && any word of field "one"\
   && any word of field "two"  & "!"
    
put someLongAndTediousCalculation(existence) into lifeTheUniverseAndEverything

answer lifeTheUniverseAndEverything
end mouseUp

function someLongAndTediousCalculation var
doLotsOfStuff
return 42
end someLongAndTediousCalculation
Craig

-Tommy-
Posts: 9
Joined: Thu Dec 05, 2013 6:40 pm

Re: A second answer window?

Post by -Tommy- » Mon Dec 09, 2013 10:03 pm

Okay, thanks a bunch guys! I finished the code, and I'm pretty impressed with the results.

Thanks so much for the help, it really means a lot to me.

If you ever feel like creating a [ponysona, I'll gladly use it to generate a name or two for ya! :mrgreen:
Image

Post Reply