Okay so lets say I have a data structure like this:
Then I later add a body with it as userdata like this:
Then later in my code I want to access someNumber, so I do something like this:
Now... When I try to access someNumber like this everything compiles just fine, but my program will crash ("Box2dTest.exe has encountered a problem and needs to close. We are sorry for the inconvenience."). If I comment out the if statement where I try to access data->someNumber, everything runs fine.
If anyone could tell me what I'm doing wrong, it would be greatly appreciated =).
Oh and I hope I gave enough information... if you need any more, I'll do my best.
Thanks!
Code:
struct MyBodyData
{
int someNumber;
};
{
int someNumber;
};
Then I later add a body with it as userdata like this:
Code:
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, -10.0f);
b2Body* body = world.CreateBody(&bodyDef);
MyBodyData bodyData;
bodyData.someNumber = 4;
body->SetUserData(&bodyData);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(50.0f, 10.0f);
body->CreateShape(&shapeDef);
bodyDef.position.Set(0.0f, -10.0f);
b2Body* body = world.CreateBody(&bodyDef);
MyBodyData bodyData;
bodyData.someNumber = 4;
body->SetUserData(&bodyData);
b2PolygonDef shapeDef;
shapeDef.SetAsBox(50.0f, 10.0f);
body->CreateShape(&shapeDef);
Then later in my code I want to access someNumber, so I do something like this:
Code:
for (b2Body* b = world.GetBodyList(); b; b = b->GetNext())
{
MyBodyData* data = (MyBodyData*)b->GetUserData();
if (data->someNumber == 4)
{
//do something
}
}
{
MyBodyData* data = (MyBodyData*)b->GetUserData();
if (data->someNumber == 4)
{
//do something
}
}
Now... When I try to access someNumber like this everything compiles just fine, but my program will crash ("Box2dTest.exe has encountered a problem and needs to close. We are sorry for the inconvenience."). If I comment out the if statement where I try to access data->someNumber, everything runs fine.
If anyone could tell me what I'm doing wrong, it would be greatly appreciated =).
Oh and I hope I gave enough information... if you need any more, I'll do my best.
Thanks!