Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
Index of array exists when logged to console but does not exist when accessed directly

I have a 2-dimensional array that stores "chunks" as canvases in a game. I've encountered a weird problem that seems to have manifested itself seemingly without me changing anything. The range can go negative, so that could be a cause.

When I run a loop to go through and log every index of the array, it only actually logs index -1. When logging the entire 2-dimensional array, indexes -1, 0, and 1 exist. However when I access index 0 of said array it returns undefined:

Code used to log array

Code used to log array

Actual logged results

Logged results

It's clear that index 0 does exist in the array, but when accessed directly it does not seem to exist and a loop through every index does not think index 0 exists.

This is a pretty bad issue since now the game will re-render a bunch of canvases, killing fps, and making the game unplayable. I don't know why this is happening, and I really have no idea why this is happening. This wasn't happening before and I haven't touched the code, but I discovered my fps was dying and eventually found that the cause was the continuous re-rendering of chunks that already existed.

I've tried a few things, including:

  • Changing all array indexes to integers with parseInt()
  • Trying to access the index as a string
  • Removing all other code that manipulates the MAPS variable

It doesn't seem to be easily reproducible since I don't know the cause, but I will try and find the exact reason why it seems to do this.

Update: It seems to work when directly accessed through console, however in the function it doesn't work.

UPdate

Ignore the two logs between the entry and the output, that's caused by a bit of lag.

Update 2: The length of the array appears to be negative, though it still has positive indexes.

update2

Update 3: I just checked GitHub Desktop and I have not changed anything related to the array, and it does not happen on the older code. I don't know what could have made this suddenly happen.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It turns out the "handy trick" to clearing an array does not work - setting the length of an array to 0 does not always clear it. It ends up doing something and does not seem to work correctly in my case.

Also it turns out I was accidentally clearing portions of the array.

Sorry.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda