Page 1 of 1

[SOLVED]Errors attempting to do acess tables

Posted: Sat May 17, 2014 2:42 pm
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.

Re: Errors attempting to do acess tables

Posted: Sat May 17, 2014 3:13 pm
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().

Re: Errors attempting to do acess tables

Posted: Sat May 17, 2014 3:14 pm
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?

Re: Errors attempting to do acess tables

Posted: Sat May 17, 2014 3:21 pm
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.

Re: Errors attempting to do acess tables

Posted: Sat May 17, 2014 3:38 pm
by ishan
Thanks! The problem is now solved.
Here is the final .love where you can enjoy watchng him walk around the screen