Page 1 of 1
Referencing an object by variable
Posted: Mon Jun 25, 2012 9:16 pm
by Cool Dave
Hi everyone,
I bought the commercial version of LiveCode several years ago, and have used it as a hobby sporadically since then. However, I did not join the forums until just now. I have always found some long, slow way of doing what I wanted to do. But I am finally sick of that and so here I am. Hi!
I need to put whichever field is focused right now into a variable that I can reference later.
I basically want to say,
"put me into theChosenOne"
where me is a field
and
"set the loc of theChosenOne to 0,0"
in another script
This is so easy in Javascript that there must be a simple way to do it with livecode. I just never learned how. I have searched the dictionary for a long time, but I haven't come up with anything satisfactory. I would really prefer not to have to play around with IDs, so I hope the answer is simple.
Thanks in advance!
Dave
Re: Referencing an object by variable
Posted: Mon Jun 25, 2012 9:28 pm
by jmburnod
Hi Dave,
One way with a cd script
Code: Select all
local theChosenOne
on openfield
put the target into theChosenOne
end openfield
on moveChosenOne
set the loc of theChosenOne to 0,0
end moveChosenOne
Best regards
Jean-Marc
Re: Referencing an object by variable
Posted: Mon Jun 25, 2012 9:33 pm
by bn
Hi Dave,
just saw that Jean-Marc already posted a good way to do this. Here is another way.
welcome to the forum.
look at the focuseObject in the dictionary.
make a new stack with say 2 fields and one button
set the script of the button to
Code: Select all
on mouseUp
put the focusedObject into myVariable
if myVariable contains "field" then
-- it is a field
-- do what you want
end if
put myVariable into field 1
end mouseUp
tab from field to field and click the button. Click on the card and click the button.
now myVariable is a local variable that only exists as long as the handler is running. You could make the variable persistent by making it
a global variable -- can be used from anywhere if it is declared first
a script local variable -- also has to be declared at the top of a script and can only be accessed by handlers in that script
a custom property -- can be accessed from anywhere
all that depends on what you want to do with the 'focusedObject'
Kind regards
Bernd
Re: Referencing an object by variable
Posted: Mon Jun 25, 2012 9:46 pm
by Cool Dave
Thanks to both of you,
Both ways are handy, but still require some action like focusing the object or running the script in the object. Is there no way to replace "the target" with " field"oneChoice" "?
If it isn't possible, that's kind of pathetic in my opinion.
Thanks again.
Re: Referencing an object by variable
Posted: Mon Jun 25, 2012 10:31 pm
by dunbarx
In the card script:
on openField
set the chosenOne of this card to the target
end openField
The property will be reset every time you open a field. At any time, you can:
answer the chosenOne of this card.
There are probably a dozen other methods, perhaps using the focusIn message or the focusedObject function.
Is this what you needed? Write back if not.
Pathetic? LC? Not a chance.
Craig Newman
Re: Referencing an object by variable
Posted: Mon Jun 25, 2012 10:38 pm
by bn
Dave,
I am afraid I don't understand what exactly you are asking.
Is there no way to replace "the target" with " field"oneChoice" "?
could you give a little more context. What is the use case.
What Jean-Marc did was to use a message that is send when a focusable field gets the focus, e.g. by user action. That triggers the handler for the openField message. Since you have more than one field Jean-Marc used the "target" to identify which field it is that triggered the message. You can of course use the "short name of the target" which gives you just the name. And Jean-Marc gave a nice example how to use a script local variable: he initialized theChosenOne outside of any handler, that way it will persist as long a the stack is open and you can send "moveChosenOne" to the card to trigger the change of location.
What I did was just an example. It might be less appropriate than Jean-Marc's but shows how you can query which of the objects is currently focused. Not necessarily by pressing a button but also in another hander running.
Please explain a little more and we can see what would be the "least pathetic" way to do this in LiveCode.
Kind regards
Bernd
Re: Referencing an object by variable
Posted: Tue Jun 26, 2012 12:04 am
by Cool Dave
Thanks Bernd,
and everyone else who posted answers.
You finally made the light bulb turn on, Bernd, when you said
You can of course use the "short name of the target" which gives you just the name.
What I wanted was this:
Code: Select all
put the abbr name of field"field1" into theChosenOne
which I can easily refer to.
I don't know how I've gotten along for so long without knowing how to do this! Five times as much code sometimes, I guess.
And Craig, you're right. LC isn't pathetic. Once in a while something takes forever to learn from the Dictionary, but other than that it's super simple.
LC was the first language I learned but now I can appreciate it. Thanks again everyone for helping me learn this critical concept which I have been missing all these years.
Also for the great welcome to the forums.
Dave
Re: Referencing an object by variable
Posted: Tue Jun 26, 2012 3:25 am
by dunbarx
None taken.
LC is a vast superset of Hypercard, more modern, and much, much richer. Many people here started with that progam long ago, and not only have the advantage of time, but a head start with a simpler foundation.
It is difficult to learn the 2000-odd language elements all at once. The best way is to write many stacks The dictionary is invaluable, but it would be like trying to learn english that way, by keeping a dictionary in ones pocket, and reading it at night. The right way is to simply try to speak the language to anyone who will listen. Write code all the time. Make up tasks for yourself or solve laughably simple problems with one. Even a trivial stack will lead to vast experience, since you will embellish and expand it, and be both frustrated and triumphant along the way. This is inevitable, and a good thing.
So the next time you need a converter from Celsius to Farenheit, write a stack.
Craig Newman