Get value of function with variable
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Get value of function with variable
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?
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?
Re: Get value of function with variable
Crashing LiveCode is not a Good Thing.This doesn’t, and crashes LiveCode:
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Get value of function with variable
Odysseus.
Neither of these lines:
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
Neither of these lines:
Code: Select all
get value(“myFunction(myVar)”, card “xx”)
get value(“myFunction(1)”, card “xx”)
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
Re: Get value of function with variable
Hi Craig,
well, sometimes LC just crashes for no apparent reason!
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
well, sometimes LC just crashes for no apparent reason!

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
Re: Get value of function with variable
Remove the quotation marks around the function call. If it's in quotes LC thinks it's a literal value.odysseus wrote: ↑Sun Sep 13, 2020 12:07 amI 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?
Code: Select all
get value(myFunction(myVar), card “xx”)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Get value of function with variable
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Get value of function with variable
Famous last words!shouldn't make a difference

Re: Get value of function with variable
LOL.
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.
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?

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.

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?
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Get value of function with variable
Hi, try:
Have fun!
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"
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!
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!
Re: Get value of function with variable
Jacque.
I never knew that extra parameter associated with the "value" function.
Craig
I never knew that extra parameter associated with the "value" function.
Craig
Re: Get value of function with variable
Hi,
!
!!
!!!
Fascinating how much it's still possible to learn, just by browsing the forum, after all these years.
Have fun!
Same here. Read it, thought "Huh?" Checked dictionary =>



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!
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!
Re: Get value of function with variable
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"
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"

Re: Get value of function with variable
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.
Kind regards
Bernd
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
Bernd
Re: Get value of function with variable
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
HyperActive Software | http://www.hyperactivesw.com
Re: Get value of function with variable
Is there an advantage there to using "value" instead of dispatching the function to the card?
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev