Cannot read property "time" of undefined error!
See original GitHub issueHey there! I followed your “long version” overview of the SI, really great video btw, presents concepts very well. The problem comes from the server reconciliation function section. The responsible code line in my app, which triggers the error is:
const playerSnapshot = playerVault.get( serverSnapshot.time, true );
When running my game locally, a couple of minutes after start (at random time), the lib crashes with an error:
Looking deeper it points to the Vault.get()
method:
Turns out the problem actually lies in line 23:
var snaps = { older: sorted[i], newer: sorted[i - 1] };
I am running into a situation where if (snap.time <= time) {
is passed as true, when the for
loop is on it’s first iteration. Then the method tries to access the sorted
array with a negative index of -1
, returning undefined
. Since snaps.newer
is undefined
, it throws an error when it tries to access snaps.newer.time
.
To fix this I patched the function with an if
check after if (closest) {
:
if (closest) {
if(snaps.newer == undefined) return;
Since I check whether the returned snapshot is undefined
before continuing, my server reconciliation just return
s for that particular frame.
I checked with a console log, and Im fine, as it doesn’t happen that often - around 3 times per 10 minutes. I dont know the inner workings of networking so this is very unpredictable to me and may be hard to reproduce the bug.
I dont know the library well, so I am not sure why this happens, or why does it happen so rarely. Should there be such an if check, like I’ve implemented? What do you think?
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
It’s good, but I’d prefer to use a non optional chaining solution, just to be on the safe side of bundlers and transpilers.
Don’t worry about that. The code will automatically be transpiled by the TypeScript compiler. The final code will look like this: