*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

Re: class problem.

Post by TheBreadCat »

leiradel wrote: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.
but when i call this function:

Code: Select all

function cCake:MoveDown()
	self.y = self.y+1
end
by doing this:

Code: Select all

oCake:MoveDown()
i get an error.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: class problem.

Post by bartbes »

Because oCake.y doesn't exist?
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

bartbes wrote:Because oCake.y doesn't exist?

Code: Select all

function cCake:init()
	self.x = 0
	self.y = 0
	self.hspeed = 0
	self.vspeed = 0
end
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

it does it must be something else.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: class problem.

Post by kikito »

TheBreadCat wrote:
bartbes wrote:Because oCake.y doesn't exist?

Code: Select all

function cCake:init()
	self.x = 0
	self.y = 0
	self.hspeed = 0
	self.vspeed = 0
end
Wasn't your constructor method called __init (with underscores)?

Also, for future posts: If you say "I get an error" it's very difficult to know what happens. It's better saying "I get the following error:" and paste the error message after it on a [ code ] [ /code ] section.
When I write def I mean function.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

TheBreadCat wrote: still does not work just gets this:

Code: Select all

attempt to perform arithmetic on field 'x'(a nil value)
done before.
just forgot to repost it.
and the __init is changed to init beouse im using http://love2d.org/wiki/Simple_Educative_Class_System.
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: class problem.

Post by leiradel »

Can you post a .love file we can use to reproduce the problem?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: class problem.

Post by Robin »

Also, you're creating a global called "instance". This does not seem to have been fixed by anyone. Not the most important of your issues, but still.

init() is not called on your class system, but __init() is, which is why it doesn't work. Change either one in the other, and it should work.
Help us help you: attach a .love.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

Robin wrote:Also, you're creating a global called "instance". This does not seem to have been fixed by anyone. Not the most important of your issues, but still.

init() is not called on your class system, but __init() is, which is why it doesn't work. Change either one in the other, and it should work.
are you even listening?
i changed libary os now its init()

Code: Select all

function class:new(...)
    local c = {}
    c.__baseclass = self
    setmetatable(c, getmetatable(self))
    if c.init then <--------
        c:init(...) <---------
    end
    return c
end
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:are you even listening?
What about you, are you listening? I've asked if you could post a .love file that reproduces the problem and you didn't even answer.

You won't get much help with this kind of attitude.
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests