Deleting Objects

If you call a list of objects, and one of the objects deletes itself from the list that was called, then the object after the one that deleted itself will miss out on the message. The same thing occurs if an object delete itself from the actorList during a stepframe event.

For example, if you have five objects (A,B,C,D and E) you could call them with the syntax-

 call(#HandlerName, ListOfObjects)

...if object "B" responds by deleting itself from that list, then the subsequent object "C" would not receive the call.

To avoid this, either (1) call a duplicate of the list (so that objects can delete themselves from the original list, leaving the call list intact); or (b) implement a 'garbage collection' style mechanism whereby objects flag their desire to be deleted once the call has finished.

Usually calling a duplicate is the quickest and easiest approach (especially if the list simply contains references to object). An example of the second approach:

on mUpdateObjects
  garbageList = []
  call(#mUpdate, listOfObjects, garbageList)
  repeat with anObject in garbageList
    listOfObjects.deleteOne(anObject)
  end repeat
end

-- the #mUpdate method of an object in the call list:

on mUpdate (me, garbageList)
  if removeMe() then garbageList.append(me)
  else ...update as normal
end
Last updated 29th of June, 2005

© 2006 MeccaMedialight. Site Powered by Wrangler 8.