Attempt to index local 'self' (a nil value)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Attempt to index local 'self' (a nil value)

Post by Nixola »

I've got this code:

Code: Select all

local gui = {}
gui.load = function(self)
	self.__checkboxes = {}
	self.__labels = {}
	self.__buttons = {}
	self.__drawables = {'checkboxes', 'labels', 'buttons'}
	self.__clickables = {'checkboxes', 'buttons'}
end
gui:load()
And it throws me that error (Attempt to index local 'self' (a nil value)) at line 3, even without calling the function (gui:load()). Am I doing something wrong?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Attempt to index local 'self' (a nil value)

Post by TechnoCat »

There is more to this story than you are telling:
http://codepad.org/Hm0w1iA1
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Attempt to index local 'self' (a nil value)

Post by Nixola »

I didn't see some lines (I actually commented out things instead of deleting them), here are my files:
gui.lua

Code: Select all

local gui = {}
gui.load = function(self)
	self.__checkboxes = {}
	self.__labels = {}
	self.__buttons = {}
	self.__drawables = {'checkboxes', 'labels', 'buttons'}
	self.__clickables = {'checkboxes', 'buttons'}
end

gui.erase = function(self)
	self.load()
end

return gui
main.lua

Code: Select all

gui = require 'gui'
function love.load()
  gui:erase()
end
.love
gui.love
(2.42 KiB) Downloaded 154 times

EDIT: I hate typos. :x
Semicolon instead of dot inside gui.erase() (as it was supposed to be) fixed it.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Attempt to index local 'self' (a nil value)

Post by rokit boy »

Nixola wrote:I didn't see some lines (I actually commented out things instead of deleting them), here are my files:
gui.lua

Code: Select all

local gui = {}
gui.load = function(self)
	self.__checkboxes = {}
	self.__labels = {}
	self.__buttons = {}
	self.__drawables = {'checkboxes', 'labels', 'buttons'}
	self.__clickables = {'checkboxes', 'buttons'}
end

gui.erase = function(self)
	self.load()
end

return gui
main.lua

Code: Select all

gui = require 'gui'
function love.load()
  gui:erase()
end
.love
gui.love

EDIT: I hate typos. :x
Semicolon instead of dot inside gui.erase() (as it was supposed to be) fixed it.
use GUI:load
u wot m8
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Attempt to index local 'self' (a nil value)

Post by Ref »

Not sure what's the issue is here.

Code: Select all

local gui = {}
function gui:load()
   self.__checkboxes = {}
   self.__labels = {}
   self.__buttons = {}
   self.__drawables = {'checkboxes', 'labels', 'buttons'}
   self.__clickables = {'checkboxes', 'buttons'}
end

function gui:erase()
   self:load()
end

gui:erase()
print(gui.__drawables[1]) -- returns checkboxes
This works fine for me.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Attempt to index local 'self' (a nil value)

Post by Nixola »

The error was 'self.load' in 'gui.erase', it should be 'self:load'
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Attempt to index local 'self' (a nil value)

Post by rokit boy »

Nixola wrote:The error was 'self.load' in 'gui.erase', it should be 'self:load'
Told ya.
u wot m8
Post Reply

Who is online

Users browsing this forum: No registered users and 148 guests