Sample Python Code

OK, now I could be wrong, but I believe it is short for "Argument List". As a non-professional Coder, I believe it lists the situations for which a line of code might be the case. A proper coder could tell you what it really means though.
 
Hey I keep reading in the codes you put up, "arglist". What's an Arglist?
As Aussie said, it is an "argument list". It is similar to argument unpacking, except what Firaxis did was define each function as accepting a single argument, a list, which contains all of the real arguments.

So your normal Python function might look like this:
Code:
def cannotTrain(pCity, eUnit, bContinue, bTestVisible):
    # do something
    return False

# And would call the function like this
if cannotTrain(pCity, eUnit, bContinue, bTestVisible):
    pass

Whereas Firaxis did this:
Code:
def cannotTrain(argsList):
    pCity = argsList[0]
    eUnit = argsList[1]
    bContinue = argsList[2]
    bTestVisible = argsList[3]
    # do something
    return False

# And would call the function like this
# Pack the list first
argsList = (pCity, eUnit, bContinue, bTestVisible)

# Then call it
if cannotTrain(argsList):
    pass

But both work essentially the same way. The first is cleaner, but perhaps they had their reasons (like integration with C++) for defining each function as accepting only a list. It also would have been more Pythonic to unpack it like this:

Code:
def cannotTrain(argsList):
    pCity, eUnit, bContinue, bTestVisible = argsList
    # do something
    return False

EDIT: It looks like they did do that simplified unpacking in some places. I'm still trying to work out which stuff they put in C++ and which in Python, and why the Python API is the way it is.
 
In python if you define a function like this
Code:
def blah(*lArgs):
        pass

then whatever arguments you give to the function will be put in that argument list.

For example blah(1, 9, 6) would pass the function a list like [1, 9, 6]. So you can pass 0 to N arguments without changing the function def line, which can be good for things with lots of args or with args that are changing alot.
 
Thanks very much for explaining this stuff.

So if I get this right, argument lists are lists defined by the written function to store the variables for that function?
 
@mrkingkong

Thanks for your snippet. I have used it as a basis for my my first succesful foray into python - causing any unit defeated by a zombie to rise up as a zombie itself! Having clear digestible chunks of code here is a godsend, as previously I have found an enormous gulf between basic python tutorials and the vast array of code contained in a typical civ4 python file. This thread helps to bridge that gulf so I just want to express my appreciation to all contributors.

PS Any more FFH functions anybody wants to put up and explain would be most welcome (*Cough*Diseased units*Cough*)
 
This is my very first Python mod.
It's a series of event functions called from the eventinfos.xml so naturally it goes in the CvRandomEventInterface.py file.


Code:
def betaReligion1(argsList):
	# CyInterface().addImmediateMessage("betaReligion1 function called", "")

	kTriggeredData = argsList[1]
	player = gc.getPlayer(kTriggeredData.ePlayer)
	game = gc.getGame()
	
	ifailuretest = game.getSorenRandNum(100, "")

	if ifailuretest < 70:
			
		(loopCity, iter) = player.firstCity(false)

		while(loopCity):

			loopCity.changeHurryAngerTimer(10)
			(loopCity, iter) = player.nextCity(iter, false)
		
		messagestring = "Our suppression of this dangerous cult has been successfull."
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("Our suppression of this dangerous cult has been successfull.", "")
		return
		
	unfounded = []
	
	for iReligion in range(gc.getNumReligionInfos()):
		if not game.isReligionFounded(iReligion):
			unfounded.append(iReligion)
			
	if len(unfounded) > 0:
		unfounded.sort()
		ifound = unfounded[0]
		player.foundReligion(ifound, ifound, True)

		(loopCity, iter) = player.firstCity(false)

		while(loopCity):
			loopCity.changeHurryAngerTimer(10)
			loopCity.changeHurryAngerTimer(10)
			(loopCity, iter) = player.nextCity(iter, false)

		messagestring = "Our failed attempt to suppress this budding new religion has caused widespread unhappiness."
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("Our failed attempt to suppress this budding new religion has caused widespread unhappiness.", "")
		return
		
	messagestring = "Extreme measures have proved unnecessary. The new sect has failed to gain a widespread following, it is already fading into obscurity."
	CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
	return
	
