Simple Question: How to delete objects in a group?

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Cheong
Posts: 30
Joined: Tue Oct 27, 2015 7:49 am

Simple Question: How to delete objects in a group?

Post by Cheong » Fri Oct 30, 2015 7:40 am

Hi readers,

I create a group using script and then add objects in it.
Then when I restart the app I want the objects in the group to be deleted and recreated.
How do I delete objects in group but keeping the group?

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Simple Question: How to delete objects in a group?

Post by MaxV » Fri Oct 30, 2015 12:12 pm

Using this code you can select what you want to delete and what to maintain (you can use your group name, instead of 1):

########CODE#######
put the number of controls of group 1 into temp
repeat with i=1 to temp
put the name of control i of group 1 after listContr
put CR after listContr
end repeat
repeat for each line tLine in listContr
if the last word of tLine is not (quote & "grElement" & quote) then
do "delete " & tLine & "of group 1" #this is to avoid syntax errors
end if
end repeat
#####END OF CODE#####

in order to create something in a group just use something like:

Code: Select all

create button "example"  in group "my_group"
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Fri Oct 30, 2015 3:37 pm

Hi.

Try this as an experiment. On a new card, make four buttons. Group any three of them, and name the group "xx". In the ungrouped button script:

Code: Select all

on mouseUp
   repeat with y = the number of controls down to 1
      if the short name of the owner of control y = "xx" then delete control y
   end repeat
answer the name of group 1
end mouseUp
Craig Newman

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Simple Question: How to delete objects in a group?

Post by sritcp » Fri Oct 30, 2015 8:20 pm

Craig:

As an aside, I see that there is no direct way for a control to delete itself.
It needs to initiate a handler elsewhere, as in a card script.
The button script:

Code: Select all

on mouseUp
   send "killMeNow " & the short name of me to this card in 0 sec
end mouseUp
There needs to be a handler in the card script to do the deed.
Assisted suicide, I guess.

Regards,
Sri

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Fri Oct 30, 2015 11:04 pm

, I see that there is no direct way for a control to delete itself.
Yep. No control with a running handler can delete itself. Even if a handler passes control to another handler, that handler cannot delete the calling handler in the calling control.

Craig

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Simple Question: How to delete objects in a group?

Post by sritcp » Fri Oct 30, 2015 11:35 pm

dunbarx wrote: No control with a running handler can delete itself.
Yes, that's why I guess "send in 0 sec" works, since the original handler ends when (before?) it sends the kill signal.

Regards,
Sri.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Fri Oct 30, 2015 11:43 pm

On a card make a button. In the button script:

Code: Select all

on mouseUp
   startDelete
end mouseUp
In the card script:

Code: Select all

on startDelete
   if the optionkey is down then exit startDelete
   put random(999) 
   delete button 1
   send "startDelete" to this cd in 30
end startDelete
The code fails at runtime. You cannot pass the buck.

Craig

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Simple Question: How to delete objects in a group?

Post by sritcp » Fri Oct 30, 2015 11:52 pm

No, but the other way (that is, putting "send in 0 sec" in the button script) works! That was my point.

Make a button named "Test" and put in its script:

Code: Select all

on mouseUp
   send "startDelete" to this card in 0 sec
end mouseUp
Put in the card script:

Code: Select all

on startDelete
   delete button "Test"
end startDelete
Now click the button "Test"; harakiri !!

Regards,
Sri

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Sat Oct 31, 2015 3:00 am

Hi.

Hmmm.

The "send in time" command seems to allow (direct?) the button handler to "complete". So the calling object's script is no longer running when the card script fires and runs.

If you step through, you can see it, even though the explicit timing "0 sec" is wrecked. Simply "sending" in equal to simply calling, so the "in time" is what changes the way LC manages the sequence. This could be either a powerful feature, or just an anomaly that might trip you up if you are not aware of it.

Your vote?

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Sat Oct 31, 2015 4:04 am

Hi again.

Here is a great example of what you found. This is from another thread I was on (HELP CREATE TIMER) in this forum. The OP wanted a countDown/countUp timer. I told him to make a buttona and a field, and place something similar to this in the button script:

Code: Select all

on mouseUp
      countDown "0:05"
end mouseUp

On countDown var
    if the optionKey is down then exit countDown
      set the itemDel to ":"
      put var into fld 1
   switch
      case  var = "0:0"
         countUp var
         break
      case item 2 of var = 0 and item 1 of var <> 0
         put 60 into item 2 of var
         subtract 1 from item 1 of var
         break
   end switch
           subtract 1 from item 2 of var
      send "countDown" && var to me in 60
