Why is this variable nil? :/

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.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Why is this variable nil? :/

Post 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
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Why is this variable nil? :/

Post 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.
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Why is this variable nil? :/

Post 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
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Why is this variable nil? :/

Post by Nixola »

love.main should be love.load.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Why is this variable nil? :/

Post 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
User avatar
easy82
Party member
Posts: 184
Joined: Thu Apr 18, 2013 10:46 pm
Location: Hungary

Re: Why is this variable nil? :/

Post 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. ;)
User avatar
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

Re: Why is this variable nil? :/

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Why is this variable nil? :/

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Why is this variable nil? :/

Post 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)
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Why is this variable nil? :/

Post 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)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests