pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber

Home Page:https://cannon.pmnd.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add body existence check in step function

alex-shortt opened this issue · comments

Hello - something about my usage seems to be wrong and I'd love some help figuring that out, but an issue regardless of my setup is the error Cannot read properties of undefined (reading 'position') coming from this line. For some reason my body subscriptions are losing values but not disappearing from state. There should definitely be a check there . I'll submit a pr in a second.

As for what may be wrong with my setup, I'm using the Physics component in a separate component, seen here, which upon re-render may be losing internal state.

Submitted a pr here: #408

As for the cause, after looking at the source I can better describe it as unloaded bodies don't seem to remove subscriptions. So it's probably not the Physics component itself that's resetting since it's still aware of the subscriptions, but that the body is being unloaded without the subscriptions being unloaded.

I've the same error when using api subscription and when objects are removed from the scene. Don't know how but then the api stops working for the other objects too.

yeah i can second this ^ seems to happen when objects are removed from the scene

Could you share a reproduction with us then we can be sure to fix it.

@bjornstar the problem seems clear, as pointed out here, subscriptions aren't being removed when the body is removed. am i missing something?

this is my fix in #411:

const key = Object.keys(state.subscriptions).find((k) => state.subscriptions[k][0] === data.uuid[i])
if (key) {
delete state.subscriptions[key]
}
}

now fixed in #412

I experienced this when I setup a subscription but did not handle the returned unsubscribe function from subscribe.

So kids, eat your veggies and don't forget to unsub

useEffect(() => { 
  const unsub = api.subscribe(...)
  return unsub 
}, [api])

so it gets called when your component unmounts.