Documentation Set for Swarm 2.2
Prev | Chapter 4. Mag 4x:Sketch of Code | Next

# 4.3. Building Agents

Now that Heatbugs have been defined, the model swarm needs to create them. This code fragment is from the `buildObjects` method on HeatbugModelSwarm.

    // A loop to create a bunch of heatbugs.
    for (i = 0; i < numBugs; i++)
     {
      Heatbug * hbug;
      int idealTemp, outputHeat;

      // Choose a random ideal temperature, output heat from the specified
      // range (model parameters).
      idealTemp = [uniformRandom rMin: minIdealTemp Max: maxIdealTemp];
      outputHeat = [uniformRandom rMin: minOutputHeat Max: maxOutputHeat];

      // Create the heatbug, set the creation time variables
      hbug = [Heatbug createBegin: [self getZone]];
      [hbug setWorld: world Heat: heat];
      hbug = [hbug createEnd];

      // Add the bug to the end of the list.
      [heatbugList addLast: hbug];

      // Now initialize the rest of the heatbug's state.
      [hbug setIdealTemperature: idealTemp];
      [hbug setOutputHeat: outputHeat];
      [hbug setX: [uniformRandom rMax: worldXSize] // random position
             Y: [uniformRandom rMax: worldYSize]];
    }

The details of this code are best explained in reading the documentation for the libraries and the heatbugs demo application itself. Essentially, we first generate two random numbers: an ideal temperature and an output heat for the new Heatbug. We then create the Hheatbug itself with `createBegin` and fill in the required parameters of world and heat. Once those are set, we can send `createEnd` to the Heatbug and it is finished being created. After it's done being created we add it into a list of Heatbugs in the model and set a few parameters on it like the ideal temperature and the initial position.

Prev | Home | Next
Defining an Agent | Up | Building Space objects