child - parent


[ Zettels Traum ] [ search / suche ]

von dp am 06.Januar 97 um 00:48:27:

zu: Perlen aus dem Strom der Nachrichten im direct_L von Daniel am 22.Dezember 96 um 02:00:59:

What I do normally when creating an object is to make it a descendant
of a basic ancestor, a kind of Adam or Eve ancestor if you like, much
the same that the basic TObject class in Object Pascal or Delphi. Here
is the code of this common ancestor I use normally:

parent script "parentObject"

property pLeaf     -- to emulate polymorphism inside objects
property pActList  -- contains normally the actorList, but you may change that
                   -- to any kind of list controlled by another object.

on birth me
  set pActList        = the actorList
  set the pLeaf of me = me

  return me
end birth

on death me
  stop (the pLeaf of me)
end death

-- useful method that returns the name of the parent script
on typeOf me
  set oldDelim = the itemDelimiter
  set the itemDelimiter = numToChar (34)
  set type = item 2 of string (the pLeaf of me)
  set the itemDelimiter = oldDelim
  return type
end typeOf

-- if the object is not placed yet in the pActList list, then do it here.
on animate me
  if not inActorList (the pLeaf of me) then
    append the pActList of me, the pLeaf of me
  end if
end animate

on stop me
  -- do not call from a stepFrame method sent directly by the ActorList!
  set posActList = inActorList (the pLeaf of me)
  if posActList then
    deleteAt (the pActList of me, posActList)
  end if
end stop

on inActorList me
  return getPos (the pActList of me, the pLeaf of me)
end inActorList

on stepFrame me
  -- to be overrided and not inherited
end stepFrame

on done me
  return FALSE
end done

When you subclasse/inherit that parent script in another parent script
you will do:

parent script "descendant" -- this parent script can also be an ancestor of
                           -- another parent script

property ancestor
property pData

on birth me, someData
   set ancestor = birth (script "parentObject")
   set the pLeaf of me = me
   set pData = someData
   return me
end birth

on stepFrame me
   doSomethingUseful
end stepFrame

on done me
   return (some condition is FALSE or TRUE)
end done

and a control parent script may be:

parent script "controlParent"

property ancestor
property pControlList

on birth me
   set ancestor = birth (script "parentObject")
   set pControlList = []
   return me
end birth

on animate me
   animate (ancestor)
   set aChild = birth (script "descendant", data1)
   set the pActList of aChild = pControlList
   animate (aChild)
   set aChild = birth (script "descendant", data2)
   set the pActList of aChild = pControlList
   animate (aChild)
   ...
   set aChild = birth (script "descendant", dataN)
   set the pActList of aChild = pControlList
   animate (aChild)
end animate

on stepFrame me
   -- need to be done that way, because we may take out some childs
   -- of the control List inside that loop
   repeat with i = count (pControlList) down to 1
      set aChild = getAt (pControlList, i)
      if done (aChild) then
         death (aChild)  -- or stop it
      else
         stepFrame (aChild)  -- send the stepFrame and any other control
                             -- message you want
      end if
   end repeat
end stepFrame

There are two things of interest here:

1) The use of a property (pLeaf) to store the reference to the last
child object in the family tree. This way, the ancestors ALWAYS call
the handler of the last descendant (I follow here a suggestion of Peter
Vanags and Tim Gardner).

2) The use of a property (pActList) to store the control list that send
the stepFrame message. The standard "the ActorList" is used normally,
but you can change that to another list controlled by another object
placed in the ActorList, as is the case here. This way you can control
all your objects directly, and only the control object is placed in the
ActorList.




Dazu:























D. Plänitz