How to access custom properties

Aug 31, 2011 at 10:44 PM
Edited Sep 1, 2011 at 8:31 AM

I set my 2nd map's custom property to:

Name: SpawnPoint

Value: 0, 18

(like the tutorial showed for Maps -> Properties -> Custom properties)


I was wondering on how to access the SpawnPoint object in code. I tried Global.player[i].X = map[1].Properties.Values. but nothing came after that looked familiar.

I tried looking for a specific tutorial on how to access the custom properties from within the C# code, but nothing of interest came up.



I might have figured it out. Does it have to do with:

map[1].Properties.TryGetValue("SpawnPoint", new xTile.ObjectModel.PropertyValue( ??? ));

How do I accept a value of:      0, 16



I figured it out!

xTile.ObjectModel.PropertyValue valueSpawnPoint;

map[1].Properties.TryGetValue("SpawnPoint", out valueSpawnPoint);



How do I convert the string value of:     0, 16      into, let's say, a Vector2(0, 16)?


- MrMiguu

Sep 1, 2011 at 8:53 AM

Ok, firstly, if you set a custom property to the value "0, 16", the editor can only assume this is a string because it cannot convert it to a boolean, integer or floating point value. So you need to retrieve it as a string and then convert it to a vector (see below).


Next, your property retrieval code works, but you could write it more neatly as:


string spawnPointProperty = map[1].Properties["SpawnPoint"];


However be aware that the code will throw an exception if the property is not set, so your code may suit you better if you want to handle such a situation. 


To convert this string to a Vector2, you will need to do some basic string processing. Off the top of my head, something like this:


string[] elements = spawnPointProperty.Split(new char[]{','}); // split string using comma as delimeter

float spawnX = float.Parse(elements[0]); // parse first value

float spawnY = float.Parse(elements[1]); // parse second value

Vector2 spawnPoint = new Vector2(spawnX, spawnY); // build vector


Hope that helps

Sep 1, 2011 at 9:06 AM

That does help tremendously. Thank you so much!