Merging events from event manager

keldath

LivE LonG AnD PrOsPeR
Joined
Dec 20, 2005
Messages
7,362
Location
israel
Hi bug team,

ill ask something that is probably been answered here quite a lot.

i have some events i got, in eventmanager file , im currently using platyping code, that has eventmanager,
but i decided to go back to bug based (from kmod).

im not sure how or where to put the code and how to call it,
example - the code for East India Company Start building i added in :

def onEndPlayerTurn(self, argsList):
'Called at the end of a players turn'
iGameTurn, iPlayer = argsList
## East India Company Start ##
pPlayer = gc.getPlayer(iPlayer)
if pPlayer.getBuildingClassCount(gc.getInfoTypeForString("BUILDINGCLASS_EAST_INDIA_COMPANY")) == 1:
if iGameTurn % 8 == 0:
(loopCity, iter) = pPlayer.firstCity(false)
while(loopCity):
if loopCity.getAddedFreeSpecialistCount(gc.getInfoTypeForString("SPECIALIST_GREAT_MERCHANT")) > 0:
pNewUnit = pPlayer.initUnit(gc.getInfoTypeForString("UNIT_CARAVAN"), loopCity.getX(), loopCity.getY(), UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION)
(loopCity, iter) = pPlayer.nextCity(iter, false)
## East India Company End ##
i read that i gotta load it vis the init.xml file in the bug folder,
but where do i out this code then? if bug does not use eventmanager, but thebugevenmanager instead of cvcustommanager.
i dont know how to call or write new python , is there a template i can use for such codes?
cause i have quite a lot of these.

i hope someone will answer :)

thanks in advance.


 
Hi Keldath,

I have some code snippets left from the crash, so I'll see if I can help. I did the same thing that you are trying to do with the Female Unit Mod I added to the BAT Mod. My suggestion is to do everything in a modular fashion:

1. Define all of your XML and your art if necessary, and place them in a folder inside the \Modules subfolder in Assets
2. Write your Python code as a separate module. Take only the function that you are going to use (In my case, OnUnitBuilt) and keep it in a file all by itself. Here is the Female Unit code as a BUG module:

Code:
## Female CEO Mod.py  08/22/10 - Lemon Merchant
## Allows the use of Saibotlieh's Female Missionary and CEO Units in the BAT Mod
## This is a BUG module, and requires the BUG mod for use.


from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import PyHelpers
import Popup as PyPopup
import CvCameraControls
import CvTopCivs
import sys
import CvAdvisorUtils
import CvTechChooser

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo


def onUnitBuilt(argsList):
        'Unit Completed'
        city = argsList[0]
        unit = argsList[1]
        player = PyPlayer(city.getOwner())
        iplayer = gc.getPlayer(city.getOwner())
        
# Female CEO begin

        iUnitType = unit.getUnitType()
        UnitInfo = gc.getUnitInfo(iUnitType)
        sUnitType = UnitInfo.getType()
    
        if UnitInfo.getDefaultUnitAIType() == gc.getInfoTypeForString('UNITAI_MISSIONARY'):
            sUnitBuilt = gc.getInfoTypeForString(sUnitType[:20])
            
            if sUnitBuilt == "UNITCLASS_EXECUTIVE_":
                sCEOType = gc.getInfoTypeForString(sUnitType[20:])
                iFemaleUnitType = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),sUnitBuilt+sCEOType+'_FEMALE')
                
            else:       
                sFemaleUnitType = 'UNIT_FEMALE'+sUnitType[4:]
                iFemaleUnitType = gc.getInfoTypeForString(sFemaleUnitType)
            
            iRnd = CyGame().getSorenRandNum(100, "female CEO")
            if iplayer.isCivic(gc.getInfoTypeForString("CIVIC_EMANCIPATION")):
                iRnd -= 35
            if iRnd <= 20:    ## Changed from 15 to 20 - better chance of generating female unit without Emancipation
                oldunit = unit               
                pFemaleUnit = iplayer.initUnit(iFemaleUnitType,oldunit.getX(),oldunit.getY(),UnitAITypes.NO_UNITAI,DirectionTypes.DIRECTION_SOUTH)
                pFemaleUnit.convert(oldunit)
                if oldunit.getGroup().isAutomated():
                    pFemaleUnit.getGroup().setAutomateType(AutomateTypes.AUTOMATE_RELIGION)
                oldunit.kill(false,oldunit.getOwner())
                
