Author Topic: procedurally  (Read 37 times)

procedurally

  • Guest
procedurally
« on: December 02, 2019, 10:22:47 AM »
Hi,
How to procedurally generate provinces by Vector2?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: procedurally
« Reply #1 on: December 02, 2019, 11:23:54 AM »
Check out demo scene 105 "Create Map Procedurally" under General Examples. It contains sample code for creating a new country using Vector2 coordinates.
Once you understand how to create a country, creating a province is a similar procedure except it involves using the Province class instead of Country and assigning a Province to a Country through the province.countryIndex property.

procedurally

  • Guest
Re: procedurally
« Reply #2 on: December 03, 2019, 07:36:47 AM »
thanks for the answer

Code
            Country country = new Country("Country", "Null", 0);
            Province province = new Province("Province",0, 0);
            Region region = new Region(province, 0);
            region.UpdatePointsAndRect(vectors);
            int countryIndex = map.ProvinceAdd(province);
I have a problem with this code, the province does not appear

procedurally

  • Guest
Re: procedurally
« Reply #3 on: December 04, 2019, 07:39:14 AM »
?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: procedurally
« Reply #4 on: December 05, 2019, 04:45:35 PM »
Hi,

Use this code:


Code
        /// <summary>
        /// Creates a province with a name and list of points.
        /// </summary>
        /// <returns>The country index.</returns>
        int CreateProvince(string name, int countryIndex, Vector2[] points) {
            // 1) Initialize a new province
            Province province = new Province(name, countryIndex, 0);

            // 2) Define the land region for this country with a list of points with coordinates from -0.5, -0.5 (bottom/left edge of map) to 0.5, 0.5 (top/right edge of map)
            // Note: the list of points should be expressed in clock-wise order
            Region region = new Region(province, 0);

            region.UpdatePointsAndRect(points);

            // 3) Add the region to the province (a province can also have multiple regions, like islands)
            province.regions = new List<Region>();
            province.regions.Add(region);

            // 4) Add the new country to the map
            int provinceIndex = map.ProvinceAdd(province);
            map.RefreshProvinceGeometry(provinceIndex);
            return provinceIndex;
        }

The call to "RefreshProvinceGeometry" was missing in your code. Reason is that Provinces in the asset are not loaded at the begining. They are loaded and initialized as they're needed (when you pass the mouse over a province for example, the asset checks if the province is loaded and proceeds to initialize it for the first time if needed). This is designed this way because there're +4400 provinces in the geodata files.

In next update that call to "RefreshProvinceGeometry" won't be needed as it will be added directly into the ProvinceAdd method.

Regards