## SOLVED [ Will reuse thread ]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Chimericect
Prole
Posts: 4
Joined: Sat Jul 24, 2021 5:54 pm

### SOLVED [ Will reuse thread ]

Hey there! I'm currently working on a project to graduate. It does not have to be perfect, hell it doesn't even need to be finished. I'm brand new to Lua, and honestly took on a task a little too big for me. But, this is moreso about learning than actually having something that works. But.. I want something that works. I've been following harvards CS50 course, which is where a lot of the code comes from. I started off by taking bits and basically just working through and trying to understand how it works, with the intention of expanding upon it and, eventually, making it my own code as I learn. But, I'm incredibly stuck.

About the problem|| What I'm trying to do right now is just get player to spawn in on the randomized grass-tile map. The grass tile map is fine, it appears as it should. The problem comes when I try to render the player. More will be added later, but this is my goal for now, so the generateAnimals function (that has the same problem) can be ignored. I've tried for about a week+ trying to understand what I did wrong, or what the code thinks is happening. I would love an answer, but also an explanation. I do really want to understand lua!

Right now, this is what the error looks like [ apologies for the size ]

Github: https://github.com/Chimericect/KOK_Game
Last edited by Chimericect on Sat Jul 24, 2021 11:07 pm, edited 1 time in total.
MrFariator
Party member
Posts: 282
Joined: Wed Oct 05, 2016 11:53 am

### Re: Unable to index (a nil value) [ Help needed ]

As per the error states, on line 28 in BaseState.lua, you're trying to index a local variable called 'anim', but it's nil. Indexing a nil value suggests you're trying to treat it like a table, when its value is in fact nil (which basically means an absence of a value). Perhaps you tried to assign a value to the variable 'anim', maybe from another table, but got a nil instead.

Without greater context or seeing the actual code, that's all I can say. If you want further help, post your code.
Chimericect
Prole
Posts: 4
Joined: Sat Jul 24, 2021 5:54 pm

### Re: Unable to index (a nil value) [ Help needed ]

MrFariator wrote: Sat Jul 24, 2021 8:17 pm As per the error states, on line 28 in BaseState.lua, you're trying to index a local variable called 'anim', but it's nil. Indexing a nil value suggests you're trying to treat it like a table, when its value is in fact nil (which basically means an absence of a value). Perhaps you tried to assign a value to the variable 'anim', maybe from another table, but got a nil instead.

Without greater context or seeing the actual code, that's all I can say. If you want further help, post your code.
I can post screenshots if necessary! But I did link the github at the bottom of the post in case that helped so people can look at the full source.
https://github.com/Chimericect/KOK_Game

If you can't see it let me know, I haven't uploaded to github *before this but I think the permissions should allow you to see!
MrFariator
Party member
Posts: 282
Joined: Wed Oct 05, 2016 11:53 am

### Re: Unable to index (a nil value) [ Help needed ]

I missed the github link, my apologies. Generally people post the relevant code directly onto the forums.

In any case though, the issue is simple:

Code: Select all

function BaseState:render()
local anim = self.entity.currentAnimation
love.graphics.draw(gameTextures[anim.texture], gameFrame[anim.texture][anim:getCurrentFrame()],
math.floor(self.entity.x), math.floor(self.entity.y))
end
Apparently your player entity has no defined member "currentAnimation", so the code will error if anim equals to nil. To catch runtime errors like this, you could add some sort of guard:

Code: Select all

function BaseState:render()
if not self.entity or not self.entity.currentAnimation return end -- return and do nothing if the object has no 'entity' or 'currentAnimation' defined
local anim = self.entity.currentAnimation
love.graphics.draw(gameTextures[anim.texture], gameFrame[anim.texture][anim:getCurrentFrame()],
math.floor(self.entity.x), math.floor(self.entity.y))
end
That should fix the crash, but understandably your player will not be rendered if currentAnimation evaluates to nil. I didn't look at your player class' code, but that's probably your next place to check.
Chimericect
Prole
Posts: 4
Joined: Sat Jul 24, 2021 5:54 pm

### Re: Unable to index (a nil value) [ Help needed ]

MrFariator wrote: Sat Jul 24, 2021 8:49 pm I missed the github link, my apologies. Generally people post the relevant code directly onto the forums.

In any case though, the issue is simple:

Code: Select all

function BaseState:render()
local anim = self.entity.currentAnimation
love.graphics.draw(gameTextures[anim.texture], gameFrame[anim.texture][anim:getCurrentFrame()],
math.floor(self.entity.x), math.floor(self.entity.y))
end
Apparently your player entity has no defined member "currentAnimation", so the code will error if anim equals to nil. To catch runtime errors like this, you could add some sort of guard:

Code: Select all

function BaseState:render()
if not self.entity or not self.entity.currentAnimation return end -- return and do nothing if the object has no 'entity' or 'currentAnimation' defined
local anim = self.entity.currentAnimation
love.graphics.draw(gameTextures[anim.texture], gameFrame[anim.texture][anim:getCurrentFrame()],
math.floor(self.entity.x), math.floor(self.entity.y))
end
That should fix the crash, but understandably your player will not be rendered if currentAnimation evaluates to nil. I didn't look at your player class' code, but that's probably your next place to check.
No worries at all, thank you very much! I had noticed that a lot of people end up just posting the github link, and it was my bad for not making it very noticeable. I will look through that now and see if I can resolve it, thank you!!
pgimeno
Party member
Posts: 2819
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Unable to index (a nil value) [ Help needed ]

Welcome to the forums.

There is a confusion between animation states and game states. I suggest you clear up that confusion and use different base classes for each. There's also a lot of globals, which makes debugging much harder. For example, in closeGame.lua the class BaseState is overwritten. In general, it's hard to tell what is a class and what is an instance.

Supposedly, BaseState:init() should be called with an entity as argument, but I placed a print() in it and it was never called before the crash. That suggests that you haven't properly set the entity for the player's stateMachine. It is a table, but that table is not the player. I've tried to follow where it's set, but I haven't found it.
Chimericect
Prole
Posts: 4
Joined: Sat Jul 24, 2021 5:54 pm

### Re: Unable to index (a nil value) [ Help needed ]

Ohhh I think I understand what's going on a bit now. Apologies for the mess, I've been trying hard to fix things and I think I made it worse in the process. I'll post in this thread again when I have gone through, I think you guys really helped the fuzzy brain moments that I had! Thank you a lot for replying to me, and replying quickly! The closeGame.lua was definitely an error, and I did catch another error in the Entity class that I had missed prior. While it didn't fix it, i think I have an idea on how to make it function now!

Update: While the player doesn't render, the error is gone!

Update 2: PLayer does render and I am dumb. Doesn't walk but this has been driving me nuts for awhile now thank you guys so much. I'll probably be back with more questions!

### Who is online

Users browsing this forum: No registered users and 63 guests