Page 1 of 2

Why is this variable nil? :/

Posted: Thu Dec 15, 2016 5:32 am
by Vimm
I'm trying to make a player move around, I'm doing it the same way i always do, yet for some reason now, xVel is nil :/ can someone find an issue im missing?

Code: Select all

player =  {}

function player:load(x,y)
	self.width = 128
	self.height = 128

	self.speed = 500 

	self.xVel = 0
	self.yVel = 0

	self.acceleration = 1500
	self.friction = 750

	self.x = x
	self.y = y

	self.grounded = false
end

function player:update(dt)
	self:move(dt)
	self:control(dt)
end

function player:move(dt)
	self.x = self.x + self.xVel * dt
	self.y = self.y + self.yVel * dt
end

function player:control(dt)
	if love.keyboard.isDown("left" or "a") then
		if self.xVel < -self.speed and self.xVel - self.acceleration * dt < -self.speed then
			self.xVel = self.xVel - self.acceleration 
		elseif self.xVel < -self.speed and self.xVel - self.acceleration * dt > -self.speed then
			self.xVel = -self.speed 
		end
	end
end

function player:draw()
	love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end

function LOAD_PLAYER(x,y)
	player:load(x,y)
end

function UPDATE_PLAYER(dt)
	player:update(dt)
end

function DRAW_PLAYER()
	player:draw()
end

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 6:33 am
by Jasoco
The problem isn't in that code. It's somewhere else in the project. Most likely you're trying to update the player before it's been loaded. But we can't tell without more information.

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 7:02 am
by Vimm
Jasoco wrote:The problem isn't in that code. It's somewhere else in the project. Most likely you're trying to update the player before it's been loaded. But we can't tell without more information.
the only other file in the project is main:

Code: Select all

require("player")

function love.main()
	LOAD_PLAYER(500, 200)
end

function love.update(dt)
	UPDATE_PLAYER(dt)
end

function love.draw()
	DRAW_PLAYER()
end

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 7:52 am
by Nixola
love.main should be love.load.

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 8:07 am
by Vimm
Nixola wrote:love.main should be love.load.
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 8:11 am
by easy82
Vimm wrote:
Nixola wrote:love.main should be love.load.
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself
This is the best way of learning! :D Now you'll always check it twice. ;)

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 9:21 am
by Vimm
easy82 wrote:
Vimm wrote:
Nixola wrote:love.main should be love.load.
HOW THE *************************************** DID I MISS THAT
ARE YOU KIDDING
thank you for being my eyes
can i please have a pair of glasses
or new eyes all together
possibly a functioning brain if they're in stock
i hate myself
This is the best way of learning! :D Now you'll always check it twice. ;)
XD yeah i will, ill like stare it down for 5 minutes everytime before advancing from now on Xd

Re: Why is this variable nil? :/

Posted: Thu Dec 15, 2016 11:42 am
by s-ol
The way you could've/should've found that yourself is by working backwards from the problem: you see the error mentioning xVel, so you try to print it in that function for debugging. It's nil, so you put a print where you thought you set it (player:load()) and are surprised to see it never gets executed. Working back from that you would put a print in LOAD_PLAYER, then love.main, at which point you would've probably realized your mistake.

Re: Why is this variable nil? :/

Posted: Mon Dec 19, 2016 9:43 am
by Jeeper
s-ol wrote:The way you could've/should've found that yourself is by working backwards from the problem: you see the error mentioning xVel, so you try to print it in that function for debugging. It's nil, so you put a print where you thought you set it (player:load()) and are surprised to see it never gets executed. Working back from that you would put a print in LOAD_PLAYER, then love.main, at which point you would've probably realized your mistake.
Just want to say that this is the important thing to take away from it all. Everyone makes these kinds of misstakes, but if you know proper debugging you will find it quickly. Print a lot and explain your code to yourself (or do what I do and use a duck: https://en.m.wikipedia.org/wiki/Rubber_duck_debugging)

Re: Why is this variable nil? :/

Posted: Mon Dec 19, 2016 5:54 pm
by Beelz
I personally vouch for the rubber duck method. Whether with a duck, a person, or just to an imaginary friend. Use the ELIF method. (Explain like I'm five)