Page 1 of 1

Share data between cards

Posted: Mon Oct 20, 2014 5:40 pm
by lexogram
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

Re: Share data between cards

Posted: Mon Oct 20, 2014 5:56 pm
by Klaus
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

Re: Share data between cards

Posted: Mon Oct 20, 2014 6:01 pm
by Klaus
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.

Re: Share data between cards

Posted: Mon Oct 20, 2014 6:29 pm
by lexogram
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

Re: Share data between cards

Posted: Tue Oct 21, 2014 5:03 pm
by jacque
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.

Re: Share data between cards

Posted: Tue Oct 21, 2014 5:19 pm
by Klaus
Yes, Jaqueline, but you are a HIGHLY experienced LC user 8)

Re: Share data between cards

Posted: Tue Oct 21, 2014 5:32 pm
by jacque
It's never too early to learn about custom properties. :-)

Re: Share data between cards

Posted: Tue Oct 21, 2014 5:34 pm
by Klaus
Yep, but one tiny step after another :D

Re: Share data between cards

Posted: Tue Oct 21, 2014 6:38 pm
by dunbarx
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

Re: Share data between cards

Posted: Wed Oct 22, 2014 8:52 pm
by jacque
dunbarx wrote: That is correct, you cannot:

Code: Select all

get the loc of field id 1002 of stack "yourStack"
Works okay here.

Re: Share data between cards

Posted: Thu Oct 23, 2014 3:46 am
by dunbarx
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