## Why is this variable nil? :/

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

### Why is this variable nil? :/

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 =  {}

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

end

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

function DRAW_PLAYER()
player:draw()
end

Jasoco
Inner party member
Posts: 3650
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: Why is this variable nil? :/

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.

Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

### Re: Why is this variable nil? :/

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()
end

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

function love.draw()
DRAW_PLAYER()
end

Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: Why is this variable nil? :/

lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics

Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

### Re: Why is this variable nil? :/

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

easy82
Party member
Posts: 184
Joined: Thu Apr 18, 2013 10:46 pm
Location: Hungary

### Re: Why is this variable nil? :/

Vimm wrote:
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! Now you'll always check it twice.

Vimm
Party member
Posts: 113
Joined: Wed Mar 16, 2016 8:14 pm

### Re: Why is this variable nil? :/

easy82 wrote:
Vimm wrote:
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! 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

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

### Re: Why is this variable nil? :/

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

Jeeper
Party member
Posts: 546
Joined: Tue Mar 12, 2013 7:11 pm

### Re: Why is this variable nil? :/

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)

Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

### Re: Why is this variable nil? :/

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

### Who is online

Users browsing this forum: Google [Bot] and 3 guests