I get a very annoying error when I do something similar with arrays. I have code that sets an array in the love.load () function:
function iceToolsInit() objectArray = {} --for object handling objectArrayLocation = 0 end
and then code that allows you to create an object. It basically captures all the information about the specified object and inserts it into an array.
function createObject(x, y, renderimage) --used in the load function --objectArray is set up in the init function objectArrayLocation = objectArrayLocation + 1 objectArray[objectArrayLocation] = {} objectArray[objectArrayLocation]["X"] = x objectArray[objectArrayLocation]["Y"] = y objectArray[objectArrayLocation]["renderimage"] = love.graphics.newImage(renderimage) end
After that, the update function reads the objectArray object and accordingly displays the images:
function refreshObjects() --made for the update function arrayLength = #objectArray arraySearch = 0 while arraySearch <= arrayLength do arraySearch = arraySearch + 1 renderX = objectArray[arraySearch]["X"] renderY = objectArray[arraySearch]["Y"] renderimage = objectArray[arraySearch]["renderimage"] if movingLeft == true then --rotation for rightfacing images renderRotation = 120 else renderRotation = 0 end love.graphics.draw(renderimage, renderX, renderY, renderRotation) end end
Of course, I cut off some unnecessary code (just additional parameters in the array, such as width and height), but you get the gist. When I create this code to create one object and render it, I get this error:
attempt to index '?' (a nil value)
the line this line points to:
renderX = objectArray[arraySearch]["X"]
Does anyone know what is wrong here, and how could I prevent this in the future? I really need help with this.