fabmax / physx-js-webidl

Javascript WASM bindings for Nvidia PhysX
MIT License
119 stars 28 forks source link

Retrieving userData #31

Closed ghost closed 8 months ago

ghost commented 8 months ago

Hey fabmax, i have been banging my head and yet haven't been able to retrieve the data i put inside the userData attribute, it seems to return a pointer(VoidPtr), but i see there is no way to dereference it. I appreciate your help in advance.

fabmax commented 8 months ago

Ah yes I don't think there's an easy way to use this in javascript unfortunately...

The problem is that the userData field indeed is a void pointer so you can only put a reference to another native object there. A relatively simple workaround is to use a dictionary instead: You can use the memory address of any native object as key and map that to any object you like:

let userDataMap = { }

// on creating an actor
userDataMap[someActor.ptr] = "some arbirtray user data";

// retrieve user data sometime later, e.g. inside a callback function
let userData = userDataMap[actor.ptr]

The ptr field contains the native memory address which is unique and doesn't change for the entire lifetime of a native object.

ghost commented 8 months ago

That's a clever a approach. I was looking to insert an unique id inside the actor anyways, so no userData required in my case, i hope these issues are useful for anyone considering this library. Have a very nice day, fabmax.