Page 1 of 1
Tag Properties
Posted: Sat Sep 15, 2012 2:44 am
by Stevemata
This is more of a general c#/xna question, but how do I use the tag property contained within entitycollidable? I would like to add a custom object for storing information that can be read and processed inside of a collision event.
Re: Tag Properties
Posted: Sat Sep 15, 2012 2:52 am
by Norbo
Just set the entity.CollisionInformation.Tag to the object instance and, in the event, grab the object stored in the passed in collidable's Tag and cast it to your type. If you don't know for sure beforehand that a collidable will have the correct object type in the Tag property (or any object at all), you can either use an 'is' check before a cast or just use an as-cast and then check for null.
Be careful about storing value types directly in the Tag property. The type of the property is object, so any value type will get boxed. Depending on the usage, this might not be an issue, but it's generally clearest to just use a reference type. You can put value types inside a reference type which is stored in the Tag without any problem, of course.
The Entity.Tag can be used similarly, although you'll have to cast the Collidable/BroadPhaseEntry down to an EntityCollidable to get at the Entity property before you can get to that Tag.
Re: Tag Properties
Posted: Sat Sep 15, 2012 2:57 am
by Stevemata
Wow, that was a nice and simple solution. You rock dude.