[SOLVED]Errors attempting to do acess tables

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
ishan
Prole
Posts: 4
Joined: Sat May 17, 2014 2:36 pm

[SOLVED]Errors attempting to do acess tables

Post by ishan »

Hello people!
I have been writing a very basic roguelike game for one of my friends
When i decided to abstract the characters to an actor class, all sorts of weird things began to happen
1) It refused to accept a function call to sprites created in an actor table
2) It refused to perform arithmetic on delta time passed into the actor class, saying it was a table value.
The sprite error causing lines have been commented out of actors.lua

I would like it if you would offer me a solution to both the problems.
Thank you in advance.
Attachments
jk.love
(9.35 KiB) Downloaded 106 times
Last edited by ishan on Sat May 17, 2014 3:45 pm, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Errors attempting to do acess tables

Post by Robin »

One of the main problems is that you're assigning to globals

That means, that when you call jk:load(), after calling newSprite, t no longer refers to the same object as jk, so things get really weird.

If you replace t = {} by local t = {} in both actor.lua and sprite.lua, it'll get you a long way. You'll still have some other issues to deal with, though. Mainly about things like scope, and the difference between a.b() and a:b().
Help us help you: attach a .love.
ishan
Prole
Posts: 4
Joined: Sat May 17, 2014 2:36 pm

Re: Errors attempting to do acess tables

Post by ishan »

Thanks. What's the difference between a.b() and a:b() may i ask?
EDIT: After applying your fix, i get this:

Code: Select all

Error: anim8.lua:229: attempt to perform arithmetic on local 'dt' (a table value)
stack traceback:
	anim8.lua:229: in function 'update'
	sprites.lua:33: in function 'update'
	actors.lua:15: in function 'update'
	main.lua:15: in function 'update'
	[string "boot.lua"]:434: in function <[string "boot.lua"]:399>
	[C]: in function 'xpcall'
What's gone wrong now?
Attachments
jk-new.love
(9.36 KiB) Downloaded 100 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Errors attempting to do acess tables

Post by Robin »

a:b() is syntactic sugar.

For function definitions, something like:

Code: Select all

function screen:load()
  --
end
is the same as

Code: Select all

function screen.load(self)
  --
end
and for function calls, world:destroy() is the same as world.destroy(world) (except world is only evaluated once).

So if you do

Code: Select all

	jk = newActor("Justforkix.png", 5, 5)
	jk:load()
my expectation would be that the definition would be something like this:

Code: Select all

	function t:load()
		self.sprite = {}
		self.sprite = newSprite(spriteFile)
		self.x = x
		self.y = y
		self.direction = "Down"
		self.speed = 150
	end
That way it wouldn't have mattered the global variable t was overwritten.
Help us help you: attach a .love.
ishan
Prole
Posts: 4
Joined: Sat May 17, 2014 2:36 pm

Re: Errors attempting to do acess tables

Post by ishan »

Thanks! The problem is now solved.
Here is the final .love where you can enjoy watchng him walk around the screen
Attachments
jk-final.love
(9.36 KiB) Downloaded 114 times
Post Reply

Who is online

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