Need QUICK python help

Sarke

Warlord
Joined
Jun 1, 2006
Messages
101
Location
Long Beach, CA, USA
Ok... i'm trying to first do something real basic... just a popup when you start the game saying "Hello World" -- and if possible have this toggle by a config file.... It seems EVERYTHING I try to do ends in nothing happening at all...

First thing I did was modify the CustomEventManager file

I added: import CvPAModEventManager to the file

Next I created a PAModEventManager

When I launch the game though...no popup... the entire files are below... am I missing something stupid?

Thanks!

CvCustomEventManager.py
Spoiler :
import CvEventManager
import CvCulturalInfluencesEventManager
import CvCulturalDecayEventManager
import CvEnhancedCultureConquestEventManager
import CvPAModEventManager

class CvCustomEventManager(CvEventManager.CvEventManager, object):

def __init__(self, *args, **kwargs):
super(CvCustomEventManager, self).__init__(*args, **kwargs)
# map the initial EventHandlerMap values into the new data structure
for eventType, eventHandler in self.EventHandlerMap.iteritems():
self.setEventHandler(eventType, eventHandler)
# --> INSERT EVENT HANDLER INITIALIZATION HERE <--
CvCulturalInfluencesEventManager.CvCulturalInfluencesEventManager(self)
CvCulturalDecayEventManager.CvCulturalDecayEventManager(self)
CvEnhancedCultureConquestEventManager.CvEnhancedCultureConquestEventManager(self)
CvPAModEventManager.CvPAModEventManager(self)

def addEventHandler(self, eventType, eventHandler):
"""Adds a handler for the given event type.

A list of supported event types can be found in the initialization
of EventHandlerMap in the CvEventManager class.

"""
self.EventHandlerMap[eventType].append(eventHandler)

def removeEventHandler(self, eventType, eventHandler):
"""Removes a handler for the given event type.

A list of supported event types can be found in the initialization
of EventHandlerMap in the CvEventManager class. It is an error if
the given handler is not found in the list of installed handlers.

"""
self.EventHandlerMap[eventType].remove(eventHandler)

def setEventHandler(self, eventType, eventHandler):
"""Removes all previously installed event handlers for the given
event type and installs a new handler .

A list of supported event types can be found in the initialization
of EventHandlerMap in the CvEventManager class. This method is
primarily useful for overriding, rather than extending, the default
event handler functionality.

"""
self.EventHandlerMap[eventType] = [eventHandler]

def handleEvent(self, argsList):
"""Handles events by calling all installed handlers."""
self.origArgsList = argsList
flagsIndex = len(argsList) - 6
self.bDbg, self.bMultiPlayer, self.bAlt, self.bCtrl, self.bShift, self.bAllowCheats = argsList[flagsIndex:]
eventType = argsList[0]
return {
"kbdEvent": self._handleConsumableEvent,
"mouseEvent": self._handleConsumableEvent,
"OnSave": self._handleOnSaveEvent,
"OnLoad": self._handleOnLoadEvent
}.get(eventType, self._handleDefaultEvent)(eventType, argsList[1:])

def _handleDefaultEvent(self, eventType, argsList):
if self.EventHandlerMap.has_key(eventType):
for eventHandler in self.EventHandlerMap[eventType]:
# the last 6 arguments are for internal use by handleEvent
eventHandler(argsList[:len(argsList) - 6])

def _handleConsumableEvent(self, eventType, argsList):
"""Handles events that can be consumed by the handlers, such as
keyboard or mouse events.

If a handler returns non-zero, processing is terminated, and no
subsequent handlers are invoked.

"""
if self.EventHandlerMap.has_key(eventType):
for eventHandler in self.EventHandlerMap[eventType]:
# the last 6 arguments are for internal use by handleEvent
result = eventHandler(argsList[:len(argsList) - 6])
if (result > 0):
return result
return 0

# TODO: this probably needs to be more complex
def _handleOnSaveEvent(self, eventType, argsList):
"""Handles OnSave events by concatenating the results obtained
from each handler to form an overall consolidated save string.

"""
result = ""
if self.EventHandlerMap.has_key(eventType):
for eventHandler in self.EventHandlerMap[eventType]:
# the last 6 arguments are for internal use by handleEvent
result = result + eventHandler(argsList[:len(argsList) - 6])
return result

# TODO: this probably needs to be more complex
def _handleOnLoadEvent(self, eventType, argsList):
"""Handles OnLoad events."""
return self._handleDefaultEvent(eventType, argsList)


CvPAModEventManager.py

Spoiler :
## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005


from CvPythonExtensions import *
import Popup as PyPopup
import DataStorage
import CvEventManager
import PyHelpers
import CvConfigParser

