Understanding Custom Properties

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
JohnHubbert
Posts: 6
Joined: Mon Jan 19, 2015 10:15 pm

Understanding Custom Properties

Post by JohnHubbert » Mon Jan 19, 2015 10:19 pm

Hi, I'm new to LiveCode (loving it by the way) and to learn I've been dissecting the scripts of the LiveCode IDE.

Doing ok but I'm confused by this>


In stack revpreferences there is a CP as follows


cScriptEditor,shortcuts,ApplyAndClose

property contents are

enter
command return

Q. how would I script to change these values and can I have a brief explination please.

Many thanks

John Hubbert

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Understanding Custom Properties

Post by FourthWorld » Mon Jan 19, 2015 10:51 pm

While dissecting existing code can often be a good way to learn, manually editing preferences may result in unexpected behavior, as the code that depends on them may require specific values.

For a good intro on using custom props in general see section 7.8, "Custom Properties", in the User Guide (starting on p222), accessible from the Help menu.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

JohnHubbert
Posts: 6
Joined: Mon Jan 19, 2015 10:15 pm

Re: Understanding Custom Properties

Post by JohnHubbert » Tue Jan 20, 2015 7:47 am

Hi Richard, Thank you for your reply.

I have read through the User Guide section but still can not get my head around this particular example.
I don't want to change the rev IDE scripts I just would like to better understand how they work, so any help is appreciated.

John

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Understanding Custom Properties

Post by Simon » Tue Jan 20, 2015 10:39 am

Hi John,
You can use custom properties as you would any variable, the big difference is that it is saved with your stack when you save in the IDE.
My current example is I have 400 medications and their dosages. There is never a need for me to show all of them to the user, just specific ones based on the first few letters they type. When needed I load a variable up with "put the cMeds of this stack into tMeds" then I filter tMeds as they type showing the results.
Once in a standalone the custom property can be changed but it will not be saved when the application is shut down (that requires a different process). If I get repeated medications that are not in my stack I then change the custom property of the stack to include the new medication on the next version of the stack.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

JohnHubbert
Posts: 6
Joined: Mon Jan 19, 2015 10:15 pm

Re: Understanding Custom Properties

Post by JohnHubbert » Tue Jan 20, 2015 11:38 am

Hi Simon, thanks for your input.

I understand all off what you have said and how simple custom properties work but i still do not understand how I would be able to change and retrieve the values in the example.

In stack revpreferences there is a CP as follows


cScriptEditor,shortcuts,ApplyAndClose

property contents are

enter
command return

Looking forward to more insight

John

Tukcedo
Posts: 65
Joined: Fri Jun 27, 2014 9:43 pm

Re: Understanding Custom Properties

Post by Tukcedo » Tue Jan 20, 2015 11:59 am

Hmm that text that you show looks a bit strange, doesn't appear to be code at all. Perhaps my experiences (also a beginner) help you along the way.

I use custom properties (CP) as a replacement for global variables. The latter are a bit hard to keep track of IMHO and they need to be declared in each card that uses them, which I frequently forgot in which case you just end up with a local (to that card) variable of the same name but obviously NOT with the value you're expecting. So I define a stack CP for things like a database name, file location etc. Then each card has CPs if need be, which is nice so that you can keep the CPs logically together, which greatly helps structured programming.

You need to be aware that the way you reference a CP is slightly different from normal variables. Rather than:

put "this is my text" into tVariable

you'd say:

set the cVariable of card "Test" to "this is my text"

where the leading "c" means Custom Property and "t" temporary (local) variable, which are just conventions from the LC community as I understand it.

Does this help?
Michel J.L. van der Kleij
Coding to help stray animals in the Philippines
Albert Foundation - http://albert.tukcedo.nl
Aklan Animal Rescue & Rehabilitation Center - http://aarrc.tukcedo.nl

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Understanding Custom Properties

Post by jacque » Tue Jan 20, 2015 9:28 pm

I'll take a shot at this.

All LC controls and objects have attributes that describe their current state, and these attributes are called properties. The width of a control is a property that describes it, as is its location, color(s), certain behaviors (autoTab, for example) and so forth. It is this collection of properties that make all LC objects look and behave a certain way.

A script may need a way to store information about an object that doesn't fit into one of the built-in properties. In that case you can create your own custom property to store the information. For example, imagine a series of cards with four buttons each that represent four answers to a quiz question. Only one button on each card is the correct answer. I need a way to see which one that is so my script can handle the response. I could hard-code the number somewhere on every card, or into every card script, or use a hidden field on each card to store it, or change the button name to something a script could recognize. But it is easier and more flexible to set a custom property on the correct button and just check that.

So I "set the cCorrect of btn 3 to true" somewhere during development, either via the message box or in the property inspector. "cCorrect" is the name I've invented for my custom property, and just like variables, setting one in a script automatically creates it if it isn't there already. After that, all my generic handler needs to do is check to see if the cCorrect of the target is true, and if so, it knows the user clicked the right answer. I don't even need to set it to false on all the other buttons, because the cCorrect will return empty if the property doesn't exist.

Custom properties are persistent and are saved with the stack. You can use them to store anything, including binary data, arrays, text, booleans, whatever. They behave much like variables, but they become a part of the object they are attached to.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

JohnHubbert
Posts: 6
Joined: Mon Jan 19, 2015 10:15 pm

Re: Understanding Custom Properties

Post by JohnHubbert » Wed Jan 21, 2015 8:40 am

Thank you all for your explanations much appreciated.


However my original question still remains unanswered, perhaps I need to reword it.

OK if I have the CP named CScriptEditor and the CP contains "enter" then I can create or change this by the following>

set the CScriptEditor of stack "revNewScriptEditor" to "enter"

and to get the contents of the CP>

put the CScriptEditor of stack "revNewScriptEditor" into tmyData

That I understand.

Now referring back to my original question?

If there is a CP (set?) called "cScriptEditor,shortcuts,ApplyAndClose" in stack "revNewScriptEditor"

and the contents of this are listed as

"enter
command return"

How would I (a) create/edit this and (b) get at the CP contents.

Hopefully, John

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Understanding Custom Properties

Post by jmburnod » Wed Jan 21, 2015 12:38 pm

Hi John,
How would I (a) create/edit this and (b) get at the CP contents.
I hope this help

Code: Select all

on createSomeProp
   put "cScriptEditor,shortcuts,ApplyAndClose" into tTheCP
   put "enter,command,return" into tTheContents
   repeat with i = 1 to the num of items of tTheCP
      put item i of tTheCP into tOneCP
      set the tOneCP of this stack to item i of tTheContents
   end repeat
end createSomeProp

function getContentOneProp pProp
   put empty into tContentProp
   if pProp is in the customkeys of this stack then
      put the pProp of this stack into tContentProp
   end if
   return tContentProp
end getContentOnePropp

on setOneProp pProp,pPropContent
   set the pProp of this stack to pPropContent
end setOneProp
Best regards
Jean-Marc
https://alternatic.ch

JohnHubbert
Posts: 6
Joined: Mon Jan 19, 2015 10:15 pm

Re: Understanding Custom Properties

Post by JohnHubbert » Wed Jan 21, 2015 2:53 pm

Thank you, Jean-Marc,

That was exactly what I was looking for.

John

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Understanding Custom Properties

Post by jmburnod » Wed Jan 21, 2015 3:12 pm

Hi John,
I'm so happy to hear I understand sometimes english correctly :D
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”