Page 1 of 1
Referencing Objects on Other Cards
Posted: Mon Dec 16, 2013 7:07 pm
by townsend
I put a lot of my code in Buttons, which makes testing easier. Then I often need to run these from other cards.
But of course if that card is not open when I execute the Dispatch command to run the script in that button, I get an error that the field object cannot be referenced. Of course by placing, of card "card.name" after every field reference, fixes this problem.
Is there some statement I can add to the beginning of these button handlers to tell it that all field references should be aimed at a given card? So I don't have to used the of card "card.name" after every field reference.
Re: Referencing Objects on Other Cards
Posted: Mon Dec 16, 2013 7:59 pm
by dunbarx
Hi.
This is really going against the internal structure of LC. The idea is to use the message hierarchy as a power tool. It is not a good idea to create a maze of explicit pathnames for the use of "send" or "dispatch" from local objects to other local objects.
That said, I understand the convenience of testing in buttons that have, say, descriptive names for what they do. But the right way to go forward is to move those button handlers to the stack script if you can, using the "target" to find which one was pressed. In that way you can access those handlers from anywhere in the stack. Now this may be a pain if you already have built a lot of stuff. But it is worth doing, and certainly worth planning in this way for future projects. There is nothing against simply copying those button handlers to the stack. They can still remain where they are. But from another card, they will execute from the stack script.
You will probably find that moving the handlers requires nothing more than renaming them, since they likely are resident in "mouseUp" handlers in your button. But they will need to have their own handler names when they are in the stack script.
Craig Newman
Re: Referencing Objects on Other Cards
Posted: Mon Dec 16, 2013 8:22 pm
by townsend
Thanks for the answer Craig. I appreciate the pointers on efficient app design.
Basically, I've got a bunch of cards that grab some info from the internet and update some text fields. I've got a few of these cards, each referencing different data. The button is necessary, for the user to initiate the update. But then, I've gone ahead and added optional timers, so that these updates can happen ever 5 minutes, whether the card is open or not. So even if I placed the code in the Stack area, I believe I would still have to use the, of card "card.name" reference, to update the specific text fields on each card.
Though, I didn't know about that Target() function. I will try to use this.
Re: Referencing Objects on Other Cards
Posted: Mon Dec 16, 2013 10:18 pm
by dunbarx
Townsend.
I will bet you that moving handlers to a higher level is simpler in your project than you think, once you get your head around extracting the functionality from the local ones and renaming. And if certain cases require, it is not at all ungainly to have to designate card names.
The trick is to reduce your management burden as these things get larger.
As an exercise, make a few buttons, and name them each differently. In the stack or card script:
Code: Select all
on mouseUp
answer the short name of the target
end mouseUp
Yep, we have a "mouseUp" handler, not in a button, where it seems to belong, but rather in a high level script. Wanna make something of it?
Craig