Python (civ4 scripting language) tutorial thread

Hi all!

Welcome to all the new posters here, and thank you to all those who answered their questions.

Again, Real-Life time constraints restrict me from doing the modding/writing about modding that I would like to do. I don't have any predicted time in which I may release any more material, but needless to say I haven't forgotten about it all, and I do intend to (eventually) recommence contributing to the modding community.

Thanks for your patience.
 
Hello!

I try to import tkinter in my script but that doen't work !

In PythonDbg.log there is: EntryPoint module:tkinter
but no : load_module tkinter

Is there a way to import tkinter in civ IV ?

Tcho !
 
wow... you do a realy good work here... hey man you are my hero!!!... I can translate this tutorial to Portuguese??? My "contarreaneos" need learn about this... tks!!!
 
Tanx much -- I'm an old programmer with little patience for reading wiki or manuals -- this was a great intro and I'm off and running. Tanx again
 
Hi; I have a module which gets imported into CvGameUtils.py. The module defines a new class and declares some global variables of that class. However, in the cannotConstruct method of CvGameUtils, the global is not properly initialized the first time it is accessed. What's up?

Thanks,
Eusebius
 
Hi; I'm working on my world religions mod and ran into an odd case of inheritance not working as I expect it to. {Doubtless because I am a Python newbie.} I think that the following code ought to work purely through inheritance, but instead I had to overload the method name 'getSpreadFactor'. Can anyone set me straight?

foo = ewrReligionInfo(1)
bar = foo.getSpreadFactor()

Thanks,
Eusebius

PS. Mod forum is http://forums.civfanatics.com/showthread.php?p=3950103

class ewrReligionInfo(CvReligionInfo):
def __init__(self, iReligionIndex):
self.parent = CvReligionInfo
self.parent.__init__(self)
self.iReligionID = iReligionIndex
self.CvReligionInfo = CvReligionInfo
self.iTempleID = gc.getInfoTypeForString('BUILDING_TP_PAGAN_TEMPLE')
self.iMonasteryID = -1
self.iCathedralID = -1
self.iShrineID = -1
self.iMissionaryID = -1
self.iFoundingID = -1
self.bSingleCiv = False
self.bIsWorldview = True
self.bAdvancedPaganism = False
self.bHatesIdols = False
self.bHasIdols = False
self.bHasMovie = False
if (iReligionIndex >= 0):
self.CvReligionInfo = gc.getReligionInfo(iReligionIndex)
<code...>


######################################################################
# I can't figure out why these shouldn't work through inheritance,
# but it always returns zero if I don't do it this way.
######################################################################
def getSpreadFactor(self):
return self.CvReligionInfo.getSpreadFactor()
 
i've looked at your first 4 lessons and i think they're great! I was wondering though is there going to be a lesson on drawing using python(or maybe functions that are related to makeing graphics or placing them on the screen) . I know its a bit advanced but i would like to see a lesson on this in the near future.
 
After I read up to lesson four, I tried to make a small program, but I got a syntax error. Can anyone tell me where I screwed up? Edit, see below.

Code:
#Eat twinkies, pal!

NumberOfTwinkies = 10


while twinkies >= 0
    print "We still have", NumberOfTwinkies, "twinkies! Must devour!"
    NumberOfTwinkies = NumberOfTwinkies -1
    print "Munch!"
    if NumberOfTwinkies == 1
        print "Oh no, the last twinkie!"
        NumberOfTwinkies = NumberOfTwinkies - 1
        print "Must... devour... last... twinkie!!! Munch!"

Edit: Ok, I think I found the first problem. When Gingerbreadman used multiple prints in the same line, he put a + before them. I've now done the same, but I'm still getting a syntax error.

Code:
#Eat twinkies, pal!

NumberOfTwinkies = 10


while twinkies >= 0
    print "We still have", + NumberOfTwinkies, + "twinkies! Must devour!"
    NumberOfTwinkies = NumberOfTwinkies -1
    print "Munch!"
    if NumberOfTwinkies == 1
        print "Oh no, the last twinkie!"
        NumberOfTwinkies = NumberOfTwinkies - 1
        print "Must... devour... last... twinkie!!! Munch!"

Another edit, oh boy. When I switched from twinkies to NumberOfTwinkies as my variable, I forgot to make the while thing switch too. I've fixed that now as well, but I'm still getting a syntax error. I must have screwed up royally on this little peice of code.

Code:
#Eat twinkies, pal!

NumberOfTwinkies = 10


while NumberOfTwinkies >= 0
    print "We still have", + NumberOfTwinkies, + "twinkies! Must devour!"
    NumberOfTwinkies = NumberOfTwinkies -1
    print "Munch!"
    if NumberOfTwinkies == 1
        print "Oh no, the last twinkie!"
        NumberOfTwinkies = NumberOfTwinkies - 1
        print "Must... devour... last... twinkie!!! Munch!"
 
