Calling a user-written function located externally

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
Steelweaver52
Posts: 14
Joined: Fri Jan 02, 2009 6:41 am

Calling a user-written function located externally

Post by Steelweaver52 » Tue Jan 20, 2009 8:01 am

Hi, friends:

In the script of button "A", I would like to call a user-written function that resides in the script of button "B". Is it possible to do that?

I have found that the following doesn't work:

Code: Select all

put (myCustomFunction() of button "B") into myVariable
Would it work if I put myCustomFunction() into the script of the card (instead of button "B", that is), since the card is located "upstream" in the message hierarchy?


Thanks!

---Tom Nally, New Orleans

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Tue Jan 20, 2009 11:23 am

Hi Tom,
Would it work if I put myCustomFunction() into the script of the card (instead of button "B", that is), since the card is located "upstream" in the message hierarchy?
That is the way to go, put it either into the card script or the stack script, depending on if it is called only from controls of the card or from more cards than just that card. Sometimes I put all this stuff into the stack script for maintenance reasons, I find it easier when debugging, but that is a question of style, habits and circumstances.
regards
Bernd

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

Post by Klaus » Tue Jan 20, 2009 11:31 am

Hi Tom,

or you could just add the buttons script to the message hierarchy (temporarily or not):

...
insert script of btn "B" of cd X of stack Y into back
put myCustomFunction() into myVariable
remove script of btn "B" of cd X of stack Y from back
## Optional
..

Et voila!
It's magic, ladies and gentlemen :-)


Best from germany

Klaus

Steelweaver52
Posts: 14
Joined: Fri Jan 02, 2009 6:41 am

Post by Steelweaver52 » Tue Jan 20, 2009 2:53 pm

Thanks, friends.

I think that I'm going to do it in the following way.

(To review, myCustomFunction() resides in the script of button "B". I want to call this function from the script of button "A".)

In my particular application, program control need not return to button "A" after myCustomFunction() is called. Therefore, I'm going to write a mini-handler in the script of button "B" which does nothing but call myCustomFunction()

Code: Select all

on CallMyCustomFunction
    put myCustomFunction() into AAA
end CallMyCustomFunction
Then, from the script of button "A", I'm going to call this mini-handler in button "B":

Code: Select all

send CallMyCustomFunction to button "B" of this card
In subsequent projects, however, I think that I will move the functions to the card on which the buttons reside, rather than doing it as I just described.

Thanks again!

---Tom Nally, New Orleans

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

Post by Klaus » Tue Jan 20, 2009 4:25 pm

Hi Tom,

yep, whatever :-)

But please put ALWAYS QUOTES around the message you are sending:
...
send "CallMyCustomFunction" to button "B" of this card
...

It may work without, but "Murphy's Law" is always striking when you least exspect it!

You may also want to look at the the "CALL" command in the docs!


Best

Klaus

Steelweaver52
Posts: 14
Joined: Fri Jan 02, 2009 6:41 am

Post by Steelweaver52 » Tue Jan 20, 2009 6:01 pm

Klaus wrote: But please put ALWAYS QUOTES around the message you are sending:
Oh, thanks!

I didn't know that was a "best practice".

Actually, I might not adhere to that best practice with perfect fidelity. But if I fail to adhere to it, I will try to remember to acknowledge to readers that I'm neglecting a best practice, so as not to lead others astray.

For that matter, if I ever post the code of an application, it could become a case study in "How Not To Program". :wink:

Thanks again!



---Tom Nally, New Orleans

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Tue Jan 20, 2009 8:21 pm

Hi Tom,

there is an easy way to enforce best practise. Set the script editor to script compile mode. :)

You will also need to declare all variables then and quote all literals, but it will save you many many headaches while debugging.

fwiw,

Malte

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Tue Jan 20, 2009 10:56 pm

You can actually also use

Code: Select all

put valued("myCustomFunction()", button "B") into myVariable
to get the result of a function defined in the other button - which can get tricky with quotes.
But the best idea is to move it 'upstream' into the card or stack script.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply