lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 8/6/08, Jonathan <jonl86_05@yahoo.com> wrote:
> I have two objects, a and b (object b contains object a). When I try to set it inside of object b directly, I get nil values (see comments below).I have to use a temp variable in order for it to work, which doesn't make any sense. Here's a short snippet to explain what I'm trying to say:
>
>  b = {}
>
>  function b:new()
>    o = {}
>    setmetatable(o, self)
>    self.__index = self
>    return o
>  end
>
>  a = {}
>
>  function a:new ()
>    o = {}
>    setmetatable(o, self)
>    self.__index = self
>
>    -- why can't I say o.nested = b:new() ?
>    temp = b:new()
>    o.nested = temp
>    return o
>  end
>
>  test = a:new()
>  test2 = a:new()
>
>  -- if I didn't use temp, these would output as nil
>  print(test.nested)
>  print(test2.nested)
>
>
>
>
>
>

If you make o local, it'll work fine. As it is, it's global in both
cases and b:new replaces the one you're using in a:new.