BugUtil.getText problem

Joined
Jul 5, 2004
Messages
23,562
Location
Canberra, Australia
I seem to be doing something wrong with the BugUtil getTexT function when I have more than one parameter or have character (string variables).

The message I want out is:-
"From killing Rhinoceros, 3 food and 3 hammers has been delivered to Mecca."

where each bolded bit is created in the python to fit into the GameText message.
Code:
 	<TEXT>
        <Tag>TXT_KEY_SUBDUED_KILL_MESSAGE</Tag>
        <English>From killing %s, %s has been delivered to %s.</English>
...    </TEXT>

The code for creating the three food and 3 hammers bit:-
Code:
		sFoodMessage = ''
		sHammerMessage = ''
		sAndMessage = ''
		if (iHammers > 0):
			pClosestCity.changeProduction(iHammers)
			if (iHammers > 1):
				sHammerMessage = '%d hammers' %(iHammers)
				sBugHammerMessage = BugUtil.getText("TXT_KEY_SUBDUED_MANY_HAMMERS",iHammers)
			else:
				sHammerMessage = 'one hammer'
				sBugHammerMessage = BugUtil.getText("TXT_KEY_SUBDUED_ONE_HAMMER")
				sReturnMessage = sHammerMessage
		if (iFood > 0):
			pClosestCity.changeFood(iFood)
			if (iFood > 1):
				sFoodMessage = '%d food' %(iFood)
				sBugFoodMessage = BugUtil.getText("TXT_KEY_SUBDUED_MANY_FOOD",iFood)
			else:
				sFoodMessage = 'one food'
				sBugFoodMessage = BugUtil.getText("TXT_KEY_SUBDUED_ONE_FOOD")
				sReturnMessage = sHammerMessage
		if (iFood > 0) and (iHammers > 0):
			sReturnMessage = '%s and %s' %(sFoodMessage, sHammerMessage)
			sBugReturnMessage = BugUtil.getText("TXT_KEY_SUBDUED_AND", (sFoodMessage, sHammerMessage))
		BugUtil.debug("Subdue Animals - FoodMessage %s", sBugFoodMessage)
		BugUtil.debug("Subdue Animals - BugFoodMessage %s", sBugFoodMessage)
		BugUtil.debug("Subdue Animals - HammerMessage %s", sBugHammerMessage)
		BugUtil.debug("Subdue Animals - BugHammerMessage %s", sBugHammerMessage)
		BugUtil.debug("Subdue Animals - ReturnMessage %s", sReturnMessage)
		BugUtil.debug("Subdue Animals - BugReturnMessage %s", sBugReturnMessage)

The trace results:-
Code:
11:58:54 DEBUG: Subdue Animals - onCombatResult called.
11:58:54 DEBUG: Subdue Animals - FoodMessage 38454076 food
11:58:54 DEBUG: Subdue Animals - [B]Bug[/B]FoodMessage 38454076 food
11:58:54 DEBUG: Subdue Animals - HammerMessage 38454076 hammers
11:58:54 DEBUG: Subdue Animals - [B]Bug[/B]HammerMessage 38454076 hammers
11:58:54 DEBUG: Subdue Animals - ReturnMessage 3 food and 3 hammers
11:58:54 DEBUG: Subdue Animals - [B]Bug[/B]ReturnMessage `JM and 
11:58:54 DEBUG: Subdue Animals - From killing Rhinoceros, 3 food and 3 hammers has been delivered to Mecca.

I forgot to trace what the final BUG version was in this test.
 
I have just found out that getText does not support multiple values which is what I was doing. The "doco" in the code suggests that it does support multiple variables if you enclose them in an extra set of ().

So my line
Code:
sBugReturnMessage = BugUtil.getText("TXT_KEY_SUBDUED_AND", (sFoodMessage, sHammerMessage))
is causing my problem.

Sorry for bothering you.
 
I'm not quite sure if you got it working, but BugUtil.getText() works very much like CyTranslator.getText() with some subtle differences:

  • If you pass in a single value, BUG will automatically put it into a tuple.
  • You can pass a default value that will be used if the <Text> key doesn't exist.
  • BUG will (by default) replace any additional symbols in the text.
One problem I see is that in the example <Text> you posted you are using "%s" without a number after it. The <Text> entries are different from Python's % substitutions: each must have a number after it (1, 2, ...) to tell it which parameter to replace. This allows the orderings to be different.

You must use this:

Code:
    <TEXT>
        <Tag>TXT_KEY_SUBDUED_KILL_MESSAGE</Tag>
        <English>From killing [B]%s[COLOR="Red"]1[/COLOR][/B], [B]%s[COLOR="Red"]2[/COLOR][/B] has been delivered to [B]%s[COLOR="Red"]3[/COLOR][/B].</English>
        ...
    </TEXT>
 
And to clarify, BugUtil.getText() most definitely does support multiple parameters by enclosing them in parentheses.
 
One problem I see is that in the example <Text> you posted you are using "%s" without a number after it. The <Text> entries are different from Python's % substitutions: each must have a number after it (1, 2, ...) to tell it which parameter to replace. This allows the orderings to be different.

Ah, that was the bit of information I was missing. I will get back to it as soon as I fix my version control error:eek:. Always a problem if you are testing a mod in multiple other mods.

Edit: yes it works a treat. thanks.
 
Top Bottom