Spawn units at sqaure on turn

Yes, it should look like
PHP:
	def onBeginGameTurn(self, argsList):
		'Called at the beginning of the end of each turn'
		iGameTurn = argsList[0]

and then your code should follow.
Also you then have to compare iGameTurn ==586.
But it would imho be better to compare it to the year, because the game turn will change, when the speed is changed.

Also iAmerica is not defined.
Most easy way here: Look in your worldbuilder file (i guess, you have one) what number america it has there, and place the number instead there.
 
Code:
        def onBeginGameTurn(self, argsList):
                    iGameTurn = argsList[0] 
	       if (iGameTurn == 586):
                    iX = 47
                    iY = 70
                    pPlayer = gc.getPlayer(11)
                    iUnitType = gc.getInfoTypeForString('UNITCLASS_MUSKETMAN')  
                    DefaultUnitAI = 'UNIT_AI_ATTACK'
                    pPlayer.initUnit(iUnitType, iX, iY, DefaultUnitAI,DirectionTypes.NO_DIRECTION )
!!!!The indentation is right in the editor but not the post!!!!!
It said there was an error in the "pPlayer.initUnit(iUnitType, iX, iY, DefaultUnitAI,DirectionTypes.NO_DIRECTION )" code that said its not how it is defined in CvPlayer.
 
Code:
        def onBeginGameTurn(self, argsList):
                iGameTurn = argsList[0] 
                if (iGameTurn == 586):
                    iX = 47
                    iY = 70
                    pPlayer = gc.getPlayer(11)
                    UnitType = gc.getInfoTypeForString('UNIT_MUSKETMAN')  
                    UnitAIType = UnitAITypes.UNITAI_ATTACK
                    DirectionType = DirectionTypes.NO_DIRECTION 
                    initUnit (UnitType iIndex, int iX, int iY, UnitAIType iIndex, DirectionType iIndex)
This is the new code and the error is inthe initUnit line
 
When calling a function, you don't repeat the types of the variables. Also, initUnit needs to be called by a player-type object. As written, that line is just nonsense.
 
Davivlallen is definately correct, you need to turn on "python logging" and "python exceptions" in your civilizationiv.ini file. Just in case you are not familiar with it, here's what you need to. Open the civilizationiv.ini file using the _Civ4Config icon in your BTS folder. and change HidePythonExceptions = 1 to HidePythonExceptions = 0. To enable logging change LoggingEnabled = 0 to LoggingEnabled = 1 (I changed it so long ago, I think that's what needs to be done, but I'm not positive).
 
Since you're struggling with the Python stuff, I think it wouldn't be inappropriate for me to tell about a project of mine: I'm creating custom Python functions for scenario makers, mostly for use with the Rhye's and Fall of Civilization scenario (even if much of the stuff would work for any scenario, with a few alterations). There will be several easy-to-use functions for spawning units, but also for spawning cities, setting buildings, unit experience, flipping cities (and whole areas on the map) and stuff like that.

I'm actually beta-testing these functions right now and some are already available as a download in this thread. This would be the latest available version, but things are changing all the time as development progresses.

I was thinking you could pretty much just copy-paste some code into your own scenario, if you like. There is also some general Python discussion in the thread linked above, if you're interested in learning. There are also full documentation available for some of the functions - or links to documentation in other threads.
 
Code:
def onBeginGameTurn(self, argsList):
                iGameTurn = argsList[0] 
                if (iGameTurn == 586):
                    iX = 47
                    iY = 70
                    pPlayer = gc.getPlayer(11)
                    UnitType = gc.getInfoTypeForString('UNIT_MUSKETMAN')  
                    UnitAIType = UnitAITypes.UNITAI_ATTACK
                    DirectionType = DirectionTypes.NO_DIRECTION 
                    pPlayer.initUnit (UnitTypes.UNIT_MUSKETMAN iIndex, int 47, int 70, UnitAITypes.UNITAI_ATTACK iIndex, DirectionTypes.NO_DIRECTION iIndex)
in the log it says this:
File "CvEventManager", line 358
pPlayer.initUnit (UnitTypes.UNIT_MUSKETMAN iIndex, int 47, int 70, UnitAITypes.UNITAI_ATTACK iIndex, DirectionTypes.NO_DIRECTION iIndex)
^
SyntaxError: invalid syntax
 
Of course that's a syntax error - you've got two words in place of each argument. Python can't pass two things when it expects one variable, and it doesn't help when you never bothered defining those things. You do not simply copy what it says in the API to call a function; you put the names of the variables you've defined in the slots for each argument.
Here's a block that does what I think you're trying to do:
Code:
iX = 47
iY = 70
player = gc.getPlayer(11)
iMusketman = gc.getInfoTypeForString('UNIT_MUSKETMAN')
player.initUnit(iMusketman, iX, iY, UnitAITypes.UNITAI_ATTACK, DirectionTypes.NO_DIRECTION)
 
Jayrad, I feel I have to tell you this, so I hope you don't take it the wrong way. :rolleyes:

You really need to take a few steps back, because you obviously haven't bothered to learn any of the basics. I appreciate that you're just trying to solve a practical problem, and I guess I could help you with that if you want me too. (Although there are more knowledgeable people around.) But if you really wanna learn, try to understand what it is your doing. You simply can't expect any results from trowing some random code around without any clue about what you're doing.

