Get value of function with variable

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

odysseus
Posts: 42
Joined: Tue Aug 25, 2020 2:15 pm

Get value of function with variable

Post by odysseus » Sun Sep 13, 2020 12:07 am

I need to call a function in a different card.
This works:
get value(“myFunction(1)”, card “xx”)

This doesn’t, and crashes LiveCode:
get value(“myFunction(myVar)”, card “xx”)

MyVar is previously defined as 1 for this example.

I need to use the variable to call the function with different values. Any ideas?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Get value of function with variable

Post by mwieder » Sun Sep 13, 2020 2:07 am

This doesn’t, and crashes LiveCode:
Crashing LiveCode is not a Good Thing.

But try

Code: Select all

dispatch function "myFunction" to card "xx" with myVar
if it is "handled" then
  -- put the result into someVariable
else
  --a Bad Thing happened
end if

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

Re: Get value of function with variable

Post by dunbarx » Sun Sep 13, 2020 2:52 pm

Odysseus.

Neither of these lines:

Code: Select all

get value(“myFunction(myVar)”, card “xx”)
get value(“myFunction(1)”, card “xx”)
even compile. How did you get it to run to the point of crashing LC?

Anyway, there is no way for LC to know either to navigate to or access that other card, whichever way you framed it. That is why Mark used another command to take care of that.

But how did you get it to crash?

Craig

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

Re: Get value of function with variable

Post by Klaus » Sun Sep 13, 2020 3:01 pm

Hi Craig,

well, sometimes LC just crashes for no apparent reason! :D

I just wanted to open a (working) stack on mine -> BOOM, LC crashed!
Restarted LC and opend that stack without problems.

I submitted about half a dozen crash reports to the "Quality Center" this year
and all of them appeared without any apparent reasons.


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Get value of function with variable

Post by jacque » Sun Sep 13, 2020 4:48 pm

odysseus wrote:
Sun Sep 13, 2020 12:07 am
I need to call a function in a different card.
This works:
get value(“myFunction(1)”, card “xx”)

This doesn’t, and crashes LiveCode:
get value(“myFunction(myVar)”, card “xx”)

MyVar is previously defined as 1 for this example.

I need to use the variable to call the function with different values. Any ideas?
Remove the quotation marks around the function call. If it's in quotes LC thinks it's a literal value.

Code: Select all

get value(myFunction(myVar), card “xx”)
Craig, before we had the dispatch command, this was the only way to execute a function outside the current message path. I used it frequently.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Get value of function with variable

Post by mwieder » Sun Sep 13, 2020 5:27 pm

Jacque- I haven't tried the example, but according to the docs removing the quotes shouldn't make a difference
If the expression is a single string, then even if it is enclosed in quotes, LiveCode attempts to evaluate its contents instead of treating it as a string literal.

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

Re: Get value of function with variable

Post by Klaus » Sun Sep 13, 2020 5:45 pm

shouldn't make a difference
Famous last words! :-D

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Get value of function with variable

Post by mwieder » Sun Sep 13, 2020 6:55 pm

LOL. :oops:

OK - I actually tried it this time.
With the enclosing quotes the *name* of the variable is passed to the function, instead of the variable contents.
Without the enclosing quotes you get a runtime error because LC tries to evaluate "myFunction" in the current context.

So no surprise to find that the docs are wrong. :roll:
I don't see a way to do this successfully with the "value" function, but I don't remember how many years it's been since I tried to use it.
I'm a bit hard-pressed at the moment to come up with a situation where I'd need it.

Maybe the docs should say it's deprecated for new code?

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Get value of function with variable

Post by AxWald » Sun Sep 13, 2020 8:14 pm

Hi, try:

Code: Select all

--  On cd 2, btn "function_btn":
function test what
   return "Test: " & what
end test

--  On cd 1, button "test_btn":
on mouseUp
   put "passed" into myVar
   put value("test(" & myVar & ")", btn "function_btn" of cd 2)
end mouseUp

--  Hit "test_btn", see in msg:
"Test: passed"
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

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

Re: Get value of function with variable

Post by dunbarx » Mon Sep 14, 2020 4:21 am

Jacque.

I never knew that extra parameter associated with the "value" function.

Craig

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Get value of function with variable

Post by AxWald » Mon Sep 14, 2020 10:31 am

Hi,
dunbarx wrote:
Mon Sep 14, 2020 4:21 am
I never knew that extra parameter associated with the "value" function.
Same here. Read it, thought "Huh?" Checked dictionary => :idea: ! :idea: !! :idea: !!!
Fascinating how much it's still possible to learn, just by browsing the forum, after all these years.

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

odysseus
Posts: 42
Joined: Tue Aug 25, 2020 2:15 pm

Re: Get value of function with variable

Post by odysseus » Mon Sep 14, 2020 1:06 pm

Thank you AxWald, that did the trick. I had already tried putting the parameter in quotes, but adding the ampersands was the key - who would have guessed that? It would be nice if that was in the dictionary!
I have just come from Toolbook where the equivalent is: get myFunction(myVar) of page "xx" ,so much simpler, no faffing around with value, lots of quote marks, brackets etc., "keep it simple" :-)

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

Re: Get value of function with variable

Post by bn » Mon Sep 14, 2020 5:02 pm

I don't know exactly why you use "value" but would'nt this do the trick? Note you have to change the function to a command on card 2, it does not work with functions.

Code: Select all

--  On cd 2, btn "function_btn":
command test what -- note changed from function to command
   return "Test: " & what && the milliseconds
end test

--  On cd 1, button "test_btn":
on mouseUp
   put "passed" into myVar
   call "test myVar" of  btn 1 of cd 2 
   put the result
end mouseUp
Kind regards
Bernd

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Get value of function with variable

Post by jacque » Mon Sep 14, 2020 5:41 pm

mwieder wrote:
Sun Sep 13, 2020 6:55 pm
I'm a bit hard-pressed at the moment to come up with a situation where I'd need it.
I use it in one app where the stack script needs to get a value from a function in a card. The function is specific to the card and never used elsewhere except for one instance where the stack needs to retrieve it to do some calculations. Rather than move the handler to the stack script, it's cleaner and less clutter to just get the value the card already knows.

I looked at my old scripts and they do use quotes, so my memory was wrong. However, I don't think I've ever passed a parameter in the function call when using value.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Get value of function with variable

Post by mwieder » Mon Sep 14, 2020 6:42 pm

Is there an advantage there to using "value" instead of dispatching the function to the card?

Post Reply