Problem with new python event

salaminizer

Colorado Internacional
Joined
Aug 12, 2006
Messages
221
Location
Porto Alegre, Brasil
Hello,

First of all (as usual), I don't know if what I'm doing is correct, but I couldn't find another way to do it, I'm following what's in the game.

What I want is another event such as onUnitKilled, etc. I want onUnitUnload.
so I have copied CvEventManager.py to my mod's directory and added:

Code:
'unitUnload'			: self.onUnitUnload

in the end of self.EventHandlerMap.

I also added the onUnitUnload function in this same file:

Code:
	# onUnload
	def onUnitUnload(self, argsList):
		pUnit = argsList[0]
#		player = PyPlayer(pUnit.getOwner())
#		iX = pUnit.getX()
#		iY = pUnit.getY()
#		pPlot = CyMap().plot(iX, iY)
#		pCity = pPlot.getPlotCity()
#		if unit.getUnitType() == gc.getInfoTypeForString('UNIT_CRATE'):
#			CvUtil.pyPrint("Player %d's %s unloaded at city %s at plot (%d, %d)" 
#		%(player, PyInfo.UnitInfo(pUnit.getUnitType()).getDescription(), pCity.getName(), iX, iY))

(it's commented because I thought this is what was wrong)

In the SDK, I have added:

Code:
gDLL->getEventReporterIFace()->unitUnload(this);

in CvUnit::unload()

and in CvDLLEventReporterIFaceBase.h:

Code:
virtual void unitUnload(CvUnit *pUnit) = 0;

I compile it successfully, but the game crashes when unloading a unit. I don't know what I else I need to set but the logs in My Games don't show anything. I have init,mplog,pythonerr2,resmgr and xml logs but none of them have anything useful at all. in my mod's ini I have:

Spoiler :

; Enable the logging system
LoggingEnabled = 1

; Enable synchronization logging
SynchLog = 0

; Overwrite old network and message logs
OverwriteLogs = 1

; Enable rand event logging
RandLog = 0

; Enable message logging
MessageLog = 1

GenerateCrashDumps = 1


and chipotle.

that's one problem, the other is the crash. by using the debug DLL I have found out that it crashes right after the call to gDLL:

First-chance exception at 0x00b4a31d in Civ4BeyondSword.exe: 0xC0000005: Access violation reading location 0xd8ea86d8.
Unhandled exception at 0x00b4a31d in Civ4BeyondSword.exe: 0xC0000005: Access violation reading location 0xd8ea86d8.

and that's all I know about the crash. solving both would be great :p

in the end what I want is doing something cool after an unit is unloaded, such checking for the plot and spawning a unit, stuff like that.

thanks.
 
In the SDK, I have added:

Code:
gDLL->getEventReporterIFace()->unitUnload(this);
I believe that won't work because the event reporter has no inherent unitUnload() function and it's not part of the SDK (all we get is the interface description) so you can't simply add your own. What you want to do instead is use the genericEvent() function. Here's an example from CvGame::update():
Code:
		// sample generic event
		CyArgsList pyArgs;
		pyArgs.add(getTurnSlice());
		gDLL->getEventReporterIFace()->genericEvent("gameUpdate", pyArgs.makeFunctionArgs());

In your case, you'd probably do something like this (untested):
Code:
		CyArgsList pyArgs;
		pyArgs.add(this);
		gDLL->getEventReporterIFace()->genericEvent("unitUnload", pyArgs.makeFunctionArgs());
 
I believe that won't work because the event reporter has no inherent unitUnload() function and it's not part of the SDK (all we get is the interface description) so you can't simply add your own. What you want to do instead is use the genericEvent() function. Here's an example from CvGame::update():
Code:
		// sample generic event
		CyArgsList pyArgs;
		pyArgs.add(getTurnSlice());
		gDLL->getEventReporterIFace()->genericEvent("gameUpdate", pyArgs.makeFunctionArgs());

In your case, you'd probably do something like this (untested):
Code:
		CyArgsList pyArgs;
		pyArgs.add(this);
		gDLL->getEventReporterIFace()->genericEvent("unitUnload", pyArgs.makeFunctionArgs());

it worked, thanks.
well, now that you say interface it makes sense :p I thought that just altering the .h didn't make much sense. next time I'll try READING the code instead of just "decoding" it... :p
thanks again

and about the logs, do you know what I'm doing wrong? :p
 
A direct SDK error often won't leave a trace in the logs. If the crash came as the result of a python function call, you'll get something in PythonErr.log, but in something like your case, debugging was the right call. ;)
 
Top Bottom