Author Topic: Territories Importer issues  (Read 1521 times)

Pollyfun

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Territories Importer issues
« Reply #60 on: August 09, 2021, 01:11:06 PM »
Sounds good  8) I will try it out soon.

Pollyfun

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Territories Importer issues
« Reply #61 on: August 11, 2021, 09:21:31 AM »
I've done some testing now. I can assign provinces one at a time. I used this code

Code
_map.CountryRemoveProvinces(province.countryIndex, provincesToAdd, false);
_map.CountryAddProvinces(targetCountryIndex, provincesToAdd, false, false, false);

https://youtu.be/Srceaq6K1-U

It seems stable when shifting the owner forth and back, so that's very positive.

There's still some issues to work out though. When I'm loading the world, and assigning provinces

"Main Country" -> "Argentine"
"Main Country" -> "United Kingdom"

The assigning works so far:
_map.CountrySetProvinces(_map.GetCountryIndex("United Kingdom"), provincesToAddUK, false, false, false);
_map.CountrySetProvinces(_map.GetCountryIndex("Argentine"), provincesToAddArgentine, false, false, false);

But when I try to remove all provinces from "Main Country" the game gets exceptions right afterwards.

Code
_map.CountryRemoveProvinces(_map.GetCountryIndex("Main Country"), provincesToRemoveMainCountry, false);

When provinces belongs both to the sourcecountry and the destinationcountry, is it the same objects or is it clones?
Is there a safe way to delete a country and all it's provinces and regions? (when the provinces and regions also belongs to another country like in this case)
« Last Edit: August 11, 2021, 09:43:38 AM by Pollyfun »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
    • View Profile
Re: Territories Importer issues
« Reply #62 on: August 11, 2021, 10:18:13 AM »
I've refreshed the latest beta with a safety check in that function so it doesn't break if the country doesn't have any region.

The Provinces objects are not cloned so they're the same object in both countries (so CountryAddProvinces / CountryRemoveProvinces should be called in sequence if you're transferring a province from one country to another). The only case where you don't need to use CountryRemoveProvines is during an initial setup for example, where you have all provinces assigned to some kind of global pool or main country and you distribute them to the real countries to finally completely removing the pool country.

You can fully remove a country calling CountryDelete method.

Pollyfun

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Territories Importer issues
« Reply #63 on: August 11, 2021, 12:50:44 PM »
Thanks, I got it working.

A possible code improvement. It would be good if we could create an empty country, without needing to add a province and a region to it.
Currently I use this code to create a country, and get country.provinces initialized.

Code
Vector2[] pointsArgentine =
{
new Vector2(-0.2f, 0.59f),
new Vector2(-0.1f, 0.59f),
new Vector2(-0.1f, 0.55f),
new Vector2(-0.2f, 0.55f)
};
int countryIndex = CreateCountry("Argentine", pointsArgentine, 11);
CreateProvince("Arg Province 1", countryIndex, pointsArgentine);   // create a province to initialize country.provinces
_map.ProvinceDelete(_map.provinces.Length-1);  // delete province
_map.GetCountry(countryIndex).regions.Clear();  // delete region


It would be great is this was enough:
Code
int countryIndex = CreateCountry("Argentine", 11);

So two parts:
Skipping the points parameter in CreateCountry()
Initializing country.provinces = new Province[0] during CreateCountry() instead of leaving null.


UPDATE: I noticed that the CreateCountry() method was not part of the core WMSK, but from one of the samples.
So I updated it myself.

Code
int CreateCountry(string name, int uniqueId) {
   Country country = new Country(name, "Continent", uniqueId);
   int countryIndex = _map.CountryAdd(country);
   return countryIndex;
}

Initializing country.provinces would still be good though, to not get errors when using it before adding provinces (or creating a dummy province to avoid those errors)
« Last Edit: August 11, 2021, 02:34:46 PM by Pollyfun »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
    • View Profile
Re: Territories Importer issues
« Reply #64 on: August 12, 2021, 09:15:51 AM »
There's a CountryCreate method already that takes a name and the continent.

I've added a province initializer to the Country constructor (in Country.cs):
Code
		/// <summary>
/// Creates a new country
/// </summary>
/// <param name="name">Name.</param>
/// <param name="continent">Continent.</param>
/// <param name="uniqueId">For user created countries, use a number between 1-999 to uniquely identify this country.</param>
public Country(string name, string continent, int uniqueId) {
this.name = name;
this.continent = continent;
this.regions = new List<Region>();
this.uniqueId = uniqueId;
this.attrib = new JSONObject();
this.mainRegionIndex = -1;
this.provinces = new Province[0];
}

Pollyfun

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Territories Importer issues
« Reply #65 on: August 12, 2021, 01:12:42 PM »
Thanks, I've added the same codeline so there's no need to create a new beta just for this.