Modding XP

Telecide

Chieftain
Joined
Oct 28, 2005
Messages
29
So in the manual, as an illustration of what python could do, it's suggested that you could make a script that gives a unit experience whenever it enters a particular square. I think that would be a cool thing to do. But I can't find anything referring to experience in any of the python files. Anyone know how you might go about doing this? :confused:
 
Goody huts give XP... that can problably be used. But I can't figure out how. If you do, post how to do it.
 
You have to inherit from CvEventManager like it is (for example) done in CvAmRevEvents.py in the American Revolution Scenario.

There overwrite the method onUnitMove other methods shouldn't be necessary. In this method argsList[1] seems to refer to the unit that has been moved. On this you can call all methods mentioned here (http://civilization4.net/files/modding/PythonAPI/AllClasses.html#CyUnit) especially getX(), getY(), setExperience(int, int).

I hope this helps to get some research going on. If not - ask again.
 
I looked in the python api, but since the link to CyUnit doesn't work and it was in bold in the index I figured it wasn't included... guess I should've looked closer.

Well, thanks for the help, it works.

edit:
There's also changeExperience(int, int). Don't know the diffrence since I haven't tried it. Perhaps experience can't go down if you use that? It can if you use set.
 
Well, thanks for the help, it works.

Good to hear that. :)

There's also changeExperience(int, int). Don't know the diffrence since I haven't tried it. Perhaps experience can't go down if you use that? It can if you use set.

No idea about that one.
 
My American Revolution scenario is a good reference to base one's mods off of (and Desert War too, I suppose ;)). It has a lot of examples of things like this (though some are more complicated than others).
 
To be more specific:

onUnitMove() is definitely the function you need in this case. For your mod you want your own custom CvEventManager.py file and change that function.

Inside of there right now all it does is unpack the arguments passed into the function into things we can understand (like pPlot and pUnit) and prints out the fact that the unit moved.

pPlot and pUnit are objects which have their own functions that can be called. To identify the plot (aka Tile or Square, though "Plot" is the correct term) you can get the X and Y coordinates by calling pPlot.getX() and pPlot.getY(). You could say something like:

Code:
if (pPlot.getX() == 10 and pPlot.getY() == 5):
        blah

"blah" being the code you want to execute if the plot matches what you're looking for. In this case we want to give the unit experience, which is pretty easy to do. We can just call the changeExperience() function on the pUnit object we have.

Code:
if (pPlot.getX() == 10 and pPlot.getY() == 5):
        pUnit.changeExperience(10,100)
I'm not exactly sure on what all the arguments are but I'm fairly confident the first one is how much experience you're adding (or subtracting, in the case of a negative number) to the unit and the second is the MAXIMUM value it can have. I believe this is used when fighting Animal and Barbarian units since you can get a max of 5 and 10 against them, respectively.

And that's it. Only a two-line change to get the XP mod working. :)
 
Trip said:
To be more specific:

onUnitMove() is definitely the function you need in this case. For your mod you want your own custom CvEventManager.py file and change that function.

Inside of there right now all it does is unpack the arguments passed into the function into things we can understand (like pPlot and pUnit) and prints out the fact that the unit moved.

pPlot and pUnit are objects which have their own functions that can be called. To identify the plot (aka Tile or Square, though "Plot" is the correct term) you can get the X and Y coordinates by calling pPlot.getX() and pPlot.getY(). You could say something like:

Code:
if (pPlot.getX() == 10 and pPlot.getY() == 5):
        blah

"blah" being the code you want to execute if the plot matches what you're looking for. In this case we want to give the unit experience, which is pretty easy to do. We can just call the changeExperience() function on the pUnit object we have.

Code:
if (pPlot.getX() == 10 and pPlot.getY() == 5):
        pUnit.changeExperience(10,100)
I'm not exactly sure on what all the arguments are but I'm fairly confident the first one is how much experience you're adding (or subtracting, in the case of a negative number) to the unit and the second is the MAXIMUM value it can have. I believe this is used when fighting Animal and Barbarian units since you can get a max of 5 and 10 against them, respectively.

And that's it. Only a two-line change to get the XP mod working. :)


Awesome! This probably would have taken a lot longer to figure out than I thought.
 
Telecide said:
Awesome! This probably would have taken a lot longer to figure out than I thought.

Hmm... well I made copy of CvEventManager.py and put it into the custormassests\python folder. I copied and pasted that code at the end of the onUnitMove event and set both coordinates one higher than the starting location of one of the maps. I figure that should be down and to the right. No luck yet though... :(

I'm kinda new to this python stuff.
 
If you hold down SHIFT and hover your cursor over a plot it will give you the X and Y coordinates. May need to set the cheat code for that but I'm not sure.
 
Ah, the man who made the mod I gained my knowledge from :D .

2 questions:

1. Why did you make a subclass of CvEventManager? To make the code smaller? To retain the debug output?

2. When I tried to imitate your event-handling-stuff I first got strange loading errors until (so it seemed to me) I gave the file/class the prefix Cv... . Is this really needed or was this some different error in disguise?

Thanks.
 
Finally figured it out. I put the code in after the return function so it was never being run. Works like a charm now. :) It looks like the second number in parameter of the changeExperience method is the max one unit can get from that particular plot.
 
Back
Top Bottom