def betaReligion2(argsList):
	# CyInterface().addImmediateMessage("betaReligion2 function called", "")

	kTriggeredData = argsList[1]
	player = gc.getPlayer(kTriggeredData.ePlayer)
	game = gc.getGame()

	ifailuretest = game.getSorenRandNum(100, "")
	
	if ifailuretest < 20:
		
		messagestring = "The new sect has failed to gain a widespread following. It is already fading into obscurity."
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("The new sect has failed to gain a widespread following. It is already fading into obscurity.", "")
		return

	unfounded = []
	
	for iReligion in range(gc.getNumReligionInfos()):
		if not game.isReligionFounded(iReligion):
			unfounded.append(iReligion)
			
	if len(unfounded) > 0:
		unfounded.sort()
		ifound = unfounded[0]
		player.foundReligion(ifound, ifound, True)
			
		(loopCity, iter) = player.firstCity(false)

		while(loopCity):
			loopCity.changeHappinessTimer(10)
			(loopCity, iter) = player.nextCity(iter, false)

		messagestring = "The new sect is rapidly gaining a widespread following. The people are gratefull for your tolerence."
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("The new sect is rapidly gaining a widespread following. The people are gratefull for your tolerence.", "")
		return
	
			
	messagestring = "The new sect has failed to gain a widespread following. It is already fading into obscurity."
	CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
	return

def betaReligion3(argsList):
	# CyInterface().addImmediateMessage("betaReligion3 function called", "")

	kTriggeredData = argsList[1]
	player = gc.getPlayer(kTriggeredData.ePlayer)
	game = gc.getGame()
	
	# player.changeGold(-10)

	ifailuretest = game.getSorenRandNum(100, "")
	
	if ifailuretest < 10:
		
		messagestring = "Despite our support, the new sect has failed to gain a widespread following. It is already fading into obscurity"
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("Despite our support, the new sect has failed to gain a widespread following. It is already fading into obscurity", "")
		return
	
	unfounded = []
	
	for iReligion in range(gc.getNumReligionInfos()):
		if not game.isReligionFounded(iReligion):
			unfounded.append(iReligion)
			
	if len(unfounded) > 0:
		unfounded.sort()
		ifound = unfounded[0]
		player.foundReligion(ifound, ifound, True)
		
		(loopCity, iter) = player.firstCity(false)

		while(loopCity):
			# loopCity.changeExtraHappiness(2)
			loopCity.changeHappinessTimer(20)
			(loopCity, iter) = player.nextCity(iter, false)
		messagestring = "Your generous support for this budding new religion has met with widespread approval. Our people's spiritual lives will be greatly enhanced by this flourishing new faith."
		CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
		# CyInterface().addImmediateMessage("Your generous support for this budding new religion has met with widespread approval. Our people's spiritual lives will be greatly enhanced by this flourishing new faith.", "")
		return
		
	messagestring = "Despite our support, the new sect has failed to gain a widespread following. It is already fading into obscurity."
	CyInterface().addMessage(kTriggeredData.ePlayer, false, gc.getEVENT_MESSAGE_TIME(), messagestring, "", InterfaceMessageTypes.MESSAGE_TYPE_INFO, None, gc.getInfoTypeForString("COLOR_WHITE"), -1, -1, true, true)
	return

What it does:
betaReligion1 represents an attempt to suppress an emerging new religion with a 70% chance of success.
If successful it causes moderate unhappiness throughout the player's empire.
If unsuccessful, the new religion is founded anyway but the player suffers even more unhappiness
betaReligion2 basically represents the government doing nothing to either help or hinder the new religion, giving the religion a 20% chance of failing on it's own
betaReligion3 represents active support of the new religion, reducing the religion's failure chance to 10%
the foundReligion section is specifically coded to found religions in the order in which they appear in the ReligionInfo.xml file and should be able to cope with any number of religions (i've tried it with about 9)

Use:
To make the founding of religions tied to random events.
To achieve this I had to set the prereq tech of all the religions to tech_future_tech because I have thus far been unable to figure out how to completely remove the need for prereqtechs

of course I haven't really done any of the help texts to go with this yet, maybe I'll come back to it in a couple of weeks and release it as a full modcomp
 
Back
Top Bottom