Using middleclass

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.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Using middleclass

Post by Ertain »

Hello again, Lovers.

I have been trying to use middleclass, but when I do the variable "self" isn't found when I use it. When I run the program LÖVE keeps giving me an error about indexing a local variable, even though I clearly required "middleclass". I even just requiring "middleclass.lua" and that didn't work.

I took some of the code from the game Kurosuke and messed around with that. But that stuff is not even running:

Code: Select all

require "middleclass"
require "Stateful.lua"

Game = class('Game', StatefulObject)

--[[ Taken from the game Kurosuke (or at least based upon code taken
from the game Kurosuke). ]]
function Game:initialize()
  super.initialize(self)
  self.menu = {}
  self.index = 1
  self.topMenu = "top"
  self.showMenus()
  self.drawMenus()
end

function Game:showMenus()
  -- Wipe her clean.
  for i=1, #self.menu do
    table.remove(self.menu, 1)
  end

  if self.topMenu == "top" then
    table.insert(self.menu, "Start Game")
    table.insert(self.menu, "Quit")
  end
end

-- Would this even work?
-- Kindly lifted from Kurosuke.
function Game:keypressed(key, unicode)
  if key == "up" then
    if self.index > 1 then
      self.index = self.index - 1
      if self.menu[self.index] == " " then -- Does this check to see if the table is empty?
	self.index = self.index - 1
      end
    end
  elseif key == "down" then
    if self.index < #self.menu then
      self.index = self.index + 1
      if self.menu[self.index] == " " then
	self.index = self.index + 1
      end
    end
  elseif key == "return" then
    Game:makeSenseOfItAll()
  end
end

-- Lovingly lifted from the game Kurosuke.
function Game:drawMenus()
  -- Set some colors and banners here.

  for i=1,#self.menu do
    if i == self.index then
    -- Set color for highlighting.
      love.graphics.setColor(255,240,25,100)
    else
    -- Set normal color.
      love.graphics.setColor(11,47,255,100)
    end
    love.graphics.print(self.menu[i], love.graphics.getWidth() / 2, (love.graphics.getHeight() / 2) + 32 * i)
  end
  -- Possibly draw the cursor here.
end

-- What happens when the player presses return on the menu
-- select screen.
function Game:makeSenseOfItAll()
  local menuSelection = self.menu[self.index]
  if menuSelection == "top" then
    Game:gotoState("Main game") -- This is for starting the actual game.
  elseif menuSelection == "Quit" then
    love.event.push('q')
  end
end
Booted, suited, and ready to get executed.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Using middleclass

Post by kikito »

Hi there!
when I do the variable "self" isn't found when I use it.
Where, exactly, are you trying to use self unsuccessfully?
But that stuff is not even running
The patch of code you posted doesn't seem very complete. What is your complete setup? I assume that you have a main.lua file with a love.run function defined somewhere?

Incidentally, StatefulObject was used on mindstate, but that lib got transformed into the Stateful lib some time ago. StatefulObject (a class) stopped existing at that point, and was replaced by just Stateful (a mixin). The difference between both is that instead of this:

Code: Select all

Game = class('Game', StatefulObject)
You now do this:

Code: Select all

Game = class('Game'):include(Stateful)
(I still haven't found the time to update the wiki on that regard. Sorry.)
When I write def I mean function.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Using middleclass

Post by Mud »

Ertain wrote:I have been trying to use middleclass, but when I do the variable "self" isn't found when I use it.
That usually means you accidentally called a method like this 'myobject.method()' rather than this 'myobject:method()'
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Using middleclass

Post by Ertain »

I'm just trying to get the function "Game:initialize(self)" to work. Disregarding "Stateful", is it "super:initialize(self)" or "self:menu ={}"?
Booted, suited, and ready to get executed.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Using middleclass

Post by Mud »

Ertain wrote:I'm just trying to get the function "Game:initialize(self)" to work.
There is no such function. Why don't you just post the code? That'll save us a lot of detective work.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Using middleclass

Post by Ertain »

I was just trying to write a simple menu (or try out a simple menu). So simple is this, I just wrote the barebones:

Code: Select all

require "menus.lua" -- This code has already been posted.

function love.load()

end

function love.update(dt)
  game = Game:new()
end

function love.draw()
  game:drawMenus()
end

Booted, suited, and ready to get executed.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using middleclass

Post by Robin »

Ertain wrote:"self:menu ={}"
That is no valid Lua. a:b() is syntactic sugar for a.b(a), and function a:b() ... end is syntactic sugar for function a.b(self) ... end. So : is only used in function calls and function definitions.
Help us help you: attach a .love.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Using middleclass

Post by Ertain »

So I take it the "self" part is not really necessary? :?
Booted, suited, and ready to get executed.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Using middleclass

Post by tentus »

Think of it this way. "self.whatever = 1" in Game:initialize() means that you made a variable that is inside of Game. "whatever = 1" in Game:initialize() means that you made a variable that is outside of Game. self.whatever equals Game.whatever. The advantage of self.whatever is that you don't have to worry about overlap: every individual object gets it's own version of that variable, whereas without the self there would be only one variable that everyone has to share.

This makes me realize that I need to do some serious cleaning to Kurosuke, it probably makes no sense everyone but me. :(
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Using middleclass

Post by kikito »

Ertain wrote:I'm just trying to get the function "Game:initialize(self)" to work. Disregarding "Stateful", is it "super:initialize(self)" or "self:menu ={}"?
It's "Game:initialize()" or "Game.initialize(self)", but never "Game:initialize(self)" (see below)

It's "super.initialize(self)" (note the '.' instead of ':' here), because super is a bit special.
Ertain wrote:So I take it the "self" part is not really necessary? :?
':' is just a shortcut. It can be used in function declarations and invokations.

When declaring functions, it creates a magical parameter called 'self':

Code: Select all

-- the following function declarations are equivalent:
function Game.initialize(self) -- when using a dot (.), there's no magic parameter
  ... -- use self here
end
function Game:initialize() -- when using ':', there's a magic parameter called 'self'
  ... -- use self here
end
When invoking functions, : adds a magical parameter pointing "the thing just before it":

Code: Select all

game.initialize(game) -- this invokes exactly the same as the next line
game:initialize() -- there's a magic parameter meaning "the thing just before the :"
The two shortcuts are convenient for object-oriented stuff.

The ':' shortcut is one of the parts I don't like about Lua. It's confusing and forces the programmer to think about irrelevant stuff. I'd rather always have an implicit parameter and have Lua optimize the non-needed cases, like it happens on Javascript.

I hope this helps!
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], DTmg and 8 guests