Share data between cards

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
lexogram
Posts: 13
Joined: Fri Oct 17, 2014 6:58 pm

Share data between cards

Post by lexogram » Mon Oct 20, 2014 5:40 pm

Hello LiveCoders!

I have 2 different cards which all need access to the same information. I have stored the information in a field. The 2 cards share a button (via a shared group). Clicking on the button queries the text of the field.

It seems that the field needs to be attached to all of the cards (and then hidden), otherwise the script on the button complains "No such object" when I refer to the field by name. Alternatively, I can hard-code the reference as "field x of card y", but this is not good practice. As the project evolves the number of card y may change.

Is there a way to refer to a field (or other control) in a non-localised manner? One workaround that I can imagine would be to create a card named (say) "Data" and use its (hard-coded) name in all references. Is that what other people do, or does LiveCode provide a more elegant way to proceed?

EDIT: It seems that simply placing the same field on 2 different cards (as part of a shared group) leads to the creation of two distinct field objects, with separate `text` properties. Fields which are added to another card using the Object > Place Group menu command are empty. Is this the intended behaviour? (I'm working with LiveCode 7 beta).

Thanks in advance,

James

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

Re: Share data between cards

Post by Klaus » Mon Oct 20, 2014 5:56 pm

Hi James,

just a letter sent via snail-mail (PAPER, envelope, post office, stamps etc..., remember that one? :D ) everything will work out fine
if you supply the correct address, otherwise: Return to sender (Error in Livecode) :D

This will work on ANY card in your stack:
...
put fld "the field with the info in ti..." OF CD 1 into myvar
...
Get the picture?
You can address ANY object form ANYWHERE as long as you supply the corect "descriptor"!

Even this will do with a NOT open stack file:
...
put fld "a field in a stack on disk" of cd 3 of stack "/Volumes/HD500/Documents/Livecode stuff/light_layout_mm.livecode" into myVar
...


Best

Klaus

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

Re: Share data between cards

Post by Klaus » Mon Oct 20, 2014 6:01 pm

Overlooked this one:
EDIT: It seems that simply placing the same field on 2 different cards (as part of a shared group) leads to the creation of two distinct field objects, with separate `text` properties. Fields which are added to another card using the Object > Place Group menu command are empty. Is this the intended behaviour? (I'm working with LiveCode 7 beta).
Do this:
1. Create a field
2. Check its SHAREDTEXT property (inspector for the field)
3. Group this single field and THEN place on all cards you need to.

That property manages that the SAME text will be shown in the field on any card.
If NOT checked, the you can have different text on different cards in the SAME field :D

Great for database like card structures.

lexogram
Posts: 13
Joined: Fri Oct 17, 2014 6:58 pm

Re: Share data between cards

Post by lexogram » Mon Oct 20, 2014 6:29 pm

Hi Klaus,

Thanks for your 2 answers. The Share Text checkbox in the Inspector does the trick.

I understand about using the full address for an object. I was hoping that there was a direct way of asking the current stack for a control with a given name or a given id, but it seems that there is not. You'd have to create a custom function in a stack script to iterate through all the cards to find it.

Thanks again,

James

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

Re: Share data between cards

Post by jacque » Tue Oct 21, 2014 5:03 pm

I wouldn't use a field at all, I'd put the data into a custom property of the stack. That will be available to any script anywhere, even those in different stacks, as long as the stack is in RAM.

If you do need to display the text, it's a simple matter to get it from the property and put it in the field.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Share data between cards

Post by Klaus » Tue Oct 21, 2014 5:19 pm

Yes, Jaqueline, but you are a HIGHLY experienced LC user 8)

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

Re: Share data between cards

Post by jacque » Tue Oct 21, 2014 5:32 pm

It's never too early to learn about custom properties. :-)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Share data between cards

Post by Klaus » Tue Oct 21, 2014 5:34 pm

Yep, but one tiny step after another :D

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

Re: Share data between cards

Post by dunbarx » Tue Oct 21, 2014 6:38 pm

Hi.
I was hoping that there was a direct way of asking the current stack for a control with a given name or a given id, but it seems that there is not.
That is correct, you cannot:

Code: Select all

get the loc of field id 1002 of stack "yourStack"
Or whatever. You must be resident on the card where the object lives, or do what you correctly surmised, query all the controls on all the cards of the stack. Fortunately, LC is fast enough to make such a function very speedy.

There are tools to do that in the authoring environment, like the "Project Browser" or the "Find and Replace" gadget.

Craig Newman

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

Re: Share data between cards

Post by jacque » Wed Oct 22, 2014 8:52 pm

dunbarx wrote: That is correct, you cannot:

Code: Select all

get the loc of field id 1002 of stack "yourStack"
Works okay here.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Share data between cards

Post by dunbarx » Thu Oct 23, 2014 3:46 am

Of course it does. Who said it didn't???

Names don't work, but ID's surely do.

Too fast, too glib. Gotta watch that...

Craig

Post Reply