Page 1 of 1

[Help] Attempt to get length of field (a nil value)

Posted: Fri Sep 26, 2014 8:30 pm
by 4forpengs
So, I'm making a constructor for draw lists and I have a table in the constructor (not the object that the constructor returns). Whenever I try to access the table, it comes up as non-existent. The constructor returns an object without a problem, however when I try to access the table, I get the error.

Now, I am a bit of a beginner when it comes to Lua, so I apologize if the problem is something overly simple, however, this is the longest I've ever been stuck in programming.

Code: Select all

local DrawList = {}

function DrawList.newDrawList()
  this = {}
  
  this.queue = {} -- This is the table
  print(#this.queue) -- Prints 0

  function this.add(image)

    print(#this.queue) -- Error

    this.queue[#this.queue+1] = image
    image.drawNum = #this.queue
  end
  
  return this
end

return DrawList

Re: [Help] Attempt to get length of field (a nil value)

Posted: Fri Sep 26, 2014 9:05 pm
by Robin
It's hard to say without context (we don't ask for a .love when people have questions for nothing), but you really should use local this = {}.

That way you don't get problems when you redefine the global variable this somewhere else.

Re: [Help] Attempt to get length of field (a nil value)

Posted: Sat Sep 27, 2014 2:55 am
by 4forpengs
Well, I guess I had a stroke because, as you said, I forgot to define the "this"s as local variables.