# Arrays Erro

## Recommended Posts

I get an error when using this function:

Func MakePos(\$posx, \$posy)

Local \$pos[2][4]

For \$i = 0 To 4 Step 1

\$ran1 = Random(0, 4)

\$ran2 = Random(0, 4)

\$ran3 = Random(1, 2)

\$ran4 = Random(1, 2)

If \$ran3 = 1 Then

\$pos[0][\$i] = \$posx + \$ran1

\$pos[1][\$i] = \$posy - \$ran2

Else

\$pos[0][\$i] = \$posx - \$ran1

\$pos[1][\$i] = \$posy + \$ran2

EndIf

Next

Return \$pos

EndFunc

The error: http://bayimg.com/faedMaaCi

##### Share on other sites

`Local \$pos[2][5]`

0, 1, 2, 3, 4 <- that's 5 elements

.

eMyvnE

##### Share on other sites

I get an error when using this function:

Func MakePos(\$posx, \$posy)

Local \$pos[2][4]

For \$i = 0 To 4 Step 1

\$ran1 = Random(0, 4)

\$ran2 = Random(0, 4)

\$ran3 = Random(1, 2)

\$ran4 = Random(1, 2)

If \$ran3 = 1 Then

\$pos[0][\$i] = \$posx + \$ran1

\$pos[1][\$i] = \$posy - \$ran2

Else

\$pos[0][\$i] = \$posx - \$ran1

\$pos[1][\$i] = \$posy + \$ran2

EndIf

Next

Return \$pos

EndFunc

The error: http://bayimg.com/faedMaaCi

You defined the array:

Local \$pos[2][4]

When you count from 0 to 4, that actually 5 elements.

Change you loop to this

```Func MakePos(\$posx, \$posy)
Local \$pos[2][4]
For \$i = 0 To 4 Step 1
\$ran1 = Random(0, 4)
\$ran2 = Random(0, 4)
\$ran3 = Random(1, 2)
\$ran4 = Random(1, 2)
If \$ran3 = 1 Then
\$pos[0][\$i] = \$posx + \$ran1
\$pos[1][\$i] = \$posy - \$ran2
Else
\$pos[0][\$i] = \$posx - \$ran1
\$pos[1][\$i] = \$posy + \$ran2
EndIf
Next
Return \$pos
EndFunc```

##### Share on other sites

`Local \$pos[2][5]`

0, 1, 2, 3, 4 <- that's 5 elements

Many thanks!!!

## Create an account

Register a new account

×

• Wiki

• Back

• Git