python randomization - what's wrong here?

This is going to sound dumb, but try using iRnd instead of rand as the variable name. I have had problems where I was trying to use a variable name and it wouldn't work because that name was already reserved for use for something internal.
 
Saarlaender said:
(another strange thing: even after clearing the cache, it keeps screaming about undefined taoist units that I took out in BuildingINfos - which no loonger contains this tag.)

If it's for Taoist buildings, possibly the entries you missed are in BuildingClassInfos?

but still; the religions in question were not the ones involved in the spread process. does this make the whole routine not work? the ones I needed in my trial (iJewish and iCatholic) were right.

If the script ran across Hinduism or Protestantism before it got to Jewish and Catholic, it would have stopped.

Nice trick for debugging this stuff - run in windowed mode. You can have another couple of windows in the background looking at the error log and debug log. If you find a bug, change the Python file and save it - when you shift focus back to Civ's window, it'll detect the change and reload python.
 
I fixed the isms, and added this line for verification(might be horridly wrong) at the end:
Code:
CvUtil.pyPrint('% value'
			%(iRnd))

no religion loss, no number printed. Ignores it.
 
Kael said:
This is going to sound dumb, but try using iRnd instead of rand as the variable name. I have had problems where I was trying to use a variable name and it wouldn't work because that name was already reserved for use for something internal.
This is a very good point. I would suggest coding in a program that regonises internal commands and highlights them in a different colour - avoids such trip-ups. I think Kael might be right with the dyagnosis of the problem.

