Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

Multidimensional Array Problem

Started by jadendecar 1 reply 900+ views
Original Post
jadendecar
jadendecar
So I am using a multdimensional array to create a tiled background. It works fine until I try to use a for loop to draw the array, at which point it throws an indexoutofrangeexception.

I declare the variable as:
[source lang="csharp"]Vector2[,] grass = new Vector2[25, 20];[/source]

Then to assign it values I use:
[source lang="csharp"]for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 20; j++)
{
grass[i,j] = new Vector2(i*32,j*32);
}
}[/source]

And to draw it I call:
[source lang="csharp"]for (int i = 0; i < 25; i++)
{
for (int j = 0; i < 20; j++)
{
spriteBatch.Draw(grasstile, grass[i, j], Color.White);
}
}[/source]

It works fine up until the last bit. It gets declared, and the variables are assigned correctly (I manually called each index of the array and it worked fine), yet for some reason the draw throws Indexoutofrangeexception every time it's called.

Maybe I'm just overlooking something simple but I'm stumped right now.
froop
froop
are you sure that you mean "i < 20" in the drawing loop?
Tasaq
Tasaq
In the draw loop you have i instead of j smile.png
I have done similar mistake in shader, where instead of y i wrote x and i couldn't see it lol biggrin.png

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.