SDK City List

OrionVeteran

Deity
Joined
Dec 25, 2003
Messages
2,443
Location
Newport News VA
I'd like to create a function in CvPlayer.cpp that returns the list of cities that the player owns. I want it to be exposed to python. Since the function returns a listing, it can't start out with void. What do I replace the void with? Here is what I have so far:

Code:
void CvPlayer::getSDKCityList()
{
 CLinkList<int> cityList;
 CvCity* pLoopCity;
 int iLoop;

 cityList.clear();  
 for (pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop))
 {
  cityList.insertAtEnd(iLoop);

 }

 return cityList
}
 
You basically need to return an array of CyCity pointers and return that one. In the interface you need to add python::return_value_policy<python::manage_new_object>() to tell python it needs to free the memory once it's done with it because C++ won't do that in this setup (avoids leaking memory). Last you need to tell python that it's a list, but I can't remember how to do that.

However CyPlayerInterface1 seems to have what you need to write your city access code in pure python.
PHP:
       .def("firstCity", &CyPlayer::firstCity, "tuple(CyCity, int iterOut) (bool bReverse) - gets the first city")
       .def("nextCity", &CyPlayer::nextCity, "tuple(CyCity, int iterOut) (int iterIn, bool bReverse) - gets the next city")
       .def("getNumCities", &CyPlayer::getNumCities, "int ()")
       .def("getCity", &CyPlayer::getCity, python::return_value_policy<python::manage_new_object>(), "CyCity* (int iID)")
 
OGI has several Python functions that create listings. My desire is to convert some of those listings over SDK. SDK is faster than Python. Just one good example SDK function and I will be able to convert them all. That's why I am focusing on the city listing. I appreciate any help you can provide. Sincerely,
 
I did a bit of research and it looks like you need to return boost::python::list. However it's not done in vanilla and I haven't tried using it. When I google it, I get that people have problems using it, like this. While there is a partial solution here, do note that it is for python 2.7 and we are using the ancient python 2.4 (from 2003 or 2004). Looks like it's not impossible, but it will require some work to figure out how to do this.


However I wonder if it's worth the effort. If you do it to gain speed, then I suspect you are looking at the wrong place. The looping of cities in itself is likely not the culprit for a noteworthy slowdown. To gain speed, you should try to move as much as possible into C++, not just the list generation. C++ has two major advantages when it comes to performance. One is that it compiles and becomes faster. The other is that you can profile and identify which lines are too slow. Known slow lines can then be fixed to become faster.
 
Back
Top Bottom