c++ - trouble converting void * back to struct -



c++ - trouble converting void * back to struct -

i'm working on game using box2d physics , having problem void *.

box2d fixtures have (void*) userdata field, doors in level, setting address of door information, struct called nextlevel (below).

struct newlevel { bool changed; std::string name; int x; int y; int posx; int posy; int num; };

the doors stored in level class in std::vector<newlevel> doors. in loop assembles each of levels, if there door, doors.push_back(newlevel()) , set info accordingly. store fixturedef.userdata = &doors.back().

however, when go retrieve information, doesn't work out. (fixb beingness pointer fixture in contact).

newlevel nextdoor = *(static_cast<newlevel*>(fixb->getuserdata()))

sometimes, works. however, @ other times, gives gibberish. when load next level, clear vector. print pointers out lot, sometimes, don't match, cause this? there improve way pass data?

thanks! (i can set more code if needed)

edit:

i ended solving storing b2fixture* box2d , index of item in doors vector, rather trying cast/re-cast pointer newlevel , has been working great. of help, notion of tracking index got me thinking in right direction.

if think it, see vector template class must allowed move info place place. otherwise dynamic resizing (e.g. push_back) wouldn't possible. if take address of element, , alter size of vector, address may become invalid. seeing.

perhaps store index of element fixturedef.userdata = static_cast<void*>(doors.size()-1) ?

c++ box2d

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -