ID:262405
 
Code:
obj/lightable
var
range
intensity
list/atoms
New()
..()
atoms=new/list(range)
//Make each element a list using getring(). Thus making it a multidimensional list(I think)
for(var/i=1,i<range,i++)
atoms[i]=getring(src,i) //Getring returns a list. This is from AbyssDragon's BasicMath library


verb
On()
set src in oview(1)
for(var/i=0,i<=range,i++) // i is zero because of its use later in the verb
//Copy each list found at [i] element
var/list/temp=atoms.Copy((i+1),(i+2)) //ERROR LINE
//For each item copied, set it's intensity according to what index it was(which is relative to how many tiles it is away from src)
for(var/atom/A in temp)
var/j=i*3 //Multiples of 3 might create an unrealistic diminishing effect, but it's good for now
A.text=A.SetIntensity(intensity-j)


runtime error: list index out of bounds
proc name: On (/obj/lightable/verb/On)
source file: lighting.dm,19
usr: Prodigal Squirrel (/mob/char)
src: the torch (/obj/lightable/torch)
call stack:
the torch (/obj/lightable/torch): On()

Problem description:
This is going to be used for a text mode dynamic lighting system. My first step is to get the dissipating effect of a light working. However, when using the verb On(), I get the above runtime error. I've tried other ways of using and modifying the list, but I get problems everytime. I'm just not sure how multi-dimensional lists work.

Prodigal Squirrel

I think you're going one index too far when looping from 0 to range. Rather than going from 0 to range, try 1 to range, and lower thoes... bah, let me just show it. I'm horrible at explaining things.

    verb
On()
set src in oview(1)
for(var/i=1,i<=range,i++) // i=0 is now i=1
var/list/temp=atoms.Copy((i),(i+1)) // +2 is now +1, and +1 is now +0. (Or nothing.)
for(var/atom/A in temp)
var/j=i*3
A.text=A.SetIntensity(intensity-j)


Let me draw a picture on what you were doing.
Loop: 0 - - - 4 (range = 4)
List:   1 2 3 4 (list has len of 4, and is 1-based)

0 + 1 = 1 (valid index)
1 + 1 = 2 (valid index)
 ...
4 + 1 = 5 (not valid)


This is a programmer skill called debugging, where you run through your code, in your head, memorizing each variable as you progress from token to token.