Ask coding questions

← Back to all posts
how do i get the value of a place on a grid from the X and the Y
h
AlmightyDeaks

i am trying to make a 2d copy of Minecraft and i am using arrays to save the data, (i will move to a more elegant solution in the future but this should work for now) and i have the array layer out in a grid seen from lines 6 to 15 in scripts.js what i need to do is find what array number it is from the line number and which iteration of the loop in line 18

Voters
AlmightyDeaks
Comments
hotnewtop
Coder100

Have you tried out 2d arrays?

You can access them like this:

each 'row' is the y and each 'column' is the x.

Please do add var or let to variable declarations, and don't forget to add semicolons!

JustCoding123

This function should do it.
If you want to expand the area change 11 to the length of savedgrid's first line.

QuantumCodes

404 not found error.... wheres your repl?
Generally: for a 9x9 grid and i have its value arranged like this:

789
456
123

Then u can create a formula.
Example i have value 9,
I could :
X = (9%3), y = (9//3)
X = 0, y = 3

If 2,
X = 2%3, y=2//3
X = 2, y = 0

RayhanADev

@QuantumCodes please don't ask people to mark your answer as correct :(.

QuantumCodes

@RayhanADev oh, im so sorry. Ive seen many people do that, so i thought its allowed. I wouldnt do this again :) deleted

RayhanADev

@QuantumCodes thank you :D! Also if someone does it feel free to report the post so that a moderator can deal with it.

QuantumCodes

@RayhanADev ok! Btw i got logged out today from replit for no reason. I didnt login from new devices.. do you know any reason? Ive never been logged out like that

RayhanADev

@QuantumCodes tbh idk lemme ask. It happened to everyone.

QuantumCodes

@RayhanADev happened again today even if i changed my password