Custom properties?

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 3:46 pm

Hello: I would like to clone my stack, but I need some of the global variables to "stick" to the stack they belong to. I have read that I can accomplish this with setting custom properties, but I don't understand how. ??

For example, a stack "Greg", uses a global "DataArray" to store a big array of data that is accessible from various buttons and handlers within that stack.

I would like to Clone stack "Greg" - so make a new stack called "Greg copy". But my "DataArray" big array of data that is a global is now being shared between both stacks, "Greg" and "Greg Copy". I would like that global "DataArray" to be LOCAL to it's stack. (ie: I would like this copy to behave as if I launched a second instance of my app rather than cloning it).

Any suggestions?

thanks.
Greg

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Custom properties?

Post by ClipArtGuy » Wed Dec 02, 2015 4:22 pm

Code: Select all

clone stack "Greg"
set the UniqueDataArray of stack "Greg copy" to DataArray
this should give your copy stack a custom property called "UniqueDataArray" which should contain your array

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 5:21 pm

ClipArtGuy wrote:

Code: Select all

clone stack "Greg"
set the UniqueDataArray of stack "Greg copy" to DataArray
this should give your copy stack a custom property called "UniqueDataArray" which should contain your array
ahh... yes. So to call this global from the new stack, is this right?

put the UniqueDataArray of this stack into DataArray

And also... within scripts, I make changes to this global array, and call handlers which use the newly changed global array. If I switch to a custom property, then every time I make a change to the global array, I would need to "set the UniqueDataArray" of this stack to global array.

Right?

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Custom properties?

Post by ClipArtGuy » Wed Dec 02, 2015 5:47 pm

adventuresofgreg wrote:
ClipArtGuy wrote:

Code: Select all

clone stack "Greg"
set the UniqueDataArray of stack "Greg copy" to DataArray
this should give your copy stack a custom property called "UniqueDataArray" which should contain your array
ahh... yes. So to call this global from the new stack, is this right?

put the UniqueDataArray of this stack into DataArray

And also... within scripts, I make changes to this global array, and call handlers which use the newly changed global array. If I switch to a custom property, then every time I make a change to the global array, I would need to "set the UniqueDataArray" of this stack to global array.

Right?
I believe this is right. If not, I am sure one of the more experienced LiveCoders will chime in. 8)

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

Re: Custom properties?

Post by Klaus » Wed Dec 02, 2015 6:01 pm

Hi Greg,
adventuresofgreg wrote:... But my "DataArray" big array of data that is a global is now being shared between both stacks, "Greg" and "Greg Copy".
yes, that is the nature of global variables! :D
adventuresofgreg wrote:I would like that global "DataArray" to be LOCAL to it's stack. (ie: I would like this copy to behave as if I launched a second instance of my app rather than cloning it).
As already mentioned, set a custom property of your stacks to the global variable (and delete the global after that)
and then refer to this CP in the stack's scripts as "the cWhatever of THIS stack".
This way you have some kind of "per stack local thingie". :D

Hope I understood all this correctly!


Best

Klaus

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

Re: Custom properties?

Post by dunbarx » Wed Dec 02, 2015 6:11 pm

Hi.

If I understand correctly, you want a global to be "local" to a particular stack, so that if you create a clone of that stack, the global does not carry over.

Hmmm. Well, that flies in the face of what globals are, and do. And you cannot delete it because that removes it from memory completely, so the original stack cannot use it.

A custom property would have the advantage that you can empty it locally when you create the clone. Custom properties have all the power that a global does, but are local to the stack they live in. Would this work for you? That is what I would do.

If not, I think you have to make all your global references according to the stack that is in front:

Code: Select all

if the name of this stack is "yourOldStack" then put yourGlobal into fld 1 else put "" into fld 1


That sort of thing. Awful. About those custom properties. On a new card, make a button. Put this into its script:

Code: Select all

on mouseUp
set the testCustomProperty of this stack to  "XYZ & return & "ABC"
end mouseUp 
Now in the message box:

Code: Select all

