GLBasic forum

Main forum => GLBasic - en => Topic started by: djtoon on 2010-Oct-03

Title: A little bit of help from the "C" people :)
Post by: djtoon on 2010-Oct-03
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
Title: Re: A little bit of help from the "C" people :)
Post by: FutureCow on 2010-Oct-03
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.
Title: Re: A little bit of help from the "C" people :)
Post by: Leos on 2010-Oct-03
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
Title: Re: A little bit of help from the "C" people :)
Post by: djtoon on 2010-Oct-03
yes im useing the port
Title: Re: A little bit of help from the "C" people :)
Post by: MrTAToad on 2010-Oct-03
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 :

Code (glbasic) Select
f(sprite->name == NULL){
     nodrawball();
    }else if(sprite->name){

    removeFromCastleDrawing();
    }


Instead.
Title: Re: A little bit of help from the "C" people :)
Post by: djtoon on 2010-Oct-03
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
Title: Re: A little bit of help from the "C" people :)
Post by: MrTAToad on 2010-Oct-03
As name is a char, then it should be easy enough to get the name :

Code (glbasic) Select
DEBUG(sprite->name);

should display it

If you want to perform string manipulation on it, then you will need to convert to DGStr :

Code (glbasic) Select
DGStr temp;

temp=DGStr(sprite->name);
Title: Re: A little bit of help from the "C" people :)
Post by: djtoon on 2010-Oct-04
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
Title: Re: A little bit of help from the "C" people :)
Post by: MrTAToad on 2010-Oct-04
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)
Title: Re: A little bit of help from the "C" people :)
Post by: djtoon on 2010-Oct-04
ok fix'd :)
didnt create new in the "C" code :)
the pointer was leading to grabage :)
sorry

thank you for your help
Title: Re: A little bit of help from the "C" people :)
Post by: Kitty Hello on 2010-Oct-04
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.