Scrolling Background Function - Self is 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
harrison
Prole
Posts: 17
Joined: Tue Feb 03, 2015 5:24 am

Scrolling Background Function - Self is a nil Value

Post by harrison »

I cannot figure this out, my local self table seems to be disappearing into thin air for no apparent reason. I think this is a pure lua error, I don't think it is the result of me abusing love. I am probably just missing something obvious but I have been trying to figure out why this is for half an hour... I just need another set of eyes to find the error.

Help would be greatly appreciated.

In case you were wondering, this is a function to create scrolling backgrounds without polluting main.lua with messy code.

The code:

Code: Select all

background = {}

function background:new(image,scrollSpeed)
	local self = {}
		self.image = image
		self.a = {}
			self.a.x = 0
			self.a.y = 0
		self.b = {}
			self.b.x = 0
			self.b.y = -self.image:getHeight()
		self.scrollSpeed = scrollSpeed or 0
print(self) --SELF IS NOT NIL
	
		function self:update(dt)
print(self) --SELF IS NOT NIL
			self.a.y = self.a.y + (self.scrollSpeed * dt)
			self.b.y = self.b.y + (self.scrollSpeed * dt)
			
			if self.a.y > love.graphics.getHeight() then
				self.a.y = -self.a.image:getHeight()
			end
			if self.b.y > love.graphics.getHeight() then
				self.b.y = -self.b.image:getHeight()
			end
		end
print(self) --SELF IS NOT NIL
		function self:draw()
print(self) --SELF IS NIL
			love.graphics.draw(self.image, self.a.x, self.a.y)
			love.graphics.draw(self.image, self.b.x, self.b.y)
		end
		
	return self
end


The error log:

Code: Select all

table: 0x12178488
table: 0x12178488
table: 0x12178488
nil
Error: background.lua:30: attempt to index local 'self' (a nil value)
stack traceback:
        background.lua:30: in function 'draw'
        main.lua:39: in function 'draw'
        [string "boot.lua"]:467: in function <[string "boot.lua"]:435>
        [C]: in function 'xpcall'
Here is a link to the full .love file:
https://www.dropbox.com/s/solszghuslzqn ... .love?dl=1
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Scrolling Background Function - Self is a nil Value

Post by airstruck »

harrison wrote:I am probably just missing something obvious
Looks like it.
main.lua:39: in function 'draw'
Take a closer look at that line.
User avatar
harrison
Prole
Posts: 17
Joined: Tue Feb 03, 2015 5:24 am

Re: Scrolling Background Function - Self is a nil Value

Post by harrison »

airstruck wrote: Take a closer look at that line.
Aha! Thank you so much! :ultraglee:

I forgot to pass self into the function and was using a . instead of a :
I am not used to my errors occurring outside of the file i am in, so I usually just ignore the traces. That's why I was missing it. I will remember to do that in the future.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 221 guests