game_map[x, y]
returns a list but when I write game_map[x, y][0]
it returns Friendly DF, stability: 30.0, [13, 0]
but this is not a list because game_map[x, y][0][0]
returns an error. I was expecting it to return Friendly DF
. Does game_map[x, y][0]
return a string?
It returns a list of Unit objects (Unit class found in unit.py). However, if there are no units at that location it will return an empty list []
so game_map[x, y][0][0]
would be out of bounds, since it has nothing inside it.
The reason you are getting the string output is not that it returns a string, but because the Unit object has been written so when you print it to the console it prints that string (which you can edit). It is done through these functions:
def __toString(self):
owner = "Friendly" if self.player_index == 0 else "Enemy"
removal = ", pending removal" if self.pending_removal else ""
return "{} {}, stability: {} location: {}{} stationary: {}".format(owner, self.unit_type, self.stability, [self.x, self.y], removal, self.stationary)
def __str__(self):
return self.__toString()
def __repr__(self):
return self.__toString()
Where the __repr__(self):
is the actual function that overloads the print to console options. Here it is just getting whatever the __toString
function outputs, which is the string you see.
You can read more about it here:
1 Like