Creating/managing/deleting many identical objects
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Creating/managing/deleting many identical objects
Hi,
I've been wondering about something for a long time and have never been able to fully understand how to create and delete objects through pure script.
So there are a lot of relevant commands I've come across in the dictionary (and probably there are some that I've missed):
create [object]
copy [object]
clone [object] (still, the object ID must be different, is that the only difference?)
set the property profile of [object] to x (does this include the object's script?)
set the name of [object] to tString
delete [object]
If I wanted to create 50-100 temporary "pins" on a world map that would be individually "hilited" by mouseover and would result in an action on mouseup, would the best way be to:
1. clone all the pins from a master pin by using a repeat command;
2. set the location of each pin using a previously existing array;
3. ignore object names/scripts and just use card scripts to manage user interactions by e.g. detecting the location of the object clicked;
4. delete all these objects when the user leaves the card (would there be a way to do this without saving the individual object names?)
I hope that this isn't too ambiguous. I'm just trying to wrap my head around this kind of LC functionality. Are there any good lessons or sample stacks on managing lots of identical objects, or is it pretty straightforward? Is this a pretty intensive function or is it no different than populating an array with 100 elements?
I've been wondering about something for a long time and have never been able to fully understand how to create and delete objects through pure script.
So there are a lot of relevant commands I've come across in the dictionary (and probably there are some that I've missed):
create [object]
copy [object]
clone [object] (still, the object ID must be different, is that the only difference?)
set the property profile of [object] to x (does this include the object's script?)
set the name of [object] to tString
delete [object]
If I wanted to create 50-100 temporary "pins" on a world map that would be individually "hilited" by mouseover and would result in an action on mouseup, would the best way be to:
1. clone all the pins from a master pin by using a repeat command;
2. set the location of each pin using a previously existing array;
3. ignore object names/scripts and just use card scripts to manage user interactions by e.g. detecting the location of the object clicked;
4. delete all these objects when the user leaves the card (would there be a way to do this without saving the individual object names?)
I hope that this isn't too ambiguous. I'm just trying to wrap my head around this kind of LC functionality. Are there any good lessons or sample stacks on managing lots of identical objects, or is it pretty straightforward? Is this a pretty intensive function or is it no different than populating an array with 100 elements?
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Creating/managing/deleting many identical objects
Yes.1. clone all the pins from a master pin by using a repeat command;
2. set the location of each pin using a previously existing array;
3. ignore object names/scripts and just use card scripts to manage user interactions by e.g. detecting the location of the object clicked;
4. delete all these objects when the user leaves the card (would there be a way to do this without saving the individual object names?)
You don't necessarily need an array. You should read up on the template(object). This allows you to preset the properties of any type of control when they are newly created. Watch the state of the lockMessages if you do this, as per the dictionary. Or, as you say, clone from a master. Also check out the keyword "last".
To make everything easy, the trick is to name the new pins, or set a custom property common to them all, so you can easily reference them for future access or deletion.
Something like, assuming you already have an img named "pin 1"(pseudo):
Code: Select all
on mouseUp
copy img "pinImage"
set the name of last img to word 1 of the short name of last img && (word 2 of the shot name of last img + 1) set the loc of last image to wherever
end mouseUp
Code: Select all
repeat with y -= 1 to 100
copy img "pinImage"
set the name of last img to "pin" && y
set the loc of img ("pin" && y) to pinArray[y]
That sort of thing.
Craig Newman
Re: Creating/managing/deleting many identical objects
Thanks, Craig. Glad to know I'm not barking up the wrong tree.
So using a custom property, the cPin of the template object could be "true", and then I could just repeat for each object y of this card, if the cPin of y is true then delete object y? Something like that?
So using a custom property, the cPin of the template object could be "true", and then I could just repeat for each object y of this card, if the cPin of y is true then delete object y? Something like that?
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Creating/managing/deleting many identical objects
Hi,
Best regards
Jean-Marc
Yes, but you have to use "num of controls down to 1" in your loop to begin with last layerjust repeat for each object y of this card, if the cPin of y is true then delete object y? Something like that?
Code: Select all
repeat with i = the num of controls down to 1
if the cPin of control i then
delete control i
end if
end repeat
Jean-Marc
https://alternatic.ch
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Creating/managing/deleting many identical objects
What Jean-Marc said about going from high to low. Do you see why that has to be? It is the way human beings count.
Craig
Craig
Re: Creating/managing/deleting many identical objects
Sometimes I count upward.
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Creating/managing/deleting many identical objects
Me too.
But when deleting, your stack will collapse downward, and your count will count itself out.
Craig
But when deleting, your stack will collapse downward, and your count will count itself out.
Craig
Re: Creating/managing/deleting many identical objects
Ahh, that makes more sense now. So if you do it the other way (counting up), by the mid point, there will no longer be the same number of controls, and the repeat function could stop prematurely?
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: Creating/managing/deleting many identical objects
Hi,
Chunk: no such object
Jean-Marc
Yes, if you have 4 controls, script stop at i = 3 because there is only 2 controls and you will get this message:there will no longer be the same number of controls, and the repeat function could stop prematurely?
Chunk: no such object
Jean-Marc
https://alternatic.ch
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Creating/managing/deleting many identical objects
Exactly, because LC will re-order the remaining controls as they are deleted. So when you delete one, all the others shift "down" in number to take its place and fill the number order from 1 to whatever. But the index in the loop, proceeding to its initial upper value based on an original greater number of controls, keeps increasing, eventually ending up with a value that is greater than the current number of controls.), by the mid point, there will no longer be the same number of controls,
But if you count downward, the original "high" value creates no issues, since both it and the number of controls decrease together in step. LC needs not reorder anything as the process proceeds; all the remaining controls can retain their original numbering.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Creating/managing/deleting many identical objects
1. Put two coins on your desk.
2. Take one away.
3. Count the remaining coins until you find coin #2.
2. Take one away.
3. Count the remaining coins until you find coin #2.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn