[SOLVED] Error: attempt to index 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
DarkblooM
Prole
Posts: 3
Joined: Fri Sep 20, 2024 9:29 pm
Location: France
Contact:

[SOLVED] Error: attempt to index a nil value

Post by DarkblooM »

Hello.

I'm taking my first steps into using LÖVE and I'm running into an issue that I can't quite identify.

Here's my main.lua:

Code: Select all

Player = {
  pos = {0, 0},
  speed = 0
}

function Player.new(pos, speed)
  local self = setmetatable({}, Player)
  self.pos = pos or {0, 0}
  self.speed = speed or 300
end

function love.load()
  player = Player.new({0, 0}, 300)
end

function love.update(dt)
  if love.keyboard.isDown("left")  then player.pos[1] = player.pos[1] - player.speed * dt end
  if love.keyboard.isDown("right") then player.pos[1] = player.pos[1] + player.speed * dt end
  if love.keyboard.isDown("down")  then player.pos[2] = player.pos[2] + player.speed * dt end
  if love.keyboard.isDown("up")    then player.pos[2] = player.pos[2] - player.speed * dt end
end

function love.draw()
  love.graphics.circle("fill", player.pos[1], player.pos[2], 10)
end
And here's the error message:

Code: Select all

Error: main.lua:24: attempt to index global 'player' (a nil value)
stack traceback:
	[string "boot.lua"]:777: in function '__index'
	main.lua:24: in function 'draw'
	[string "boot.lua"]:618: in function <[string "boot.lua"]:594>
	[C]: in function 'xpcall'
If needed, here's the packaged project: https://files.catbox.moe/k27voy.love


=== Solution ===
pgimeno wrote: Sat Sep 21, 2024 4:34 am Hello, welcome to the forums. You're using the return value from Player.new() to set the variable `player`, yet you've forgetting to return a value within Player.new(). Simply add `return self` at the end of the function.
Last edited by DarkblooM on Sat Sep 21, 2024 8:18 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error: attempt to index a nil value

Post by pgimeno »

Hello, welcome to the forums. You're using the return value from Player.new() to set the variable `player`, yet you've forgetting to return a value within Player.new(). Simply add `return self` at the end of the function.
User avatar
DarkblooM
Prole
Posts: 3
Joined: Fri Sep 20, 2024 9:29 pm
Location: France
Contact:

Re: Error: attempt to index a nil value

Post by DarkblooM »

pgimeno wrote: Sat Sep 21, 2024 4:34 am Hello, welcome to the forums. You're using the return value from Player.new() to set the variable `player`, yet you've forgetting to return a value within Player.new(). Simply add `return self` at the end of the function.
Ah OK, I'm still not fully used to "classes" in Lua haha. Thanks for the help.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Error: attempt to index a nil value

Post by pgimeno »

You're welcome. Unrelated to classes, though, if you're using the return value of a function, that function needs to return something; otherwise you'll always get nil from it.
User avatar
DarkblooM
Prole
Posts: 3
Joined: Fri Sep 20, 2024 9:29 pm
Location: France
Contact:

Re: [SOLVED] Error: attempt to index a nil value

Post by DarkblooM »

pgimeno wrote: Sat Sep 21, 2024 9:22 am You're welcome. Unrelated to classes, though, if you're using the return value of a function, that function needs to return something; otherwise you'll always get nil from it.
Yeah I know that, I just assumed that 'self' would be automatically returned, like in Python
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot], Google [Bot] and 18 guests