Skip to content
Jasper Geurtz edited this page Jan 13, 2020 · 5 revisions

Now that we are successfully gathering minerals, we need to spend them somehow.

Let's train some workers when we have the minerals for them!

Training units

Lets go trough all our buildings in theonFrame method, check if and what they can make and then train the first UnitType they can make if we have enough resources. This code will NOT work for Zerg as Larvae are not buildings.

// Train units while we can
for (Unit trainer : self.getUnits()) {
    UnitType unitType = trainer.getType();
    if (unitType.isBuilding() && !unitType.buildsWhat().isEmpty()) {
        UnitType toTrain = unitType.buildsWhat().get(0);
        if (game.canMake(toTrain, trainer)) {
            trainer.train(toTrain));
        }
    }
}

The buildsWhat() method in UnitType is used to check what a specific Unit can build. Furthermore the canMake(target, builder) method in Game is used to check if we can currently build this unit (e.g. have all the necessary prerequisites.

Finally training a unit is a simple as

trainer.train(toTrain)

Another more specific example would be

commandCenter.train(UnitType.Terran_SCV)

After this short example on how to train units, let's dive into how to build some Buildings!

← previous | next →

Clone this wiki locally