[SOLVED] CvEventManager.py not able to edit/open files?

Louis the XIV

Sun King
Joined
Jun 23, 2020
Messages
837
Location
Court of Versailles
Hi! I have been trying to make civ open a file and write the turn in it on the endTurn function in the CvEventManager.py file.
My endTurn function looks like this but it is not writing anything in the file when I click end turn in game, nor any errors.
Code:
    def onEndGameTurn(self, argsList):
        'Called at the end of the end of each turn'
        current_turn = gc.getGame().getGameTurn()
        file = open("turn.txt", "w")
        file.write(str(current_turn))
        file.close()
        iGameTurn = argsList[0]
I tried to make it write just a string like "hello" but even that didn't work. I also tried to open the file like this
Code:
with open("turn.txt", "w") as file:
      file.write("something")
      file.close()
but that also didn't work.
Any help or advice appreciated!

EDIT:
It created/edited the files in the Beyond the Sword folder and not the folder of the CvEventManager.py!
 
Last edited:
Why not write to a log file? Enable logging in .ini (in my documents/my games) and use print, like
PHP:
print "%s%s: %.3fs" % (strIndent, text, fEndTime - fStartTime)
Your output will then appear in PythonDbg.log (obviously in the logs folder)
Oh I just realized what a stupid mistake I've made! I thought it would create/edit the file in the folder of the cveventmanager.py but it actually makes it in the Beyond The Sword folder! Just checked now and works fine.
 
Oh I just realized what a stupid mistake I've made! I thought it would create/edit the file in the folder of the cveventmanager.py but it actually makes it in the Beyond The Sword folder! Just checked now and works fine.
:lol:
The game views all paths relative to the working directory, which is the same dir as the exe. If you start the game with a different working directory (you can if you make a shortcut and edit it) then vanilla code fails to find the needed files and nothing will work.
PHP:
gDLL->getModName()
C++ code for getting the folder containing the loaded mod, including the path. Useful if you want to read files from the mod, though I don't think this can be reached in python. That is unless you mod the DLL to add a python exposed function for it.
 
Top Bottom