Page 1 of 1

LUA attempt to index global anim8 a nil value

Posted: Fri Apr 28, 2023 11:26 pm
by l0ki77
Hello I'm a creating a game project for my class and I keep getting the error "attempt to index global anim8 a nil value", which I've tried to figure out for the past couple of hours. I have tried extending anim8 to Player but also get the same error.

Main.lua

function love.load()
Object = require 'libraries/classic'
anim8 = require 'libraries/anim8'
sti = require 'libraries/sti'
gameMap = sti('maps/monMap.lua')
camera = require 'libraries/camera'
tick = require 'libraries/tick'
require 'Player'
require 'Coin'

Coin = Coin()
Player = Player()
cam = camera()

end


function love.update(dt)
Player:update(dt)
Coin:update(dt)
tick.update(dt)

time = time - dt
if time <= 0 then
gameover = true
end


w,h = love.graphics.getDimensions()

if cam.x < w/2 then
cam.x = w/2
end

if cam.y < h/2 then
cam.y = h/2
end

mapW = gameMap.width * gameMap.tilewidth
mapH = gameMap.height * gameMap.tileheight

-- right and bottom edges
if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end



function love.draw()
cam:attach()
gameMap:drawLayer(gameMap.layers["Ground"])
gameMap:drawLayer(gameMap.layers["Tree"])
Player:draw()
Coin:draw()
cam:detach()
end

Player.lua

Player = Object:extend()

function Player:new()
self.image = love.graphics.newImage("sprites/player-sheet.png")
self.x = 100
self.y = 100
self.speed = 300
self.width = love.graphics.getWidth()
self.height = love.graphics.getHeight()
self.grid = anim8.newGrid(12, 18, self.image:getWidth(), self.image:getHeight())

self.animations = {}
self.animations.down = anim8.newAnimation(self.grid('1-4', 1), 0.2)
self.animations.left = anim8.newAnimation(self.grid('1-4', 2), 0.2)
self.animations.right = anim8.newAnimation(self.grid('1-4', 3), 0.2)
self.animations.up = anim8.newAnimation( self.grid('1-4', 4), 0.2)

self.anim = self.animations.down
end

function Player:update(dt)
cam:lookAt(Player.x , Player.y)

isMoving = false

if love.keyboard.isDown('w') then
Player.y = Player.y - Player.speed * dt
self.anim = self.animation.up
isMoving = true
end

if love.keyboard.isDown('s') then
Player.y = Player.y + Player.speed * dt
self.anim = self.animation.down
isMoving = true
end

if love.keyboard.isDown('a') then
Player.x = Player.x - Player.speed * dt
self.anim = self.animation.right
isMoving = true
end

if love.keyboard.isDown('d') then
Player.x = Player.x + Player.speed * dt
self.anim = self.animation.right
isMoving = true
end

if isMoving == false then
Player.anim:gotoFrame(2)
end

self.anim:update(dt)
end

function Player:draw()
Player.anim:draw(Player.image, player.x, player.y, nil, 10)
end

Re: LUA attempt to index global anim8 a nil value

Posted: Sat Apr 29, 2023 1:10 am
by MrFariator
Welcome to the forums, but please, for future reference, use code tags to make your code more readable.

I don't see any immediate problems with how you're using anim8 in what you've posted. Could you provide a .love or a zip of your project, so we could perhaps inspect things further? Also do note that including the whole error traceback could also be beneficial, since it includes the exact line number where the error happens, as well as the code path that led there.

Re: LUA attempt to index global anim8 a nil value

Posted: Sat Apr 29, 2023 5:06 am
by l0ki77
Problem solved. Thanks