[SOLVED] Error indexing 'tablename' a nil value after it's declared

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
4KbShort
Prole
Posts: 15
Joined: Thu Jun 04, 2020 4:21 pm

[SOLVED] Error indexing 'tablename' a nil value after it's declared

Post by 4KbShort »

I know everyone is going to want to see code, but I don't have any so use your imagination brushes for a minute:

Trying to animate some sprites and I have this:

Code: Select all

function animateSprites(sprite,x,y,frameSpeed) --Starting on a random frame won't work
	dt = lt.getDelta()
	currentFrame = currentFrame+ frameSpeed*dt
	if currentFrame > #sprite then
		currentFrame = 1
	end
	lg.draw(spriteSheet,sprite[math.floor(currentFrame)],x,y,0,zoom)
end
Which works. One on sprite and only if there is only one sprite on the screen. I know why, of course because each sprite is "standing in line" to use this function and so it's jamming/failing. So what I need is to declare an OOP object and set its self.blahblah so each sprite instance is animating itself.

The problem there is I don't know how. I've done it in the past using copied code, but there are two things:
1: I want to LEARN how to do it
2: I want the code to be in the main file NOT a resource script or library

Now to the error:
From what I can find people state declare a variable table and write some code:

Code: Select all

 t = {}

function t:func(x, y)
  self.x = x
  self.y = y
end

t:func(1, 1)
print(t.x) -- 1
(Code example stolen from this post: https://love2d.org/forums/viewtopic.php?t=10896)

Which I guess would work or some variation of it would work, but when I use these examples I always get the error:
attempt to index global "tablename" (a nil value)

Well, yes... the table is NIL, but it's declared and this happens even if I don't call the function. Just goes off.

I know the post is rambley and I've not really asked a specific question, but please try to post constructive and helpful feedback? I'm kinda stuck and don't want to copy-pasta.

EDIT: Turns out using OOP in my project was counter to how I had my project layed out in the first place. Although I did figure out my crashing issue there is no way OOP in this manner is going to work for my current build. Leaving here for posterity(?) but marking as solved as I no longer need help with it.
Last edited by 4KbShort on Sun Aug 02, 2020 1:43 am, edited 1 time in total.
4KbShort
Prole
Posts: 15
Joined: Thu Jun 04, 2020 4:21 pm

Re: Error indexing 'tablename' a nil value after it's declared

Post by 4KbShort »

I figured out one problem: Never declare the table variable under love.load()

With that out of the way I now have some code that doesn't instantly crash. Downside is it also doesn't render anything to screen:

Code: Select all

function objects:new(sprite,x,y,frameSpeed,frame)
	local o = {sprite = sprite, x = x, y = y, fameSpeed = frameSpeed, frame = frame}
	setmetatable(o,{__index = objects})
	return o
end

function objects:update(dt)
	self.currentFrame = self.currentFrame+ self.frameSpeed*dt
	if self.currentFrame > #self.sprite then
		self.currentFrame = 1
	end
end

function objects:draw()
	lg.draw(spriteSheet,self.sprite[math.floor(self.currentFrame)],self.x,self.y,0,zoom)
end
When I put everything under objects.new it shows up on screen. Of course this doesn't work because it's show the initial state and not updating. However, when I split to the following it doesn't show or do anything. Am I missing a step? Do I need to push these to love.update/love.draw?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest