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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
4forpengs
Prole
Posts: 2
Joined: Fri Sep 26, 2014 7:55 pm

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

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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.
Help us help you: attach a .love.
4forpengs
Prole
Posts: 2
Joined: Fri Sep 26, 2014 7:55 pm

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

Post by 4forpengs »

Well, I guess I had a stroke because, as you said, I forgot to define the "this"s as local variables.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests