Add message string

Paolo80

Chieftain
Joined
Dec 20, 2019
Messages
85
Hello guys,

I want to add a message during game.

I wrote the following xml tag:

TXT_KEY_ERETICO
...
Heretic was reported near %s1
...

And in python event file

Code:
iCity = gc.getMap().findCity(xplot, yplot, pPlot.getUnit(1).getOwner(), TeamTypes.NO_TEAM, True, False, TeamTypes.NO_TEAM, DirectionTypes.NO_DIRECTION, CyCity())
print(iCity.getName(), "ERETICO")
message = CyTranslator().getText("TXT_KEY_ERETICO" %(iCity.getNameKey()), ())
CyInterface().addImmediateMessage(message, "")

When heretic is created, it causes python error

TypeError: not all arguments converted during string formatting

In PythonDbg my alert writes e.g. (u'Ravenna', 'ERETICO').

I think the error is that getNameKey() returns a wstring type instead string type. Must I convert wstring into string? How can I do?
 
Last edited:
I don't think that's the issue. wstring vs string is a C++ concern, it's a little different in Python. Python 2 has string and unicode types, but I don't think you need to ever be aware of them when doing string formatting.

Instead, your problem is that you are trying to do Python string formatting (% operator) within CyTranslator.getText. But that method already fulfills that purpose for you (in particular, it expects different keys because it is positional, which Python 2 string formatting isn't.

The working code should be:
Code:
CyTranslator().getText("TXT_KEY_ERETICO", (iCity.getNameKey(),))
Be particularly aware of the comma in that tuple, to make sure it is interpreted as a single element tuple (getText requires a tuple as second argument).

Also, I would encourage you not to use iCity as the name for this variable. The i prefix is by convention used for integer variables (for debatable reasons, but it's the convention in Firaxis's Python code), while this is a custom object.
 
Back
Top Bottom