How do you get the list of units at the start of the game?

DaddyHolby

Chieftain
Joined
Feb 20, 2003
Messages
32
Location
Superior, Co
I've been trying to get a list of units through python that are in play at the start of a game and I haven't found anything that will work. I've looked through the API guide and I haven't seen anything that will return a list (array or collection) of CyUnit objects or a list (again array or collection) of unit IDs that can be used with the CyPlayer.GetUnit function to get units.

What I'm trying to do is loop through all of the units at the start of a game and change their names (and later assign a promotion to them).

So is there anyway to do this in Python or is this a SDK thing?
 
There is an easy way to do this in python, and also a correct way.

The easy way: for a given player, call CyPlayer.getNumUnits() to get the number of units that player has. Then make a loop of that range and call CyPlayer.getUnit(i) to get the unit. Two lines of code.

The correct way: apparently the range sometimes has "holes", where units 0,1,2,3 exist, but unit 4 does not, and units 5,6,7... exist. So a better way is:
Code:
(pUnit, iter) = pPlay.firstUnit(false)
while (pUnit):
   ... your stuff here
   (pUnit, iter) = pPlay.nextUnit(iter, false)
Once you have the unit, you can call pUnit.getUnitType() and all the other functions to do things with it.
 
Top Bottom