See Demo Scenes 104a/104b
Suppose your game changes geodata at runtime. It could change country name, or population, or the relationship between a province and its country, or even change frontiers of countries and provinces. Then you want to save the modified data so the player can resume the game in the future, how to do that?
Well, WMSK does not provide you a persistence layer, like a local database, but it provides you the required functions so you can get and set the state of the entities (countries, provinces, cities and mount points) in a formatted string so you can store that text information wherever you want and reload it in a later moment.
The functions are split in 3 groups and are available for countries, provinces, cities and mountpoints:
- Get/Set borders data in packed string format (optimized for storage).
- Get/Set attributes data in jSON format (attributes can be assigned using the Map Editor or through the .attrib property, eg: country.attrib[“currency”] = “euro”).
- Get/Set all data (borders and attributes) in jSON format.
GetCountryGeoData(): returns a string containing the countries information, including the frontiers data. SetCountryGeoData(string): loads the countries information, including frontiers data, from the given string.
Optional functions (only useful if you are adding custom attributes to countries):
GetCountriesAttributes(): returns a string in jSON format containing any country attribute (the contents of the .attrib field for each country). SetCountriesAttributes(string): loads the countries attributes from the given string.
The following methods use jSON format and include everything related to countries (geodata plus attributes in a single call):
GetCountriesDataJSON(bool prettyPrint = true): returns a string in jSON format containing all countries data (frontiers and attributes). SetCountriesDataJSON(string s): loads the countries data from the given string in jSON format.
Provinces, Cities and MountPoints
Similar functions are available for provinces, cities and mountpoints (GetProvincesGeoData(), …).
Where can I store the geodata?
GetCountryGeoData(), GetProvinceGeoData(), … all returns a string with the required information. You can store this string in:
- A local text file in the Application data folder. Example: System.IO.
- A local database like SQLite.
- A remote database using a webservice.
For example, to save the country information to a local text file you could do:
string s = map.GetCountryGeoData(); System.IO.File.WriteAllText(Application.persistentDataPath + "/myCountries", s);
And to retrieve the country geodata in a future session you would do:
string s = System.IO.File.ReadAllText(Application.persistentDataPath + "/myCountries"); map.SetCountryGeoData(s);