I have a recurrent problem in which I keep making errors, mainly logistic, but sometimes technical. But, I don’t seem to know how to resolve some of them. Like the following - I have an array which keeps throwing up errors in use or in trying to print out the array. One of the elements in the array must be duff - but I can’t find which one.
Here is an example of the array, trying to set up a 3D matrix. Is there any way in which we could get Codea to run through the procedure and print out to a file/screen so that I could identify the element. Instead I get the printout:
main.lua:27: attempt to concatenate field ‘?’ (a nil value) - which is not very helpful - I need to trap the error.
function setup()
--
points = { x = {}, y = {}, z = {}}
radius = 100
antorad = math.pi/180
index = 1
for i = 0, 360, 30 do
for j = 0, 360 do
points.x[i] = radius * math.sin(i * antorad) * math.cos(j * antorad)
points.y[i] = radius * math.sin(i * antorad) * math.sin(j * antorad)
points.z[i] = radius * math.cos(i * antorad)
index = index + 1
end
end
end
function draw()
--
for loop = 1, index do
text("Array is "..loop.." x: "..points.x[loop].." y: "..points.y[loop].." z: "..points.z[loop], 100, 200+loop*20)
end
end
May, or may not, be relevant but the code and error were taken from Love2D - which I tend to use for early trials.
I think your main problem is that your index value ends up being far larger than the number of items in your table. If you print the value of index at the end of your setup function, its value is 4694.
So, your for loop in your draw() method will try and print out 4694 values when there are only 14. `points[3435].x for example will give you a nil value because it doesn’t exist.
There are also a number of problems with your for loop in your draw() method:
You start counting from 1, but earlier (in setup) you used 0 as the first index in your table. This means you won’t be printing out the first value.
Your for loop goes through numbers which are only one digit apart: 1, 2, 3, 4, 5… However, in your setup you used the following indexes: 0, 30, 60, 90, 120, 150…
You could just do the following:
for loop = 0, 360, 30 do
text("Array is "..loop.." x: "..points.x[loop].." y: "..points.y[loop].." z: "..points.z[loop], 100, 200+loop*20)
end