end countDown

On countUp var
    if the optionKey is down then exit countUp
      set the itemDel to ":"
      put var into fld 1
   switch
      case item 2 of var = 60
         put 0 into item 2 of var
         add 1 to item 1 of var
         break
      case item 2 of var <> 60
         add 1 to item 2 of var
         break
   end switch

-- send "countUp" && var to me in 60

  wait 60
   countUp var
end countUp
As written, with a "wait" and a "hard" call to the handler "countUp" it works. But comment out those two lines, and uncomment the "send..." line, and the "countUp" handler does NOT fire, rather the "countDown" handler fires, and continues.

This is perplexing to me, and I thought I knew something about how xTalks work. Please check it out. I feel like sending this in as a bug report, but I have a feeling it involves something I am missing, all that xTalk stuff notwithstanding.

Craig

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

Re: Simple Question: How to delete objects in a group?

Post by jacque » Sat Oct 31, 2015 6:02 pm

A "send" command without a time attached executes immediately inside the handler like any other command or function. The calling handler then resumes where it left off.

Sending in time puts the command into a queue. The calling handler completes and then at the next idle the queue is checked to see if any pending messages should be executed. Sending in 0 will execute the command immediately because zero wait time is "now". Since the original handler has completed, deletion of its object is possible.

It's actually possible to send a message in -1 time units which will force it to the top of the queue if there are two events scheduled for the same time, but that's a very rare situation and I've never needed to use it. It could be useful in some heavy networking scripts though.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple Question: How to delete objects in a group?

Post by jacque » Sat Oct 31, 2015 6:24 pm

dunbarx wrote:As written, with a "wait" and a "hard" call to the handler "countUp" it works. But comment out those two lines, and uncomment the "send..." line, and the "countUp" handler does NOT fire, rather the "countDown" handler fires, and continues.

This is perplexing to me, and I thought I knew something about how xTalks work. Please check it out. I feel like sending this in as a bug report, but I have a feeling it involves something I am missing, all that xTalk stuff notwithstanding.
Not a bug, it's the structure of the script. Countup is blocking, it calls itself recursively. Because it was triggered from countdown, when the recursion ends, control is returned to the countdown handler which then places a message in the queue for countdown to run again in 60 seconds.

A better way to structure the script might be to use a flag to indicate the direction and use a send in time command for both directions. That would avoid the block, which as written will prevent any user or background actions for as long as the timer is running.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Simple Question: How to delete objects in a group?

Post by Simon » Sat Oct 31, 2015 8:29 pm

Back to Cheong's original question...
Once in a standalone if the stack was last saved without the group and the objects they wont show up on restarting the app.
Simple :)

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

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Simple Question: How to delete objects in a group?

Post by sritcp » Sat Oct 31, 2015 9:55 pm

jacque wrote:Not a bug, it's the structure of the script. Countup is blocking, it calls itself recursively. Because it was triggered from countdown, when the recursion ends, control is returned to the countdown handler which then places a message in the queue for countdown to run again in 60 seconds.
Yes, it is working exactly as one would (should?) expect. If you step through the code:
When var hits 0, countup is called. It increments and then "sends in 0 sec" a countup recursion with var =1.
This is queued, while countdown resumes. But, it resumes with var = 0, and goes on decrementing it to -1, -2, -3, etc. interminably.
If you press the Option key, Craig's code will exit the countdown recursion and go to countup that is waiting in the queue.
This will take the var = -3 (say) that came out of the countdown process, and start incrementing interminably, until Option key is pressed.

All's well, except for the fact that the queued countup had a parameter value of var = 1, but instead it uses the output of countdown, that is var = -3 (or wherever you ended the recursion).
This tells me that the parameter is not evaluated when it is sent but when it is executed. Didn't know that!

Regards,
Sri

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7812
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Simple Question: How to delete objects in a group?

Post by dunbarx » Sun Nov 01, 2015 4:16 am

Jacque.

Makes sense. I got stuck thinking that in-line execution ought to rule, but of course there are "layers" of control among several handlers.

I had a flag, and it worked fine. I was fooling around with Sri's "send in 0 seconds". The point is not that 0 seconds executes at once, rather that "send in time" works as you said, releasing control back to the calling handler.

All is well...

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”