Creating/managing/deleting many identical objects

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
ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Creating/managing/deleting many identical objects

Post by ittarter » Sat Nov 25, 2017 9:16 pm

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?

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Sat Nov 25, 2017 10:35 pm

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?)
Yes.

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 
Or maybe, since you mentioned an array to hold certain data, with a repeat loop:

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]
  
You could delete them all (or do anything to them all) by only targeting those images with a specific property.

That sort of thing.

Craig Newman

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Creating/managing/deleting many identical objects

Post by ittarter » Sun Nov 26, 2017 11:21 am

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?

jmburnod
VIP Livecode Opensource Backer
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

Post by jmburnod » Sun Nov 26, 2017 1:51 pm

Hi,
just repeat for each object y of this card, if the cPin of y is true then delete object y? Something like that?
Yes, but you have to use "num of controls down to 1" in your loop to begin with last layer

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
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Sun Nov 26, 2017 4:31 pm

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

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Creating/managing/deleting many identical objects

Post by ittarter » Sun Nov 26, 2017 11:19 pm

Sometimes I count upward.

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Sun Nov 26, 2017 11:36 pm

Me too.

But when deleting, your stack will collapse downward, and your count will count itself out.

Craig

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Creating/managing/deleting many identical objects

Post by ittarter » Mon Nov 27, 2017 9:47 am

dunbarx wrote:
Sun Nov 26, 2017 11:36 pm
Me too.

But when deleting, your stack will collapse downward, and your count will count itself out.
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?

jmburnod
VIP Livecode Opensource Backer
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

Post by jmburnod » Mon Nov 27, 2017 10:08 am

Hi,
there will no longer be the same number of controls, and the repeat function could stop prematurely?
Yes, if you have 4 controls, script stop at i = 3 because there is only 2 controls and you will get this message:
Chunk: no such object

Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
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

Post by dunbarx » Mon Nov 27, 2017 2:43 pm

), by the mid point, there will no longer be the same number of controls,
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.

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

FourthWorld
VIP Livecode Opensource Backer
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

Post by FourthWorld » Mon Nov 27, 2017 4:43 pm

1. Put two coins on your desk.
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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”