Tag Properties

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Stevemata
Posts: 33
Joined: Sat Mar 17, 2012 11:11 pm

Tag Properties

Post 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.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Tag Properties

Post 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.
Stevemata
Posts: 33
Joined: Sat Mar 17, 2012 11:11 pm

Re: Tag Properties

Post by Stevemata »

Wow, that was a nice and simple solution. You rock dude.
Post Reply