I struggled with this also at the beginning, but honestly, it only took me a month or so before I knew enough to be able help others trying to learn Python. I'm only in my third month programming on my spare time, and I still learn more all the time. So you have a ton to learn if you wanna be able to do just about anything with Python. But I can promise you that its worth it!

There are some basics about programming you wound wanna learn urgently, but I'm not sure if this is the time or the place for it. So, do you wanna get into programming or do you wanna solve this problem you have with the scenario? Or both? You could send me a private message if you had enough of this thread already, since its doesn't look like its getting anywhere (at least from where I'm standing). You can't be helped with merely friendly advice if you don't wanna listen and learn.

edit: Use the code that jmerry posted, as it would achieve what you set out to do. But you still need to get everything else in order. Plus you would probably wanna do more with your scenario. What you really need is some useful tools...
 
It worked thank you Jmery and to Baldyr you are right i learn by experience and i thought that this would be one of the simpler functions and that python would be easier but i was wrong.
one last question though how do i change the amount of units that spawn.
 
initUnit spawns one unit, so you'll have to call it multiple times somehow. The simplest way to do that is to have multiple lines. For more flexibility, there are loops. As an example, here's the "makeUnit" function from Rhye's and Fall:
Code:
        def makeUnit(self, iUnit, iPlayer, tCoords, iNum): #by LOQ
                'Makes iNum units for player iPlayer of the type iUnit at tCoords.'
                for i in range(iNum):
                        player = gc.getPlayer(iPlayer)
                        player.initUnit(iUnit, tCoords[0], tCoords[1], UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)
That block "for i in range(iNum):" tells the function to repeat the next lines iNum times. Since that was passed as a parameter, this function allows you to spawn 1 or 5 or 20 units just by varying that parameter. More precisely, this loop defines a new integer variable i, and repeats the following line with values i=0, i=1, ..., i=iNum-1. If you wanted, you could use that value of i inside the loop for something.

There are several other variants of this loop syntax; you can start somewhere other than zero, or you can loop over a list.
Still, loops are a very basic element of programming, and you should know them. My recommendation: Take an introductory programming course, in any language. You will learn syntax for that language, along with basic principles of how to design functions and programs. Then, come back to Civ4's Python. Read some existing code; you'll pick up on a lot of Python syntax that way, and there are plenty of working examples to copy. Once you understand how the stuff that's already there works, start tinkering with it.

I went from no prior Python knowledge to releasing stuff on this forum in a few weeks. It's not that hard to pick up, if you already know something about programming.
 
You could start in the Modiki.
 
Code:
        def onBeginGameTurn(self, argsList):
                iGameTurn = argsList[0] 
                if (iGameTurn == 566):
                    iX = 47
                    iY = 70
                    player = gc.getPlayer(11)
                    i1player = gc.getPlayer(2)
                    iMusketman = gc.getInfoTypeForString('UNIT_MUSKETMAN')
                    iCannon = gc.getInfoTypeForString('UNIT_CANNON')
                    player.initUnit(iMusketman, iX, iY, UnitAITypes.UNITAI_ATTACK, DirectionTypes.NO_DIRECTION)
                    player.initUnit(iCannon, iX, iY, UnitAITypes.UNITAI_ATTACK, DirectionTypes.NO_DIRECTION)
                    i1player.declareWar(player, False)
Now the war delarations don't work it says that declareWar doesn't exist in CyPlayer
 
Now the war delarations don't work it says that declareWar doesn't exist in CyPlayer
Yeah, that would be because Civ differentiates between players (Civs) and teams. In most cases these are the same, but some stuff will be player stuff and others team stuff, nonetheless. Diplomatic status (like war) is a team issue, as the whole team would be at war with a Civ (or really another team). So you feed the CyTeam.declareWar() function a team instance. Makes sense? :crazyeye:

Also, the code differentiates between index numbers (IDs) and instances. Some functions only need, say, the player ID and others require the actual player instance. And its the same thing with teams.

You get the player instance corresponding to an index number with the CyGlobalContext.getPlayer() function. And you get the team instance with CyGlobalContext.getTeam(). ("CyGlobalContext" is usually abbreviated "gc" - you can check this out at the start of any .py file as it will say "gc = CyGlobalContext".)

About team instances, there is one thing I haven't figured out for myself yet:

It should be sufficient to merely do:
Code:
gc.getTeam(iID)
But I've seen this in a lot of code:
Code:
player = gc.getPlayer(iCiv)
team = gc.getTeam(player.getTeam())
The second function (CyPlayer.getTeam()) just gets the ID # of the team, which is then fed to the CyGlobalContext.getTeam() function as an integer value. Am I right to assume that this is done to ensure that the variable "team" gets the right instance - no matter if there would form new teams during a game session? But aren't teams defined in the World Builder file and the index values wouldn't change during play?

By the way, Jayrad, the above code could also be written on a single line as:
team = gc.getTeam(gc.getPlayer(iCiv).getTeam())
Please tell me this makes sense to you... :eek:
 
Top Bottom