views:

50

answers:

1

I'm having a huge nightmare with these subclasses and I can't figure out why this isn't working.

I have main class BODY with a subclass RECTANGLE. BODY has a function called SetWorld which does the following

function BODY:SetWorld( worldnum )

    self.world  = worldnum

end

Now, if I do this:

rect = RECTANGLE:new()
rect:SetWorld(1)

The value self.world is changed to 1 in the SetWorld function but if I try to use self.world in another function in BODY it always returns nil. Why is this? It works if I create a new BODY instead of a RECTANGLE, but that means I can't use my subclass.

Thanks in advanced.

A: 

When using a colon to call a method, the first argument is set the "self", and all other arguments are shifted. If you had supplied more code it would be easier to resolve the issue, however, I believe this may be the issue:

    local x = {
    new = function(o, t)
        print("1st Arg", o)
        print("2nd Arg", t)
    end
}

x.new("Hello World")
x:new("Hello World")

Do you see how the arguments are shifted?

Camoy