Python Snippets

Congrats on getting it working!. :goodjob:
This site contains the API for most of the Python objects exposed to you. It's lacking some (like the GUI widgets), but it's a start.

Thanks for that. :king:

I can't say for sure, but I suspect you may be stuck with this unless you want to modify the C++ DLL.

Hopefully not. But Zebra 9 can do amazing things with python, so I'm hopeful. :mischief:
 
I still have to check into it but I think it's SDK only. I'll check.

Well, I was just thinking, it might be possible to remove them as Great People in the XML. If I did this the SDK would not put the Message then w/ Python I could display whatever message you want. :hmm:
 
Zebra 9, do you know if there is a way to limit how far a unit can travel from its civ's cities or cultural borders? Specifically, what I'm thinking about is limiting the exploration of an entire continent until a certain game date (whereupon the check would automatically be turned off, to increase game speed). This way you won't have lots of the map uncovered until later in the game.

I think there may be a mod that does this already...
 
I have python questiions please.

def onGreatPersonBorn(self, argsList):
'Great Person Born'
pUnit, iPlayer, pCity = argsList
player = PyPlayer(iPlayer)
infoUnit = pUnit.getUnitClassType()
# Check if we should even show the popup:
if pUnit.isNone() or pCity.isNone():
return

if(len(pUnit.getNameNoDesc()) == 0):

iCivilizationType = player.player.getCivilizationType()

pUnit.setName(NameUtils.getCivilizationName(iCivilizationType, infoUnit))


What is "argsList" first line
(self, argsList):
?
Why do
'Great Person Born'
comes between quotes ?
Wherre do
PyPlayer(iPlayer)
pyPlayer come from ?

I MUST learn python in order to have my mod working. Its a brand new concept that will rock....

Please.
Thanks in advance....
 
The argsList is an argument that is passed to that python function from the SDK as a tuple. The contents of this tuple varies from 1 function to the other.

And yes 'Great Person Born' is just a comment. I dilike using any comment type other then the pound symbol (#) because they tend to look too much like strings and the triple quote sometimes makes the comment blend w/ the code making the code very confusing.

PyPlayer comes from the PyHelpers (I think that's what it's called) module which is imported at the top of the file then just below the import you have a line like PyPlayer = PyHelper.player. A PyPlayer instance gives you some controls that allow you to change some things about a player. I personally prefer the gc.getPlayer system.
 
The comment line in "" that comes after each function or method definition is considered good python syntax and is used by certain programs to gather information about the function. One of the standard python pdf documents explains it all, but one good example is the python IDLE editor. When you start to type a function or method and it shows you the popup with what arguments are needed it will also display that first comment line. Not that important for Civ4, but good python knowledge. :)
 
Thanks a lot for your time people ^^.
Now
The contents of this tuple varies from 1 function to the other.
So How do I know this content? Where does it comes from ?

What relation with this
pUnit, iPlayer, pCity = argsList
?
 
Code:
 pUnit, iPlayer, pCity = argsList
That is the code that is used to unpack the argsList tuple. You might also see code like this:
Code:
pUnit = argsList[0]
iPlayer = argsList[1]
pCity = argsList[2]
I havn't found any python functions in the eventmanager or game utils that didn't already have the unpack line(s). So if you keep your original files you can run a search for the function name in the appropriate file and just copy the unpack line(s).

Wow, you learn somthing new everyday. I didn't know that Jeckel. Now is that just for single quotes, I would think so?
 
No, as a matter of fact, the proper syntax is to use triple quotes for the main doc strings. For example,
Code:
def aMathFunc(iA, iB):
        """Return result of some math"""

And if you use the triple quotes, for those that don't know, you could also do somthing similar to this,
Code:
def aMathFunc(iA, iB):
        """Return result of some math

        A math function in a module that takes two int numbers
        and does some math to them. It will then return an int
        number."""

If you are interested, here is a zip of an rtf text file. I grabed it from one of the main python site and is the excepted syntax for using doc strings. Its not that long or well written, but it has all the important info. :)
 
Well use the CyGlobalContext (usualy it has been assingned to the gc variable) class' getCivilizationInfo function like this:
Code:
tech = gc.getInfoTypeFor("TECH_WRITING")
civ = gc.getInfoTypeForString("CIVILIZATION_AMERICA")
gc.getCiviliztionInfo(civ).isCivilizationDisableTech(tech)
Now just so you know, the isCivilizationDisableTech returns a boolean value (True or False).
 
Yes, all I need now is a "setCivilizationDisableTech"

Then I think I will go, in the TechResearched event, to get a list off all remaining Techs, and randomly pick a tech from it, and then "setCivilisationDisable" it, as the "disable" integer is part of the technologies description.

Ok, so where can I find
CyGlobalContext
?

is it part of Python or from the SDK ? Because I can't find it nowhere in the Py files....
 
You don't get the py file for it. It's part of the compiled package that is imported when you import PythonExtensions (iirc) at the top of your py file. All the main cIV py files import it, so just copy that line.

Also, this site is a good reference for all the available functions. My guess is that you cannot dynamically alter that setting as you'd like. That class has no corresponding setXXX() function.

The Info classes tend to encapsulate read-only information on the basic data for cIV, but don't quote me. :)
 
Thanks for answering,

main cIV py files import it, so just copy that line.
Yeah ok, but do they import it from? Does it just come from nowhere?

That class has no corresponding setXXX() function
Isn't there a way to create new functions ? I can do this all the time in the NWN2 (an RPG) editor... Wasn't the SDK released just for that?

you see why I have a problem with python now? I am . .. .. .. .ing not being lazy, spending tons of hours just trying to understand things.....

I am so upset to have to learn a whole new programming language just because of a tiny code stuff.

But that mod will be so great, I have to do it.....
 
My advice is to post a new thread detailing what you want to do. Give it a topic that tells people what it's about. This thread is not going to attract the same people that might be able to answer your question, possibly.

You can add all the functions you want in Python, but the game needs to make use of them. All of the Python classes are wrappers around the C++ classes that the game core DLL uses. They provide access via Python to the core logic without forcing you to use C++. However, not everything can be done via Python. Many mods create their own DLL by modifying the game's C++ code. This is possible but more work.

Therefore, if you start by stating what you want to have happen, maybe someone has a way to do it.
 
I used your code for adding a religion requirement for certain civics. My friends and I have created a new civic temporarily called "Jihad", and we want to make it require the Islam religion. Snippet:

Code:
def cannotDoCivic(self,argsList):
		ePlayer = argsList[0]
		eCivic = argsList[1]
		religion = "RELIGION_ISLAM"
		civic = "CIVIC_JIHAD"
			## If The Civ Doesn't Have The Correct Religion Tell the SDK Not To Allow The Player To Chose This Civic
        		if eCivic == gc.getInfoTypeForString(civic):
            		if gc.getPlayer(ePlayer).getStateReligion() != gc.getInfoTypeForString(religion):
               		 return True

		return False

The problem is, once I input this code into the mod's Assets/Python/CvGameUtils.py file, and then load a game to test, I cannot see the control panel on the bottom of the screen. Do you know why?
 
Top Bottom