*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:
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.
no i cant post it , its to messed up and i deleted the problem. ill try to make a version that simulates the old problem.
oh and sorry but personly its a bit messed up , so i was i little angry at the moment , sorry.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

here you go:

Code: Select all

__HAS_SECS_COMPATIBLE_CLASSES__ = true

local class_mt = {}

function class_mt:__index(key)
    if rawget(self, "__baseclass") then
        return self.__baseclass[key]
    end
    return nil
end

class = setmetatable({ __baseclass = {} }, class_mt)

function class:new(...)
    local c = {}
    c.__baseclass = self
    setmetatable(c, getmetatable(self))
    if c.init then
        c:init(...)
    end
    return c
end


cCake = class:new()

function cCake:init()
	self.x = 0
	self.y = 0
	self.hspeed = 0
	self.vspeed = 0
end

function cCake:MoveUp()
	self.y = self.y-1
end

function love.load()
	cake = cCake
end

function love.update(dt)
	cake:MoveUp()
end
and the error:

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

function love.load()
	cake = cCake
end
That won't work. you have to make an instance of cCake:

Code: Select all

function love.load()
	cake = cCake:new()
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

vrld wrote:
TheBreadCat wrote:

Code: Select all

function love.load()
	cake = cCake
end
That won't work. you have to make an instance of cCake:

Code: Select all

function love.load()
	cake = cCake:new()
end
thanks but now there is one problem..
when i to to (ex.) add to varibles of the same instance , ill get an error.

Code: Select all

function cCake:Update()
	self.x = self.x + self.hspeed
	self.y = self.y + self.vspeed
	self.hspeed = 0
	self.vspeed = 0
end

Code: Select all

attempt to index local 'self'(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 index local 'self'(a nil value)
From the code you posted it's hard to tell, but I think you might be calling Update() wrong. A variation of this was pointed out to you earlier many times. I suggest that you read this: colon operator
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: class problem.

Post by TheBreadCat »

vrld wrote:
TheBreadCat wrote:

Code: Select all

attempt to index local 'self'(a nil value)
From the code you posted it's hard to tell, but I think you might be calling Update() wrong. A variation of this was pointed out to you earlier many times. I suggest that you read this: colon operator
FINALLY thanks all of you!
after 2 days i can finally start making that game!
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: *SOLVED*class problem.

Post by BlackBulletIV »

So it was the colon operator issue? Didn't I point that out before? Maybe I wasn't explicit enough.
TheBreadCat
Prole
Posts: 22
Joined: Wed Jan 26, 2011 6:05 pm

Re: *SOLVED*class problem.

Post by TheBreadCat »

BlackBulletIV wrote:So it was the colon operator issue? Didn't I point that out before? Maybe I wasn't explicit enough.
i had to do some declariations in the init functions.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest