anim8 nil value error in multi-scene project

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
Sashz
Prole
Posts: 8
Joined: Sun Sep 24, 2023 3:13 pm

anim8 nil value error in multi-scene project

Post by Sashz »

Hello, everyone.

I'm starting a new game divided into several files, but I don't know exactly how to reference the anim8 library in it.

When running the project below, the message "Error: player.lua:44: attempt to index field 'anim' (a nil value)" is displayed.

I know it's something simple, but I'm a bit lost... I appreciate any help.

main.lua

Code: Select all

require 'intro'
require 'menu'
require 'level1'

function love.load()
	scene = intro

	if scene == intro then
		intro:load()
	elseif scene == menu then
		menu:load()
	elseif scene == level1 then
		level1:load()
	end
end

function love.update(dt)
	if scene == intro then
		intro:update(dt)
	elseif scene == menu then
		menu:update(dt)
	elseif scene == level1 then
		level1:update(dt)
	end
end

function love.draw()
	if scene == intro then
		intro:draw()
	elseif scene == menu then
		menu:draw()
	elseif scene == level then
		level:draw()
	end
end
intro.lua

Code: Select all

intro = {}

function intro:load()
	-- body
end

function intro:update(dt)
	if love.keyboard.isDown('return') then
		scene = menu
	end
end

function intro:draw()
	love.graphics.print("Intro. Press ENTER to Menu", 100, 100)
end
menu.lua

Code: Select all

menu = {}

function menu:load()
	-- body
end

function menu:update(dt)
	if love.keyboard.isDown('x') then
		scene = level1
	end
end

function menu:draw()
	love.graphics.print("Menu. Press X to Start", 100, 100)
end
player.lua

Code: Select all

player = {}

anim8 = require 'anim8'

function player:load()
	player.img = love.graphics.newImage('sprites/player-frame-1.png')
	player.spriteSheet = love.graphics.newImage('sprites/player.png')
	player.grid = anim8.newGrid(44, 64, player.spriteSheet:getWidth(), player.spriteSheet:getHeight())
	player.animations = {}
	player.animations.right = anim8.newAnimation(player.grid('1-4', 1), 0.2)
	player.animations.left = anim8.newAnimation(player.grid('1-4', 2), 0.2)
	player.anim = player.animations.right
	player.width = love.graphics.getWidth()
	player.height = love.graphics.getHeight()
	player.x = 200
	player.y = 200
	player.speed = 300
end

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

function player:move(dt)
	local isMoving = false

	if love.keyboard.isDown('right') then
		if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
			player.x = player.x + player.speed * dt
			player.anim = player.animations.right
			isMoving = true
		end
	end

	if love.keyboard.isDown('left') then
		if player.x > 0 then
			player.x = player.x - player.speed * dt
			player.anim = player.animations.left
			isMoving = true
		end
	end

	if isMoving == false then
		player.anim:gotoFrame(1)
	end

	player.anim:update(dt)
end

function player:draw()
	player.anim:draw(player.spriteSheet, player.x, player.y)
end
level1.lua

Code: Select all

level1 = {}

require 'player'

function level1:load()
	player:load()
end

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

function level1:draw()
	love.graphics.print("Game is running!", 100, 100)

	player:draw()
end
When I create only main.lua and player.lua, everything goes fine. But when I split it into scenes, the error occurs.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: anim8 nil value error in multi-scene project

Post by pgimeno »

Yeah, what's happening here is that love.load() only executes once at the start of the whole project, therefore if you make the other files' initializations depend on the current scene, they won't execute.

Simply call all xxxx:load() unconditionally within love.load().

Alternatively, if you want to lazily call xxxx:load() only when needed, you can make a function to switch scenes, and make it call the new scene's load() method (and only switch scenes using that function, not by setting a variable).
User avatar
Sashz
Prole
Posts: 8
Joined: Sun Sep 24, 2023 3:13 pm

Re: anim8 nil value error in multi-scene project

Post by Sashz »

Thank you. Your explanation helped me a lot. Later I will try to create a function to change scenes as you said.

I changed the beginning of the main.lua file and now it no longer gives an error, but when it reaches level1, the screen just goes black.

main.lua

Code: Select all

require 'intro'
require 'menu'
require 'level1'
require 'player'

function love.load()
	intro:load()
	menu:load()
	level1:load()
	player:load()

	scene = intro

	if scene == intro then
		intro:load()
	elseif scene == menu then
		menu:load()
	elseif scene == level1 then
		level1:load()
	end
end

etc... etc...
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: anim8 nil value error in multi-scene project

Post by pgimeno »

Sashz wrote: Fri Oct 18, 2024 4:02 pm Thank you. Your explanation helped me a lot. Later I will try to create a function to change scenes as you said.

I changed the beginning of the main.lua file and now it no longer gives an error, but when it reaches level1, the screen just goes black.

main.lua

Code: Select all

require 'intro'
require 'menu'
require 'level1'
require 'player'

function love.load()
	intro:load()
	menu:load()
	level1:load()
	player:load()

	scene = intro

	if scene == intro then
		intro:load()
	elseif scene == menu then
		menu:load()
	elseif scene == level1 then
		level1:load()
	end
end

etc... etc...
Do you realize that you're calling intro:load() and player:load() twice?

- First you call intro:load()
...
- Then you call level1:load() which in turn calls player:load().
- Then you call player:load(), so it's executed twice.
- Then you set scene to intro and do: if scene == intro then intro:load() ..., therefore intro:load() will also execute again.

Anyway, probably it doesn't draw anything because the variable `level` is not assigned, yet your love.draw checks for it.

Edit: Rather than use all the if scene == ... elseif scene == ..., why not just call scene:update(dt) and scene:draw()?
User avatar
dusoft
Party member
Posts: 654
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: anim8 nil value error in multi-scene project

Post by dusoft »

Sashz wrote: Fri Oct 18, 2024 4:02 pm Thank you. Your explanation helped me a lot. Later I will try to create a function to change scenes as you said.
Or you can use hump library: https://hump.readthedocs.io/en/latest/g ... -functions or any other existing scene/state managers.
User avatar
Sashz
Prole
Posts: 8
Joined: Sun Sep 24, 2023 3:13 pm

Re: anim8 nil value error in multi-scene project

Post by Sashz »

Thanks guys.
I'll work on it and post the results here.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 15 guests