# globals
gc = CyGlobalContext()

PyPlayer = PyHelpers.PyPlayer
PyGame = PyHelpers.PyGame()

data = DataStorage.DataStorage()

g_bDisplayTurn = true


class CvPAModEventManager:
def __init__(self, eventManager):

global g_bDisplayTurn

config = CvConfigParser.CvConfigParser("PAMod Config.ini")

if(config != None):
g_bDisplayTurn = config.getboolean("PAMod", "Display Message at Start of Turn", True)

if(g_bDisplayTurn):
eventManager.addEventHandler("GameStart", self.onEndGameStart)


def onGameStart(self, argsList):
popup = PyPopup.PyPopup()
popup.setBodyString( 'Hello World' )
popup.launch()



PAMod Config.ini

Spoiler :
[PaMod]

; Displays Hello World at Start of Game
Display Message at Start of Turn = True
 
update 2: Ok i've been at this for 6 hours-- i give up for tonight, If anyone can help me with this most basic of functions i'd appreciate it. I'm fairly confident that once I can get ANY function to launch, i'll be able to figure out the syntax of python and get things moving.

This is a second way of doing it I attempted... I turned on the debugger and got the following error for PAModUtils: module object has no attribute

Attached are the two files from this attempt

CvPAModEventManager

Spoiler :
## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
##
##

from CvPythonExtensions import *
import CvUtil
import Popup as PyPopup
import CvEventManager
import PyHelpers
import CvConfigParser
import PAModUtils
import math

PAModUtils = PAModUtils.PAModUtils()

class PAModEventManager:
def __init__(self, eventManager):
# initialize base class

eventManager.addEventHandler("GameStart", self.onGameStart)

def onGameStart(self, argsList):
PAModUtils.handleNewGame(argsList)


PAModUtils

Spoiler :


from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import CvDebugTools
import PyHelpers
import Popup as PyPopup
import sys
import cPickle as pickle
import CvConfigParser


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






class PaModUtils:


# blah
def handleNewGame(self, yesno):

# Return immediately if an invalid city was pased in


# Setup the city data if it wasn't previously setup.
if(yesno.isNone()):
CyInterface.addImmediateMessage("ISNONE", "")
return
CyInterface.addImmediateMessage(yesno, "")



Update 3: I just wanted to add that whoever designed python needs to be shot. Who designs a language where whitespace is considered syntax? Grrrr.... 12 hours and counting and I still cant even get a "Hello World" message to appear. Never have I seen a more poorly designed language :(

update 4:At something like 16 hours straight of trying... have read EVERY thread in the forum with the word Python in it (google searchbar sure is useful huh).... still nothing.... several other people have had the same issue with their attempts but every time they are either ignored or told to try harder.... Is there some sort of unspoken rule amongst you guys against helping people get started with their python work? The SDK is simple once you get it to compile... why is this so difficult?
 
Hey Sarke,

Did you change the EventInterface.py file so it changes the following lines from:
Code:
import CvEventManager
from CvPythonExtensions import *

normalEventManager = CvEventManager.CvEventManager()

to:
Code:
import CvCustomEventManager
from CvPythonExtensions import *

normalEventManager = CvCustomEventManager.CvCustomEventManager()
 
Hey Sarke,

Did you change the EventInterface.py file so it changes the following lines from:
Code:
import CvEventManager
from CvPythonExtensions import *

normalEventManager = CvEventManager.CvEventManager()

to:
Code:
import CvCustomEventManager
from CvPythonExtensions import *

normalEventManager = CvCustomEventManager.CvCustomEventManager()


I didn't.... actually you did... I kinda plagerized some of your files in trying to get it working.... so this should seem pretty familair.... here's the CvEventInterface.py file:

Code:
# Sid Meier's Civilization 4
# Copyright Firaxis Games 2005
#
# CvEventInterface.py
#
# These functions are App Entry Points from C++
# WARNING: These function names should not be changed
# WARNING: These functions can not be placed into a class
#
# No other modules should import this
#
import CvUtil

from CvPythonExtensions import *

import CvCustomEventManager

normalEventManager = CvCustomEventManager.CvCustomEventManager()

def getEventManager():
	return normalEventManager

def onEvent(argsList):
	'Called when a game event happens - return 1 if the event was consumed'
	return getEventManager().handleEvent(argsList)

def applyEvent(argsList):
	context, playerID, netUserData, popupReturn = argsList
	return getEventManager().applyEvent(argsList)

def beginEvent(context, argsList=-1):
	return getEventManager().beginEvent(context, argsList)
 
Top Bottom