python action buttons in MP?

davidlallen

Deity
Joined
Apr 28, 2008
Messages
4,743
Location
California
I have written some action buttons in pure python, modifying CvMainInterface.py in a similar way to what the Gods Of Old mod does. The action buttons lead to some simple code which modifies the game state, such as, a particular unit "refugee" has an action button. If you use the action button when the unit is in a city, it removes the refugee and increases the city population by one. The active part of the python is:

pUnit.kill(true, iUnitOwn)
pCity.changePopulation(1)

This is working correctly, in single player games. I have only the one CD, so I cannot directly test network multiplayer. But, recently somebody tried out multiplayer, and reported that an OOS error comes as soon as these action buttons are used. After thinking about it, I guess I had naively expected calls like kill and changePopulation to inform the other players; but there is no reasonable way they could do that.

How do action buttons in MP games work? Is there some step which I have missed, to communicate these arbitrary changes to the other players?
 
Well, after a little research I can answer my own question. The action button should not modify the game state. Instead, it should use CyMessageControl.sendModNetMessage (some arguments). Then the game engine will issue an event to all the players, which you can receive with the onModNetMessage method. Searching for these in Gods Of Old will give multiple examples.

So, I have mis-implemented these action buttons, they do not work in MP.

Is there a guide somewhere which may explain this, so I can double-check?
 
I had seen that link before but lost it. Thanks for sending it. That link is an excellent description of MP in general, not just for civ. It also contains a pointer to another thread specifically on the message system. I had read the link before, but evidently not realized all the places it should be applied. I understand now, I just have to make the code work.
 
I had seen that link before but lost it. Thanks for sending it. That link is an excellent description of MP in general, not just for civ. It also contains a pointer to another thread specifically on the message system. I had read the link before, but evidently not realized all the places it should be applied. I understand now, I just have to make the code work.

Unfrotunately, I wrote that guide years ago, and I believe the message-sending system has changed since vanilla Civ4, but it seems like you have the right idea with sendModNetMessage. I think that function was included in the newer Civ4 versions for just this type of situation that you are describing.
 
The concept described in your guide is clearly the right one. For coding, I tend to steal ^h^h^h^h^h draw inspiration from other working mods. I highly recommend the Gods Of Old code. In retrospect, I should have paid more attention to it.
 
Top Bottom