Lets see if I can help... Here's my take on it:
Code:
NumberOfTwinkies = 10


while NumberOfTwinkies > 0:
    if NumberOfTwinkies == 1:
        print &quot;Oh no, the last twinkie!&quot;
        print &quot;Must... devour... last... twinkie!!! Munch!&quot;
    else:
        print &quot;We still have&quot;, NumberOfTwinkies, &quot;twinkies! Must devour!&quot;
        print &quot;Munch!&quot;
    NumberOfTwinkies = NumberOfTwinkies -1
There were a few issues:

The way you structured the program was a bit confused. Try writing out what each line of code will do in plain english first, if that helps. For example, I approached it this way:
Code:
While there's twinkies left:
    If its the last twinkie:
        Print the &quot;Oh noes&quot; message
    otherwise:
        print the normal message
    take away one twinkie
You need to put colons at the end of if, else and while statements. It pretty much works like this:
Code:
if this happens[B]:[/B]
    Do this
else[B]:[/B]
    Do this
while this is true[B]:[/B]
    Do this
Where you had the following:
Code:
    print &quot;We still have&quot;, + NumberOfTwinkies, + &quot;twinkies! Must devour!&quot;
you were confusing a couple of things. First of all, you don't need a comma and a plus sign. One or the other. Second thing, you can only use the plus sign when all of the things you are adding or joining are of the same type, e.g. adding a string to a string, adding a number to a number. Think of a comma as indicating yet another print statement, but putting it on the same line as the previous one.
 
Thank you! Ok, you did say to put colons after ifs and elses, so that was just me being stupid. However, I didn't see anything about how to use + and commas in the section where you used them. I would suggest putting something there about it, or linking to a later article if you do explain that somewhere. Edit: After looking around, I noted a few places where you did say that, that I had already read. You should make a bigger point of that when you use it in an example though.) Once I did both of those, I got:

Code:
#Eat twinkies, pal!

NumberOfTwinkies = 10


while NumberOfTwinkies > 0:
    print "We still have", NumberOfTwinkies, "twinkies! Must devour!"
    NumberOfTwinkies = NumberOfTwinkies -1
    print "Munch!"
    if NumberOfTwinkies == 1:
        print "Oh no, the last twinkie!"
        NumberOfTwinkies = NumberOfTwinkies - 1
        print "Must... devour... last... twinkie!!! Munch!"

We still have 10 twinkies! Must devour!
Munch!
We still have 9 twinkies! Must devour!
Munch!
We still have 8 twinkies! Must devour!
Munch!
We still have 7 twinkies! Must devour!
Munch!
We still have 6 twinkies! Must devour!
Munch!
We still have 5 twinkies! Must devour!
Munch!
We still have 4 twinkies! Must devour!
Munch!
We still have 3 twinkies! Must devour!
Munch!
We still have 2 twinkies! Must devour!
Munch!
Oh no, the last twinkie!
Must... devour... last... twinkie!!! Munch!

:D

You were also right about using the greater than, instead of greater than or equal to (it went to "0 twinkies left, must devour!"). It wasn't a syntax problem though (just me being stupid), so at least it would have worked enough for me to see the problem and fix it.

And now I can get a program to do 99 bottles of beer on the wall. :)
 
Glad I could help :)

When I next have free time in my life (possibly never), I might get around to updating my website. I've currently put it on my own server which I have full control over, so there's quite a lot more I can do.
 
Ok, one more quick question. I'm trying to use the read/write stuff from lesson 10. Here's the code I wrote:

Code:
#Readwrite.py
#Attempting to read Rw.txt (doc), add "This is a string", then read it again
#to confirm that it was added
doc = open('C:\Documents and Settings\Dimitri\Desktop\Rw.txt', 'r+')
print doc.read()
doc.write('This is a string')
print doc.read()
doc.close()

When I ran it I got:

Code:
>>> 
If you can read this you have found Rw.txt

[COLOR="Red"]Traceback (most recent call last):
  File "C:/Program Files/Python 2.3.4/test", line 6, in -toplevel-
    doc.write ('This is a string')
IOError: (0, 'Error')[/COLOR]

I know I screwed up with writing, but I don't know what to do to make it work.

Edit: Ok, someone else spotted some syntax errors for me, so I corrected that. I'm still getting the same error though.
 
I'm not sure if this counts as modding, but...
How do all the people that do unit reskins do it? Where are the .dds files? I have a converter and and have reskinned one that I found, but that's the only one! WHERE ARE THEY???
 
ggganz - Hmm, I only really know about python programming, and even then, I'm only really useful for beginners. Maybe try asking somebody else?
 
ggganz - on this thread, there's only me, and maybe a couple of other pythoneers. I'm sure there's plenty of graphics experts who will be able to help you, in another thread.
 
Back
Top Bottom