# End Female CEO code

        #CvAdvisorUtils.unitBuiltFeats(city, unit)

3. So let's say that you were going to call your little bit of code (or function in this case) Caravan.py You would put this bit of code into the Contrib folder under the Python folder with all of the other BUG Python files.

4. Then you find the init.xml file in \assets\contrib, and where it talks about the MODS, add a line to the end of that XML block that says:

<load mod="Caravan"/>

BUG modding is actually pretty simple, and my code above is essentially doing pretty much the same as what your code snippet does. You, obviously, are going to have to adjust a few things, but as long as you have all of the XML defined and the art defined somewhere in the Modules folder, the modification should work if you have no Python errors.

Give it a try and let me know. :)
 
hi lemon!

thank you so much,
it seems simpler indeed with your example.

i tried it on a simple code - starting pop up.

now when the game loads - i get an error that it cant find the xml for the mods name ive put in the init.

i looked into it -
i think there should be an xml file for the code , in this structure - i took the reminder.xml file as an example


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
Reminder
by eotinb, EmperorFool, Ruff_Hi

Copyright (c) 2008 The BUG Mod.
-->
<mod id="Reminder"
module="ReminderEventManager"
name="Reminder"
author="eotinb, EmperorFool, Ruff_Hi"
version="1.0"
date="11/29/2005"
url="http://forums.civfanatics.com/showthread.php?t=144612">

<options id="Reminder" file="Reminder.ini">
<section id="Reminder">
<option id="Enabled"
type="boolean" default="True"
get="isEnabled" set="setEnabled"
label="Enable Reminders [ALT + M]"
help="Turn on eotinb's Reminders mod."/>
<list id="DisplayMethod" key="Display Method"
type="int" default="2" listType="string"
get="getDisplayMethod" set="setDisplayMethod">
<choice id="Message" get="isShowMessage"/>
<choice id="Popup" get="isShowPopup"/>
<choice id="Both" get="isShowMessage isShowPopup"/>
</list>
<option id="Autolog"
type="boolean" default="True"
get="isAutolog" set="setAutolog"/>
</section>
</options>

<events/>
<shortcut key="Alt Ctrl R | Alt M" function="createReminder"/>

</mod>


i need to do something like that?
its not clear...

thank!
 
Oops. I forgot that you need a Fem CEO Mod.XML in the Contrib folder (same folder as init.XML) for my example to work as written. In the case that I showed, the file would contain this:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Fem CEO Mod - Displays female great people   -->
<mod id="Fem CEO Mod" module="Fem CEO Mod">
    <event type="unitBuilt" function="onUnitBuilt"/>
    
</mod>

You'll have to adapt that with the proper file name, and edit the contents of the XML file to reflect your parameters. Let's say you called your little function "Caravan" as I described in my last post... You would make Caravan.XML and it would contain this:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Caravan - Keldath's little function   -->
<mod id="Caravan" module="Caravan">
    
<!-- Make whatever event type function below as the one you're intercepting -->
<event type="unitBuilt" function="onUnitBuilt"/>
    
</mod>

Sorry about that.
 
its ok LM,
i figured it out by looking at other mods and bug it self.

but - despite that,
im getting an error upon game start that says syntax issue.

i tried to merge into bug - popup python code from the_j mods,
its using the "ongamestart" functions in the eventmanager.

u added the new code with the def gamestart function in the separated new py i made.
what can be the cause?
can i upload it over here?

thanks anyway LM.
 
Upload it and let me take a look please. :)
 
