This project has moved. For the latest updates, please go here.

Using UserData

Feb 17, 2011 at 4:39 PM


I have an object that contains a Fixture. in my constructor for that object I have a line:

moduleFixture.UserData = this; 

I believe that this is how I would set the fixtures UserData to the current object.

The problem Im having is when I try to access the data. I need to have an on collision event check that a variable in one of the colliding physics bodies associated objects is set to true.

I would expect that you would use something like:

if(collFixture.UserData.VariableIsTrue()) {}

But Visual studio says you cant access the function "VariableIsTrue()" like that.



Feb 17, 2011 at 5:10 PM
Edited Feb 17, 2011 at 5:13 PM

I probably should just ignore this question as it is not related to Farseer at all but basic C# knowledge. So using Google to look up the error message Visual Studio gave you should have done the trick.

Of course you have to cast UserData to the proper type first. UserData has the type "object" from which all classes in C# are derived, so it is kinda the base class for everything. Use the "is" or "as" operator or just cast it to the proper type. This...

if((collFixture.UserData as MyClass).VariableIsTrue()) { //doStuff }

...or this...

if(collFixture.UserData is MyClass)
	MyClass temp = (MyClass)collFixture.UserData;
	if(temp.VariableIsTrue()) { // doStuff }
should both work. Maybe you should read up a bit on object orientation and inheritance in C# or in general.
Feb 17, 2011 at 5:38 PM

Thank you for responding to my question inspite of how noobish it may be.

My C++ is really strong, but I'm very new to C#