*SOLVED*class problem.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

*SOLVED*class problem.

Post by TheBreadCat »

Code: Select all

function class()
    local cls = {}
    cls.__index = cls
    return setmetatable(cls, {__call = function (c, ...)
        instance = setmetatable({}, cls)
        if cls.__init then
            cls.__init(instance, ...)
        end
        return instance
    end})
end
i have this class function that makes a class decoration.

Code: Select all

player = class()


function player:__init()
	self.x = 320
	self.y = 240
	self.hspeed = 0
	self.vspeed = 0
	self.hp = 0
end

function player:update()
	self.x = self.x + self.hspeed
	self.y = self.y + self.vspeed
	self.vspeed = 0
	self.hspeed = 0
end


p = player()


function love.update(dt)
   p.update()
end
every thing should be fine, but im getting an error:
cant call field update(a nil value)
Last edited by TheBreadCat on Fri Jan 28, 2011 9:10 pm, edited 1 time in total.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: class problem.

Post by BlackBulletIV »

I'm not quite sure where the error is coming from (I'm not all that familiar with the metatable, etc. stuff). However I can see one thing that's wrong:

Code: Select all

p:update() -- instead of p.update()
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: class problem.

Post by leiradel »

Running you code with 0.7.0 gives me the following error:

Code: Select all

Error: [string "main.lua"]:24: attempt to index local 'self' (a nil value)
stack traceback:
        [string "main.lua"]:24: in function 'update'
        [string "main.lua"]:33: in function 'update'
        [string "boot.lua"]:314: in function <[string "boot.lua"]:302>
        [C]: in function 'xpcall'
BlackBulletIV's suggestion fixes it and a black screen is rendered.

Are you using 0.7.0?

Here's my main.lua with your code:

Code: Select all

function class()
    local cls = {}
    cls.__index = cls
    return setmetatable(cls, {__call = function (c, ...)
        instance = setmetatable({}, cls)
        if cls.__init then
            cls.__init(instance, ...)
        end
        return instance
    end})
end

player = class()

function player:__init()
   self.x = 320
   self.y = 240
   self.hspeed = 0
   self.vspeed = 0
   self.hp = 0
end

function player:update()
   self.x = self.x + self.hspeed
   self.y = self.y + self.vspeed
   self.vspeed = 0
   self.hspeed = 0
end

p = player()

function love.update(dt)
   p.update()
end
SirLoon
Prole
Posts: 1
Joined: Thu Jan 27, 2011 11:29 am

Re: class problem.

Post by SirLoon »

BlackBullet is right
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

leiradel wrote: Are you using 0.7.0?
yes, what are you running?
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

leiradel wrote:Running you code with 0.7.0 gives me the following error:

Code: Select all

Error: [string "main.lua"]:24: attempt to index local 'self' (a nil value)
stack traceback:
        [string "main.lua"]:24: in function 'update'
        [string "main.lua"]:33: in function 'update'
        [string "boot.lua"]:314: in function <[string "boot.lua"]:302>
        [C]: in function 'xpcall'
BlackBulletIV's suggestion fixes it and a black screen is rendered.

Are you using 0.7.0?

Here's my main.lua with your code:

Code: Select all

function class()
    local cls = {}
    cls.__index = cls
    return setmetatable(cls, {__call = function (c, ...)
        instance = setmetatable({}, cls)
        if cls.__init then
            cls.__init(instance, ...)
        end
        return instance
    end})
end

player = class()

function player:__init()
   self.x = 320
   self.y = 240
   self.hspeed = 0
   self.vspeed = 0
   self.hp = 0
end

function player:update()
   self.x = self.x + self.hspeed
   self.y = self.y + self.vspeed
   self.vspeed = 0
   self.hspeed = 0
end

p = player()

function love.update(dt)
   p.update()
end
still does not work just gets this:

Code: Select all

attempt to perform arithmetic on field 'x'(a nil value)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: class problem.

Post by vrld »

TheBreadCat wrote:

Code: Select all

attempt to perform arithmetic on field 'x'(a nil value)
Please look at the first reply again:
BlackBulletIV wrote:

Code: Select all

p:update() -- instead of p.update()
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: class problem.

Post by leiradel »

TheBreadCat wrote:
leiradel wrote: Are you using 0.7.0?
yes, what are you running?
0.7.0 on Windows, tested on both XP 32 bits and 7 64 bits. It runs fine on both, as long as you change p.update() to p:update().
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

leiradel wrote:
TheBreadCat wrote:
leiradel wrote: Are you using 0.7.0?
yes, what are you running?
0.7.0 on Windows, tested on both XP 32 bits and 7 64 bits. It runs fine on both, as long as you change p.update() to p:update().
no it dont , i changed it.
do i also have to do it with variables?
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: class problem.

Post by leiradel »

Ok, here's the code that I'm running without errors:

Code: Select all

function class()
  local cls = {}
  cls.__index = cls
  return setmetatable(cls, {__call = function (c, ...)
    instance = setmetatable({}, cls)
    if cls.__init then
      cls.__init(instance, ...)
    end
    return instance
  end})
end

player = class()

function player:__init()
  self.x = 320
  self.y = 240
  self.hspeed = 0
  self.vspeed = 0
  self.hp = 0
end

function player:update()
  self.x = self.x + self.hspeed
  self.y = self.y + self.vspeed
  self.vspeed = 0
  self.hspeed = 0
end

p = player()

function love.update(dt)
  p:update()
end
I've created a test folder in the same folder where love.exe lives, and put the above code in a main.lua file inside test. I'm then able to run it fine by either dragging test over love.exe or via the command line: love test.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests