april  1.0.0
...
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Example 7 - Creating an actor

New actors may be created from scratch, using energy from the world, or may be created from existing actors, using a aprt of their energy.

For the first method to work we have to set-up factories as in the previous example. We ammend the ActorFactory to create a more interesting actor. This is the new constructor:

FActor( World * w ) : ActorFactory( w ) {
w->insertId( IdKind, "kinds.test" );
addMyself( IdKind );
DNA::InitData idata;
idata.kind_ = IdKind;
idata.cost_ = 1;
idata.age_= 100;
idata.energy_= 10;
initDNA( idata );
defaultDNA().addBrain( IdBrain);
defaultDNA().addActuator( IdActuator );
defaultDNA().addReflex( IdReflex );
defaultDNA().addSensor( IdSensor );
}

We're initialising the DNA our factory holds arround with a kind (IdKind), a running cost (1), an average life span (100) and the energy required at birth (10). As previously stated, this energy must come from the world or from the parents.

Once we have a valid DNA instance we add the brain, actuator, reflex and sensor to the DNA sequence. Any actor created by this factory will have these components (if they can be created).

Now, after we have a valid world we can create a new actor from scratch like so:

Actor * actor = w->createActor( IdKind );

This is what happens inside:

  • the world locates the ActorFactory for IdKind;
  • the factory is asked to create the instance with ActorFactory::create();
  • Actor::decodeDNA() is employed to iterate the components saved in the DNA as IDs (in our case a brain, an actuator, a reflex and a sensor); for each component the Factory class is searched and, if found, asked to create an object for that ID;
  • the ammount of energy required by the actor is substracted from the free (unbounded) energy of the world
  • Actor::makeAlive() is called to finalise the initialisation
  • custom actors are informed that they were inserted in a world with a call to Actor::inserted().

Do not forget to release the reference that is passed to the caller by World::createActor().




Example 6 - Factories april-core library Example 8 - Events and event lines