My antivirus blocked the site as malicious. Can you upload the code to CFC? Just attach it to your post.
 
HI LM,

this is the new code: opoup.py :

Code:
from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import CvDebugTools
import PyHelpers
import Popup as PyPopup
import CvCameraControls
import CvTopCivs
import sys
import CvAdvisorUtils
import CvTechChooser

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

    def onGameStart(self, argsList):
        'Called at the start of the game'
###starting popup - begin
        if (gc.getGame().getGameTurnYear() == gc.getDefineINT("START_YEAR") and not gc.getGame().isOption(GameOptionTypes.GAMEOPTION_ADVANCED_START)):
            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                if (player.isAlive() and player.isHuman()):
                    popupInfo = CyPopupInfo()
                    popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_TEXT)
                    szBody = localText.getText("TXT_KEY_MOD_HEADER", ()) + "\n\n" + localText.getText("TXT_KEY_MOD_TEXT", ())
                    popupInfo.setText(szBody)
                    popupInfo.addPopup(iPlayer)                                      
###starting popup - end
this is the opoup.xml file under the config folder:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
    ChangePlayer
    Originally by jdog5000
  Converted to BUG module by Josh Sjoding
-->
<mod id="opoup" module="opoup">

 <event type="GameStart" function="onGameStart"/>
</mod>

this is the added line in the init.xml file :
Code:
    <!-- BULL -->
  
    <load mod="BULL Core"/>
    <load mod="BULL Actions"/>
    <load mod="BULL City Bar"/>
    <load mod="BULL Misc Hovers"/>
  
    <load mod="AutoSave"/>
    <load mod="MapFinder"/>
    <load mod="Advanced Combat Odds"/>
    <load mod="opoup"/>   <!-- the new mod -->
  
    <!-- BUFFY -->


thats all.

and im getting some errors of syntax and more.

i hope you can assist,
not sure i added the function properly.
 
You can't add the opoup mod information into init.XML in the location that you have put it. You've put it into the BULL section of the file and the game is only expecting to see BULL modules right there. IT has to be farther down under "New Mods"

Put into init.XML where I have put BATpop. Like this:

Code:
<!-- BULL -->
  
    <load mod="BULL Core"/>
    <load mod="BULL Actions"/>
    <load mod="BULL City Bar"/>
    <load mod="BULL Misc Hovers"/>
  
    <load mod="AutoSave"/>
    <load mod="MapFinder"/>
    <load mod="Advanced Combat Odds"/>
  
    <!-- BUFFY -->
  
    <load mod="BUFFY"/>
  
    <!-- New Mods -->
  
    <load mod="BATPop"/>
    <load mod="MovieMod"/>
    <load mod="Fem CEO Mod"/>
    <load mod="Messages"/>
    <load mod="GreatPersonEvents"/>

Start with that. And what are the SPECIFIC errors you are getting?
 
A Python syntax error? On an XML syntax error?
 
What is the exact syntax error that you are getting? It might help me narrow the problem down for you.
 
Keldath,

Replace the two files of the same name in your config and Python folders with the two files I have in this zip file. It works now for me. If it works for you, I'll explain what I did.
 

Attachments

  • opoup.zip
    1.1 KB · Views: 231
HI lemon,
well -it didnt work and it worked,
at first, i had to change the gamestart to Gamestart ,
and than, i had to remove arglist variable, since it said some error on it,
then it worked, but i got double dawn of man pop up, so i removed the part after the function, and left only the code below and it worked as it should!

so now i only call the function with (self) only, and the cod below.
so, if i understand what you did - is basically wrong indentation of the space in the code i gave you?

edit
wow i struggled with this more, i got an error now upon city build, something about module.
after many trials,
turns out - i cant have the same naming :
<mod id="Popup" module="Popup">
i found that the name of the module must be different:
<mod id="Popup" module="Popup_keldath">

now gonna try to add the rest of my stuff....

do you know if i can add it all into this popup python? as in one module for all my stuff?
 
Last edited:
Top Bottom