which .getUnit do i want here?

naf4ever

Dread Lord
Joined
Feb 8, 2003
Messages
405
Location
Southeast Washington State
Simply trying to get the unit owner from a range of units. But keeping getting errors.

Code:
def onBeginPlayerTurn(self, argsList):
        GameTurn, iPlayer = argsList
	player = gc.getPlayer(iPlayer)
        for i in range(player.getNumUnits()):
		pUnit = gc.getUnitInfo(i)
		mPlayer = gc.getPlayer(pUnit.getOwner())

Says my object has no attribute for getOwner in the mPlayer line. Ive tried a few of the other .getUnit commands for pUnit, but few seem to work with the integar there.

Any ideas?
 
naf4ever said:
Simply trying to get the unit owner from a range of units. But keeping getting errors.

Code:
def onBeginPlayerTurn(self, argsList):
        GameTurn, iPlayer = argsList
	player = gc.getPlayer(iPlayer)
        for i in range(player.getNumUnits()):
		pUnit = gc.getUnitInfo(i)
		mPlayer = gc.getPlayer(pUnit.getOwner())

Says my object has no attribute for getOwner in the mPlayer line. Ive tried a few of the other .getUnit commands for pUnit, but few seem to work with the integar there.

Any ideas?


It should be

Code:
def onBeginPlayerTurn(self, argsList):
	GameTurn, iPlayer = argsList
	player = gc.getPlayer(iPlayer)
	for i in range(player.getNumUnits()):
		pUnit = player.getUnit(i)
		mPlayer = gc.getPlayer(pUnit.getOwner())
But why do you need mPlayer? it will be the same as player.
 
TheLopez said:
But why do you need mPlayer? it will be the same as player.

Because when im up late the previous night typing code like i was I tend to get a bit spaced out and start setting up multiple definitions which do the same things, sigh. Your suggestion worked though, thanks. But it appears I wasnt using the right range function either.

Im basically trying to get a list of all the units the current player has on the board, then if the player has a particular one (I assume i can check against their class type), return its plot coordinates.

The range i specified though seems to return a list of every single unit the player can build even if they dont have them... hmm
 
Why do people always want to reinvent the wheel? ;)

Code:
import PyHelpers

PyPlayer = PyHelpers.PyPlayer

def onBeginPlayerTurn(self, argsList):
	GameTurn, iPlayer = argsList
	unitList = PyPlayer(iPlayer).getUnitList()

Not sure exactly what you mean by wanting a particular unit (individual unit? unit type?) but it should be easy enough to extract from this list.
 
Locutus said:
Why do people always want to reinvent the wheel? ;)

Code:
import PyHelpers

PyPlayer = PyHelpers.PyPlayer

def onBeginPlayerTurn(self, argsList):
	GameTurn, iPlayer = argsList
	unitList = PyPlayer(iPlayer).getUnitList()

Not sure exactly what you mean by wanting a particular unit (individual unit? unit type?) but it should be easy enough to extract from this list.

Locutus is (as usual) absolutly correct. to continue with his train of thought your probably looking for:

Code:
		for pUnit in PyPlayer(iPlayer).getUnitList():
			mPlayer = gc.getPlayer(pUnit.getOwner())

In fact I removed getNumUnits() from all of my code. The problem with it is that the unit array is unstructured. So your units may look like:

Unit(0)
Unit(1)
Unit(2)
Unit(4)
Unit(14)

Now when you do getNumUnits() it returns 5 and only hits the first 4 units in that list. Unit(14) is left out and missed in all the checks. It what was causing some of the spells to never wear off and some summoned creatures not to go away in early versions of FfH.
 
I agree with both Locutus and Kael. I was just trying to solve his initial problem. Here's another question, should this be extended to removing the usage of objPlot.getNumUnits() as well?
 
TheLopez said:
I agree with both Locutus and Kael. I was just trying to solve his initial problem. Here's another question, should this be extended to removing the usage of objPlot.getNumUnits() as well?

Thats a good question, I never noticed a problem with the call on pPlot. But to be honest in only happened in very rare occasions over the entire unit population (like 2-3 times a game) that a unit would get "outside the normal array bounds". So the chance of seeing this same issue in a plot check has gotta be pretty small even if it does exist.
 
Thanks all.

But are you basically saying the:
Code:
for i in range()
function is flawed to begin with? Or just for units. I use it in quite a few things like to produce a range of cities of techs...
 
naf4ever said:
Thanks all.

But are you basically saying the:
Code:
for i in range()
function is flawed to begin with? Or just for units. I use it in quite a few things like to produce a range of cities of techs...

for i in range is fine. Its just that the unit array is unsorted. Hence the wrapper class that Locutus mentioned.
 
TheLopez said:
Here's another question, should this be extended to removing the usage of objPlot.getNumUnits() as well?

No, that works fine. It's just that units (and cities) are added to global list and just removed when the are killed/conquered, the existing indices aren't 'moved up'. So you end up with lists much like the one Kael posted with a lot of holes in it. For plots, the list is recalculated whenever you request it, because that tends to be much, much more volatile. It's an efficiency issue, although I'm personally not convinced a whole lot is gained by it. But that you'll have to take up with Soren ;)
 
Thanks for your quick answer
 
Back
Top Bottom