Return going to message box, not caller

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
dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Return going to message box, not caller

Post by dpatterson » Sat Jan 28, 2017 8:32 pm

Okay. Color me confused. I have a function that is displaying its return value in the message box instead of returning it to the caller.
Calling code:

Code: Select all

#
# Adds the specified text and a return to the log field.
#
command log msg
  local lineCount
  local lineHeight
  local timestamp
  local newLine = "<br>"
  
  if the hilited of button "enabledBtn" of card "log" of me is not true then exit log
  if msg is an array then
    put formatArray( msg ) into msg    -- Format the array and replace the contents of msg with the result.
  end if
  -- At this point msg should always be a string
  -- However, if formatArray() was called it's still an array :-/
  if the hilited of button "timestampsBtn" of card "log" of me is true and msg is not empty then
    put "[" & the long time & "] " into timestamp
    put "             " after newLine
  end if
  lock screen
  get the htmlText of field "output" of me
  put ( "<p>" & timestamp & replaceText( msg, cr, newLine ) & "</p>" ) after it
  set the htmlText of field "output" of me to it
  put the effective textHeight of field "output" of card "log" of me into lineHeight
  put the number of lines in field "output" of card "log" of me into lineCount
  Set The VScroll Of field "output" of card "log" of me to lineHeight * lineCount
  unlock screen
end log
The formatArray() function:

Code: Select all

#
# Formats an array for logging.
# Based on http://lessons.livecode.com/s/lessons/m/4071/l/21022-how-do-i-display-an-array-in-human-readable-form
#
function formatArray theArray, indent 
  local idx                -- The array key
  local ret
  
  #
  # If the supplied value is not an aray then just return it as is.
  #
  if theArray is not an array then return theArray
  
  return "it's an array"
  get "array" & cr  -- into "it"
  put "  " after indent
  repeat for each key idx in theArray
    put ( format( "%s[%s] => %s", indent, idx, formatArray ( theArray[idx], indent )) & cr ) after it
  end repeat
  delete the last character of it
  put it into ret
  return ret
end formatArray
I have stepped through this with the debugger and everything looks right at the point of the return.
At that point, ret contains a nicely formatted string.
When I step back to the calling handler, msg still contains the original array.
Strangely, the formatted output is displayed in the message box.
One other thing, I am calling log from the message box.

I must be missing something basic here.
Thanks in advance.
Dave

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Return going to message box, not caller

Post by bn » Sat Jan 28, 2017 9:16 pm

Hi Dave,

this line

Code: Select all

put formatArray( msg ) into msg
means that the result of formatArray should go the message box since msg is shorthand for message box.

change "msg" to tMsg or somesuch and the problem should go away.

Kind regards
Bernd

dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Re: Return going to message box, not caller

Post by dpatterson » Sat Jan 28, 2017 10:00 pm

Bernd,

Doh. Of course. Variable name overload. Oops! :oops:
Thanks very much for pointing that out.

I'm still getting used to all of the LiveCode reserved words (and I have a tendency to use their long forms in my source). :D

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Return going to message box, not caller

Post by bn » Sat Jan 28, 2017 10:22 pm

Dave,

I for exactly this reason not to use accidentally I prepend my variables with handler scope with "t"

Script local variables (those that are declared outside of any handler and that are accessible by any handler below the declaration I prepend with "s"

Global variables (I never use them because any stack or substack can access those and then you don't know why your global "myStandardGloball" does not do what you want) I prepend with "g"

Custom properties with "u" or "c"

You might want to have a look at
http://www.fourthworld.com/embassy/arti ... style.html

Richard Gaskin discusses these and other style questions. Many Livecoder follow his recommendations.

Kind regards

Bernd

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”