Page 1 of 1

Confusion with mixins and missing variables

Posted: Tue Jun 03, 2014 12:59 am
by Splatpope
Me again, this time it wont be as easy as choosing the right file loading function :v

Basically, to handle game objects, I have decided that objects had a prototype class (like actor or item), a defined template to copy (such as wall, enemy, player, door, gun, etc) that would define it's behavior and the object's attribute, and then a proper instance created via the template's spawn() function.

The prototype classes uses mixins that contain code for movement and drawing, both dependent on a mixin defining their grid based behavior.

The problem appears when I try to have each cell of my grid draw their contents : there seems to be no errors with objects of instance item, but for objects of instance actor, it just cannot find the reference to the current map passed during it's creation and throws an error when entering the drawing mixin.

Print() debugs have shown that the only actor created in the game.lua main loop, player, knows what his map is. (print(player.map.name) outputs "testmap" as intended)

I think the problem comes from mixins, but I am not sure. Could someone more well versed help me find my error ? (Also, obviously, if I am coding useless/ugly things, that is not the problem :p)

dat love file

Keep in mind that those pitiful stubs of documentation are meaningless since I began rewriting the way I manage everything, so do not rely on them :v

Re: Confusion with mixins and missing variables

Posted: Tue Jun 03, 2014 6:57 pm
by DrCicero
after inserting the following line into Module_Mapped_Drawable:draw

Code: Select all

function Module_Mapped_Drawable:draw() 
  print("draw", self, self.map)
  [...]
end
i get the following output:

Code: Select all

draw	instance of class item	instance of class map
[...]
draw	instance of class item	instance of class map
draw	class actor	nil
This means the last call was not from an instance, but on the class itself?
If you then change the function a second time:

Code: Select all

function Module_Mapped_Drawable:draw() 
  print("draw", self, self.map)
  if tostring(self) == "class actor" then error() end
  [...]
end
you get the following stack trace:

Code: Select all

Error: nil
stack traceback:
	[C]: in function 'error'
	source/modules/drawable.lua:17: in function 'draw'
	source/cell.lua:77: in function 'drawContents'
	source/map.lua:96: in function 'draw'
	source/game.lua:41: in function 'draw'
	[string "boot.lua"]:438: in function <[string "boot.lua"]:399>
	[C]: in function 'xpcall'
source/cell.lua:77: in function 'drawContents' is as follows:

Code: Select all

  if self.contents.actor then self.contents.actor:draw() end
so the error must be in setting self.contents.actor.
a global search for 'self.contents.actor = ' found a match in source/cell:65

Code: Select all

--tells the cell which actor is inside it
function cell:setContent(content)
  if content:isInstanceOf(actor) then
    self.contents.actor = actor
  end

  if content:isInstanceOf(item) then
    table.insert(self.contents.items, content)
  end
end
Is the third line that what you indended to do? Setting self.contents.actor to the actor class?

I hope this serves as a creative guide for future debugging!

Re: Confusion with mixins and missing variables

Posted: Tue Jun 03, 2014 7:01 pm
by Splatpope
Haha yes, I was just going to post about how I had found what my error was, and it indeed came from the line you highlighted :D

I had forgot that setContent's parameter was not "actor" anymore but just "content", as I added support for the item class, so it indeed added the actual class as the actor for that cell :v

Thanks for helping, your post will actually help me debug a lot !