C# scripting error. Index was out of range(line:0)


(chiharu) #1


I am a programinng biginer.
I wrote script in C# but it doesn’t work.
error line is 0.
I don’t know what mean is it.

I read following the topic but I cannot resolve.
/Scripting C# Index problem.return error:Index was out of range.
(Scripting C# Index problem. returns error: Index was out of range)

I am grad if someone help me.

my gh-file:scriptingerror.gh (24.2 KB)

(David Rutten) #3

The code needs to be broken up into smaller functions that do less. Ideally a function does only a single thing. That way it becomes much easier to test whether what you’ve written actually works the way you intended.

(David Rutten) #4

I started with some cleaning, but there’s too much going on at once for me to understand what the code is supposed to do.

I’d recommend not nesting loops deeper than one level. If you find yourself writing a loop inside a loop, try and refactor the inner loop into a separate method. You’re also probably better off creating a custom type for your panel grid which has bounds-checked accessor methods, so you can get rid of all that low level code in the main algorithm.

If you can explain the steps this algorithm is supposed to take I may be able to help you further.

scriptingerror_refactored.gh (13.3 KB)

(chiharu) #5

Thank you.:heart_eyes:
I am greatful for your help.

What I want to do is to find some wall pattern with following conditons.
1.Two panels are not arranged side by side continuously
2.three or more panel are not arranged verticaly continuously
3.all panel are connected

Now,in my script
When p[i,j] is 1, a panel is arranged in that cell.

my algorithm is
1, I randomly place a panel
2, Cells that can not be placed are set to 0
3, Find blank cell around the cell had 1(the cell arranged panel)
4, Choose blank cells(in 3) randomly and place a panel

Sorry for bad explanation…:persevere:
I am glad if you help me.

(chiharu) #6


I find error code but I don’t know why the code is error.
If you know,could you tell me the reason and how to resolve?

errorcode.gh (27.9 KB)

error code is in line 135.