Page 1 of 3

*SOLVED*class problem.

Posted: Wed Jan 26, 2011 9:05 pm
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)

Re: class problem.

Posted: Thu Jan 27, 2011 12:43 am
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()

Re: class problem.

Posted: Thu Jan 27, 2011 1:01 am
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

Re: class problem.

Posted: Thu Jan 27, 2011 11:50 am
by SirLoon
BlackBullet is right

Re: class problem.

Posted: Thu Jan 27, 2011 12:24 pm
by TheBreadCat
leiradel wrote: Are you using 0.7.0?
yes, what are you running?

Re: class problem.

Posted: Thu Jan 27, 2011 2:40 pm
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)

Re: class problem.

Posted: Thu Jan 27, 2011 3:20 pm
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()

Re: class problem.

Posted: Thu Jan 27, 2011 3:42 pm
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().

Re: class problem.

Posted: Thu Jan 27, 2011 3:59 pm
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?

Re: class problem.

Posted: Thu Jan 27, 2011 4:13 pm
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.