passing arrays to scripts in other objects

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
vanderpump
Posts: 6
Joined: Sun Sep 28, 2008 4:20 pm
Contact:

passing arrays to scripts in other objects

Post by vanderpump » Mon Sep 29, 2008 11:14 pm

Hi
encouraged by the success of my previous posting I have here another question to ask. This is a question to which today I have spent a good couple of hours seeking an answer in the documentation and by trial and error.

The problem concerns passing an array over to a method in the script of an object other than that of the sender.

I have a Scrolling Field: resultsField with the following script:

Code: Select all

on printAry pAry
   printValue "hi from resultsField"
   repeat for each element tElem in  pAry
      printValue tElem
   end repeat
end printAry

on printValue pValue
      get the number of lines in me
      put pValue into line it + 1 of me
end printValue
I have two buttons: passArrayInternally and passArrayExternally.
The script of passArrayInternally is as follows

Code: Select all

on mouseUp
   repeat with tvar = 0 to 5
      put tvar into tAry[tvar]
   end repeat
   localPrintAry tAry
end mouseUp

on localPrintAry pAry
   repeat for each element tElem in  pAry
      send printValue && tElem to card field "resultsField"
   end repeat
end localPrintAry
The code of this button works perfectly.
The array is passed and the messages to display each element are sent to the resultsField successfully.

Here is the script of passArrayExternally

Code: Select all

on mouseUp
   repeat with tvar = 0 to 5
      put tvar into tAry[tvar]
   end repeat
   send printAry && tAry to card field "resultsField"
end mouseUp
This executes ok as is shown by the fact that the string "hi from resultsField" is printed but the array is not printed.

I have found documentation that states that messages to custom methods use a different syntax than those to system methods but not documentation that illustrates how this is to be done for messages to scripts of other objects.

Is it possible that as in the case of calling functions in the scripts of other objects (e.g. http://forums.runrev.com/phpBB2/viewtopic.php?t=2128)
so here too it is necessary to employ a different approach?


Thanks
Julius

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

Post by SparkOut » Tue Sep 30, 2008 12:14 am

Hi Julius,

First question is what version of Rev are you using?
in [url]http://www.runrev.com/newsletter/september/issue57/newsletter1.php?id=NW57S445981[/url], Tom Healy wrote:In version 3.0 the power of arrays has really been unlocked for the first time in Revolution. Previously, once the contents of the arrays were stored, they had to remain within their local scope ie. if an array was a command local it could not be passed as a parameter, or sent to any other function or command for use elsewhere. This was a considerable constraint upon their use. Now however, arrays can not only be passed as single parameters to other commands/functions but can also be stored as custom properties of objects...
...This is a simple example but the potential for the use of arrays is dramatically improved now that they can be passed around your programs as simply as variables.

vanderpump
Posts: 6
Joined: Sun Sep 28, 2008 4:20 pm
Contact:

Post by vanderpump » Tue Sep 30, 2008 9:00 am

SparkOut hi,
yes, I should have said.
I'm running Revolutions Media 3.0 on a mac pro leopard 10.5.5.

thanks
Julius

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

Post by SparkOut » Tue Sep 30, 2008 3:56 pm

Ah, it seems to be just a syntax issue.
under the entry for [i]send[/i], Rev docs wrote:If the message is a literal string containing more than one word (for example, if it includes parameters, or if it is a multi-word command), it must be enclosed in double quotes.

Any parameters are evaluated before they are passed to the send command. For example, the following statement sends the mouseUp message with 2 as the first parameter:

send "mouseUp 1+1" to button "Example"
So:

Code: Select all

on mouseUp 
repeat with tvar = 0 to 5 
put tvar into tAry[tvar] 
end repeat 
send "printAry tAry" to card field "resultsField" 
end mouseUp 
should work (it does here! - on Rev 3.0.0-gm-3 on Windows XP)

vanderpump
Posts: 6
Joined: Sun Sep 28, 2008 4:20 pm
Contact:

Post by vanderpump » Tue Sep 30, 2008 8:21 pm

SparkOut hi
yes, thankfully just syntax. Thanks.

In fact I caught sight last night of the existence of the call function and combining this with what I learnt earlier about function calls figured out (hopefully correctly) that messages are passed as interpretable strings.
The rest as they say is history!!!

BTW for completeness the call looks like this

Code: Select all

call "printAry tAry" of card field "resultsField"
One moment.
I've just re-read your missive
Any parameters are evaluated before they are passed to the send command.
Ok. And this of course takes into account the contextual differences between send and call.

viz in the dictionary:
send

When the send command is used the stack containing the target handler temporarily becomes the defaultStack. All object references in the message are evaluated in the current context i.e. the defaultStack. Therefore references within the message that refer to "this card" or "this stack" will be referring to the card or stack where the target handler is located.
and
call

When executing a handler invoked by the call command the defaultStack remains the same as it was when the call command was issued. Therefore any object references in the called handler are evaluated in the context of the call command that invoked the handler. For example, button 3 may commonly refer to button 3 of the current card of the stack from which the target handler was called.


Thanks again
Best wishes
Julius[/quote]

Post Reply