Page 1 of 2
Get value of function with variable
Posted: Sun Sep 13, 2020 12:07 am
by odysseus
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?
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 2:07 am
by mwieder
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
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 2:52 pm
by dunbarx
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
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 3:01 pm
by Klaus
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
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 4:48 pm
by jacque
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.
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 5:27 pm
by mwieder
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.
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 5:45 pm
by Klaus
shouldn't make a difference
Famous last words!

Re: Get value of function with variable
Posted: Sun Sep 13, 2020 6:55 pm
by mwieder
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?
Re: Get value of function with variable
Posted: Sun Sep 13, 2020 8:14 pm
by AxWald
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!
Re: Get value of function with variable
Posted: Mon Sep 14, 2020 4:21 am
by dunbarx
Jacque.
I never knew that extra parameter associated with the "value" function.
Craig
Re: Get value of function with variable
Posted: Mon Sep 14, 2020 10:31 am
by AxWald
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 =>

!

!!

!!!
Fascinating how much it's still possible to learn, just by browsing the forum, after all these years.
Have fun!
Re: Get value of function with variable
Posted: Mon Sep 14, 2020 1:06 pm
by odysseus
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"

Re: Get value of function with variable
Posted: Mon Sep 14, 2020 5:02 pm
by bn
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
Re: Get value of function with variable
Posted: Mon Sep 14, 2020 5:41 pm
by jacque
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.
Re: Get value of function with variable
Posted: Mon Sep 14, 2020 6:42 pm
by mwieder
Is there an advantage there to using "value" instead of dispatching the function to the card?