hi
im making a game based on box2d
here is what im doing:
im making a 4 box's that fall i need to know when thay step out of bounds and this is what im doing:
im creating 4 box's like this and giving them an id in the "USERDATA" and it need's to be a pointer so here is the code:
STUDENT mary;
STUDENT * pupil;
pupil = &mary;
pupil->age=x;
pupil->name = 0;
def.userData=pupil;
b2Body* pBody = bWorld->CreateBody( &def );
then when there is an outof bounds event i try to get the name from here:
class MyBoundaryListener : public b2BoundaryListener
{
void Violation(b2Body* body)
{
STUDENT *sprite = (STUDENT*)body->GetUserData();
if(sprite->name == 0){
nodrawball();
}else if(sprite->name == 1){
removeFromCastleDrawing();
}
// nuke[nukeCount++] = body;
}
};
sprite->name returs not "0" but some long number: 83.023245
what am i doing wrong?
am i not setting the pointer ok?
10x
I was never any good at pointers so anything I say may be and probably is totally incorrect =D . Try putting a "&" in front of it to de-reference it.
At first i thought you were getting an address but then i figured it would come like 0xSOMETHING, so it might not be the case of dereferencing it...
Are you using the Box2D port that they have in the forum? Because if you are, maybe there might be a way of doing what you want without going into the evil "C" :S
yes im useing the port
Is sprite->name defined as char pointer (char *) ?
If so, then you need to treat the variable as a "string" (and yes, I know C doesn't actually have string variables :) ), so you would do something like :
f(sprite->name == NULL){
nodrawball();
}else if(sprite->name){
removeFromCastleDrawing();
}
Instead.
well
i use a pointer to a structure
there are 2 vars
1. int id
2. char name
and i cant get the data out :(
maybe i sould use DGStr?
10x
As name is a char, then it should be easy enough to get the name :
DEBUG(sprite->name);
should display it
If you want to perform string manipulation on it, then you will need to convert to DGStr :
DGStr temp;
temp=DGStr(sprite->name);
well
still weird
this is what i did
i changed the name to int and the box name param is:99
then i do this on the boundy event
class MyBoundaryListener : public b2BoundaryListener
{
void Violation(b2Body* body)
{
dataHolder *sprite = (dataHolder *)body->GetUserData();
DEBUG(sprite->nameid);
};
on the debug consule i get 64??!?!? not 99
am i creating someting wrong??!
10x
It would be silly changing name to an int if its supposed to be a char *
Its possible that the data wasn't set properly with the opposite to GetUserData (whatever it is)
ok fix'd :)
didnt create new in the "C" code :)
the pointer was leading to grabage :)
sorry
thank you for your help
be sure to delete it when you destroy the object. It's really bad. I recommend casting the pointer to an integer and put a string in a list and use the index in that list as the reference to the name.