7. Guide to Python in Civ IV: Creating A Unit In Civ

Changing whether or not there's a religion in a city is one thing, but creating a brand new unit out of thin air is quite another. Thankfully, it's not that difficult if you know the right function. A similar function exists for cities, but unfortunately not for players.

The function to create a new unit belongs to the Player class, and has the structure as follows: ".initUnit(int UnitType, int XLoc, int YLoc, obj UnitAI)". UnitType is an integer from 0 to something (the last unit); XLoc is the x-value where the unit appears, YLoc is the y-value where the unit appears, and UnitAI is an object which contains stuff related to what AI the unit uses. Right now you should only use the string "UnitAITypes.NO_UNITAI" in this location - using something else later may be possible, but for now that's all that should be used.

Now then, to create a unit things are fairly straightforward. We will create a Warrior, which has an ID of 9. The next two arguments are a bit trickier. Where on the map do you want the Warrior to appear? Well, just picking numbers out of thin air is possible, but it might land the unit on top of an enemy city, on a peak or even in the water - which probably wouldn't end well. So we need to be careful in where we put it - how about in the capital city?

First build your first city then try the following in the CIV console:

GC = CyGlobalContext()
Player = GC.getPlayer(0)
City = Player.getCity(0)
CityX = City.plot().getX()
CityY = City.plot().getY()
Player.initUnit(9, CityX, CityY + 1, UnitAITypes.NO_UNITAI)

The first 3 lines I've already covered.

The next two involve a new class/object: plot. Plots are the different tiles on the map. Plot (0,0) is in the far upper left-hand corner (I think, either that or the lower left-hand) and the Plot object can be accessed through the "plot()" function in the CyCity class (accessed from CyPlayer using the getCity(x) function… and so on). After we find the capital city's plot, we then find its X and Y values and assign them to a new pair of variables. The final line is the actual function which creates the unit. As outlined above, the first argument is the Warrior's ID, the second the unit's X, the third the unit's Y and the final the Unit's AI. When called as above, a new Warrior will appear in the capital! Voila.

As I said, something similar can be done to create cities. Under the player class is the initCity() function, which takes two parameters that we've seen before, the city's location: the first being X, and the second Y. It shouldn't be that hard to figure out by now, but if anyone tries and needs some help, just go ahead and post.

I've covered the basics now. I can't think of anything else to add to this section, but if I do I will post it later. The final section I have planned is a nice organized list of useful classes and functions for easy reference.


Index

1. Guide to Python in Civ IV: Introduction
2. Guide to Python in Civ IV: Basic Python
3. Guide to Python in Civ IV: Classes in Python
4. Guide to Python in Civ IV: Python in Civ IV
5. Guide to Python in Civ IV: Iteration (Looping)
6. Guide to Python in Civ IV: Civ IV Function Assignment
7. Guide to Python in Civ IV: Creating a Unit in Civ IV
8. Guide to Python in Civ IV: Class Reference (by Locutus)
9. Further Class Reference (by Solver)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License