answer the testCustomProperty of this stack
Easy. And custom properties survive sessions. They can be called from anywhere at any time. They do not have to be declared. They live only in the stack they live in.

Craig Newman
Last edited by dunbarx on Wed Dec 02, 2015 6:16 pm, edited 1 time in total.

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 6:15 pm

I'm really digging these custom properties. I've used them before.. without knowing what I was doing. I just noticed that I could set "anything" on "anything" to "anything" and then refer to it later. Ha ha! Thanks!

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

Re: Custom properties?

Post by dunbarx » Wed Dec 02, 2015 6:17 pm

Good.

They are a godsend. I just edited my most recent post, but I bet you are ahead of that.

Craig

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 7:58 pm

The only issue that I see using custom properties rather than a global variable, is if you refer to that global often in handlers and functions. Every time you make a change to the contents of a custom property, you must "set the customproperty of whatever to whatever", whereas with a globlal, when you make the change, the global is changed, right then and there. When you are working with a very large data array, and making a lot of changes to it through various handers and/or functions, then re-setting the custom property could end up taking more time, and slowing things down. This is my concern right now. I manipulate a very large data array in real-time with sliders than control variables. So far, from experiments moving the global to a custom property, it seems like it has slowed down my processes.

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

Re: Custom properties?

Post by dunbarx » Wed Dec 02, 2015 8:46 pm

Hmmm.

There should be little difference in setting a property and loading a variable. It is true that the method is different:

Code: Select all

put yourData into yourGlobal
set the yourGlobal of this stack to yourData
Each is a one-liner. You need "yourData" to be ready to go in either case.

I did a test with six million lines of data in a variable. Putting that data into a global and setting a property take the same amount of time (26 ticks). What are you working with that seems to either take more time (or code) to implement, or longer to execute?

Craig

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 8:53 pm

Yes, I just ran a test, and getting and setting custom properties is taking significantly longer than working with the globals. I think the reason is due to the extra steps in the custom property? for example:

Globals method

Code: Select all

Global BigDataArray

Put this into BigDataArray
put ThisFunction(BigDataArray) into that
Custom properties method

Code: Select all

put BigDataArrayProperty of this stack into BigDataArray

Put this into BigDataArray
put ThisFunction(BigDataArray) into that

set the BigDataArrayProperty of this stack to BigDataArray
So the difference is 2 additional statements getting and putting the custom property. Is there a more efficient way of doing this???

thanks

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

Re: Custom properties?

Post by Klaus » Wed Dec 02, 2015 11:01 pm

Hi Greg,

nope, you can access a custom property directly!
Looks like you do not modify/alter "the BigDataArrayProperty of this stack" at all, so you can do this even in a one-liner:
...
put ThisFunction(the BigDataArrayProperty of this stack) into that
...
:D


Best

Klaus

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

Re: Custom properties?

Post by dunbarx » Wed Dec 02, 2015 11:02 pm

Hi.

You are using two steps to put data into BigDataArray. But only the second one matters, overwriting the first. And you are taking another step to reload the property. Why? It has not changed, or if it has, then you also need to reload the global.

Try again? It would be of interest to me and others how this works out, but make sure the processes are identical.

Craig

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

Re: Custom properties?

Post by Klaus » Wed Dec 02, 2015 11:07 pm

After reading a second time I think I left out one line!? :oops:

Now it's two-liner, not too shabby either :D
...
put ThisFunction(this) into that
set the BigDataArrayProperty of this stack to this
...

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: Custom properties?

Post by adventuresofgreg » Wed Dec 02, 2015 11:16 pm

So.. how do I reference my array key from within the property?

I should have formed my example as such:

Code: Select all

put BigDataArrayProperty of this stack into BigDataArray

Put this into BigDataArray[z]
put ThisFunction(BigDataArray[z]) into that

set the BigDataArrayProperty of this stack to BigDataArray
I tried:

Code: Select all


put ThisFunction(BigDataArrayProperty[z] of this stack) into that

and it doesn't work. I think because the property is an array itself. ???

Post Reply