As for printing the random number, the correct syntax should be:
Code:
CvUtil.pyPrint("%d value"%(iRnd))
Note the %d. It would seem that %d should be used when the thing to be printed is a numeral (maybe it stands for decimal? I don't know), and %s should be used if it's a string.
Code:
print iRnd
should work as well.

Just out of interest, does anybody know what CvUtil.pyPrint does that print doesn't do?
 
Saarlaender said:
changed all rand tags to iRnd, didn't seem to change anything. I'll try the new print code.

Can you zip up and attached your CIV4ReligionInfo.xml file so I can check it out.
 
Try this one instead.
 
Not sure what you changed (apart from cleaning up my mess), but I replaced it. No noticeable effect, though.
it still doesn't print the iRnd numbers, it still doesn't delete religions....

(But a weird thing I noticed: A city that had a religion became jewish on top of it through normal spread (from a different civ). Thought that wasn't posible?)
 
Saarlaender said:
Not sure what you changed (apart from cleaning up my mess), but I replaced it. No noticeable effect, though.
it still doesn't print the iRnd numbers, it still doesn't delete religions....

(But a weird thing I noticed: A city that had a religion became jewish on top of it through normal spread (from a different civ). Thought that wasn't posible?)

I fixed a bunch of white space errors. Extra returns, incorrect tabbings, things like that. A city can gain all of the religions through normal spread. Having one religion doens't stop it form getting new religions.
 
if thats a problem, then i have tons over tons of those in all the many files i modded. still, all of that seems to work out so far. it's just this onReligionSpread thing...
 
Oneother idea, though probably nothing:

The usual civ text gets printed when a religion comes to a city. so some function is called, and the error messages proved it was actually my onReligionSpread routine. one little thing: I have the German version and the text is always in German. Yet, the print command in my EventManager has this command in English:
Code:
		CvUtil.pyPrint('%s has spread to Player %d Civilization %s city of %s'
			%(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pSpreadCity.getName()))

how can this be? does it get overridden somewhere else? where is that somewhere else?
 
Saarlaender said:
I have the German version and the text is always in German. Yet, the print command in my EventManager has this command in English:

how can this be? does it get overridden somewhere else? where is that somewhere else?

Those print commands are debugging information for the english-speaking programmers. They never appear as part of the game, just in Logs/PythonDbg.log

Kael said:
A city can gain all of the religions through normal spread. Having one religion doens't stop it form getting new religions.

Are you sure? I've heard otherwise, although it could be an urban legend, butI don't think I've ever seen it...

I suppose the quick way to check would be to delete the missionary units, run a game, and see if any non-holy-city ever gets multiple religions...
 
could of course be that the AI sent a missionary, though I have yet to see that i n any game.
In any case, the print prints text in the LOG?? .. then how do I print on screen in the game?

And I checked the logs, no sign of my random number being printed there either (not that I really got everything there, but there's no number that could be the iRnd)
 
too many if' in the original version, I guess! I had five equal if calls after one another. that must have killed it. cleaned it up, this seems to work (with one exception)


Code:
def onReligionSpread(self, argsList):
		'Religion Has Spread to a City'
		iReligion, iOwner, pSpreadCity = argsList
		iCatholic = gc.getInfoTypeForString('RELIGION_CHRISTIANITY')
		iProtestant = gc.getInfoTypeForString('RELIGION_PROTESTANTISM')
		iJewish = gc.getInfoTypeForString('RELIGION_JUDAISM')
		iHindu = gc.getInfoTypeForString('RELIGION_HINDUISM')
		iPolytheism = gc.getInfoTypeForString('RELIGION_POLYTHEISM')
		iIslam = gc.getInfoTypeForString('RELIGION_ISLAM')
		player = PyPlayer(iOwner)
		pPlayer = gc.getPlayer(iOwner)
		iRandLoss = CyGame().getSorenRandNum(100, "Religion Loss")

		if iReligion == iCatholic:
			if (pSpreadCity.isHasReligion(iProtestant) and pSpreadCity.isHolyCityByType(iProtestant) == False):
				if (iRandLoss <= 45):
					pSpreadCity.setHasReligion(iProtestant, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Protestanen sind zur Heiligen Kirche zurückgekehrt!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)

			if (pSpreadCity.isHasReligion(iIslam) and pSpreadCity.isHolyCityByType(iIslam) == False):
				if (iRandLoss <= 30):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime der Stadt sind katholisch geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iHindu) and pSpreadCity.isHolyCityByType(iHindu) == False):
				if (iRandLoss <= 45):				
					pSpreadCity.setHasReligion(iHindu, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Hindus sind zur Heiligen Kirche konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
		
			if (pSpreadCity.isHasReligion(iJewish) and pSpreadCity.isHolyCityByType(iJewish) == False):
				if (iRandLoss <= 50):				
					pSpreadCity.setHasReligion(iJewish, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden de Stadt sind jetzt katholisch!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iPolytheism) and pSpreadCity.isHolyCityByType(iPolytheism) == False):
				if (iRandLoss <= 85):
					pSpreadCity.setHasReligion(iPolytheism, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Heiden sind zur katholischen Kirche konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			


		if iReligion == iProtestant:
			
			if (pSpreadCity.isHasReligion(iIslam) and pSpreadCity.isHolyCityByType(iIslam) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime sind protestantische Christen geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Polytheism.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iHindu) and pSpreadCity.isHolyCityByType(iHindu) == False):
				if (iRandLoss <= 40):				
					pSpreadCity.setHasReligion(iHindu, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Hindus habe Jesus anerkannt, aber nicht den Papst!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Polytheism.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iCatholic) and pSpreadCity.isHolyCityByType(iCatholic) == False):
				if (iRandLoss <= 30):				
					pSpreadCity.setHasReligion(iCatholic, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Reformation! Die Kathoiken der Stadt sind Protestanten geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Polytheism.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iJewish) and pSpreadCity.isHolyCityByType(iJewish) == False):			
				if (iRandLoss <= 50):				
					pSpreadCity.setHasReligion(iJewish, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden der Stadt sind evangelisch geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Polytheism.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iPolytheism) and pSpreadCity.isHolyCityByType(iPolytheism) == False):
				if (iRandLoss <= 70):
					pSpreadCity.setHasReligion(iPolytheism, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Heiden sind zur evangelischen Kirche konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
		if iReligion == iPolytheism:
			
			if (pSpreadCity.isHasReligion(iIslam) and pSpreadCity.isHolyCityByType(iIslam) == False):
				if (iRandLoss <= 10):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime haben die Opfer an Zeus wiederaufgenommen!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iCatholic) and pSpreadCity.isHolyCityByType(iCatholic) == False):
				if (iRandLoss <= 10):				
					pSpreadCity.setHasReligion(iCatholic, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Katholiken haben die Kulttempel wieder geöffnet!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iProtestant) and pSpreadCity.isHolyCityByType(iProtestant) == False):
				if (iRandLoss <= 10):				
					pSpreadCity.setHasReligion(iProtestant, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Protestanen sind zum Heidentum konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iHindu) and pSpreadCity.isHolyCityByType(iHindu) == False):
				if (iRandLoss <= 30):				
					pSpreadCity.setHasReligion(iHindu, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Hindus haben Zeus als König der Götter erkannt!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iJewish) and pSpreadCity.isHolyCityByType(iJewish) == False):	
				if (iRandLoss <= 15):				
					pSpreadCity.setHasReligion(iJewish, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden haben Zeus als König der Götter erkannt!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)

			
		if iReligion == iIslam:
			if (pSpreadCity.isHasReligion(iCatholic) and pSpreadCity.isHolyCityByType(iCatholic) == False):
				if (iRandLoss <= 25):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Katholiken sind Muslime geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iProtestant) and pSpreadCity.isHolyCityByType(iProtestant) == False):
				if (iRandLoss <= 25):				
					pSpreadCity.setHasReligion(iProtestant, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Protestanten sind Muslime geworden!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iHindu) and pSpreadCity.isHolyCityByType(iHindu) == False):
				if (iRandLoss <= 45):				
					pSpreadCity.setHasReligion(iHindu, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Hindus sind zum Islam konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iPolytheism) and pSpreadCity.isHolyCityByType(iPolytheism) == False):
				if (iRandLoss <= 80):
					pSpreadCity.setHasReligion(iPolytheism, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Kultreligion macht Platz für den Islam!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			

			if (pSpreadCity.isHasReligion(iJewish) and pSpreadCity.isHolyCityByType(iJewish) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iJewish, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime sind zum Judentum konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
		if iReligion == iJewish:
		
			if (pSpreadCity.isHasReligion(iIslam) and pSpreadCity.isHolyCityByType(iIslam) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime sind zum Judentum konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				
			
			if (pSpreadCity.isHasReligion(iCatholic) and pSpreadCity.isHolyCityByType(iCatholic) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iCatholic, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Katholiken sind zum Judentums konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				
			if (pSpreadCity.isHasReligion(iProtestant) and pSpreadCity.isHolyCityByType(iProtestant) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iPotestant, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Protestanen sind zum Judentum konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				

			if (pSpreadCity.isHasReligion(iHindu) and pSpreadCity.isHolyCityByType(iHindu) == False):
				if (iRandLoss <= 30):				
					pSpreadCity.setHasReligion(iHindu, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden sind zum Hinduismus konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
	
			if (pSpreadCity.isHasReligion(iPolytheism) and pSpreadCity.isHolyCityByType(iPolytheism) == False):
				if (iRandLoss <= 80):
					pSpreadCity.setHasReligion(iPolytheism, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Muslime haben die Opfer für Zeus wiederaufgenommen!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
		if iReligion == iHindu:
		
			if (pSpreadCity.isHasReligion(iIslam) and pSpreadCity.isHolyCityByType(iIslam) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iIslam, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden sind zum Hinduismus konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				
			
			if (pSpreadCity.isHasReligion(iCatholic) and pSpreadCity.isHolyCityByType(iCatholic) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iCatholic, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Katholiken sind zum Hinduismus konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				
			if (pSpreadCity.isHasReligion(iProtestant) and pSpreadCity.isHolyCityByType(iProtestant) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iProtestant, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Protestanten sind zum Hinduismus konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
				

			if (pSpreadCity.isHasReligion(iJewish) and pSpreadCity.isHolyCityByType(iJewish) == False):
				if (iRandLoss <= 20):				
					pSpreadCity.setHasReligion(iJewish, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Juden sind zum Hinduismus konvertiert!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
			if (pSpreadCity.isHasReligion(iPolytheism) and pSpreadCity.isHolyCityByType(iPolytheism) == False):
				if (iRandLoss <= 40):
					pSpreadCity.setHasReligion(iPolytheism, False, False, False)
					CyInterface().addMessage(CyGame().getActivePlayer(),True,25,'Die Tempel werden dem Hinduismus neu geweiht!','AS2D_RELIGION_CONVERT',1,'Art/Interface/Buttons/Religions/Catholic.dds',ColorTypes(8),pSpreadCity.getX(),pSpreadCity.getY(),True,True)
			
					
		CvUtil.pyPrint('%s has spread to Player %d Civilization %s city of %s'
			%(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pSpreadCity.getName()))

Every once in a while, I get an exception call though:
unicode decode error - can't decode bythe 0xfc in position 29: ordinal not in range (128). don't know what that means...
 
Try replacing all you strings that are '...' with u'...'. This will make them unicode strings and I think will let you use the special characters you are using.
 
Back
Top Bottom