lua-users home
lua-l archive

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


You should use:

myObj = {}
myObj.xPos = 10;
myObj.someVal = 100;

function myObj:update()
    --some code
    self.someVal = self.xPos * 10
    --remaining code
end

Pay attention to the ":" in the function declaration. It means there is an
implicit parameter "self" pointing to the object (table).



--Rafael


----- Original Message ----- 
From: "Ryan Leigh" <ryan@robotfood.net>
To: <lua@bazar2.conectiva.com.br>
Sent: Monday, April 12, 2004 11:00 PM
Subject: A question of scoping (I think)


> Hello everyone,
>
> So I have a question. I just bet it has been answered somewhere, but I
> don't have a succinct way to phrase it for a search. I'm creating a
> scripting engine for a game and in this script I define an object
> (which is of course really just a table) as well as some member values
> a la:
>
> myObj = {}
> myObj.xPos = 10;
> myObj.someVal = 100;
>
> Now, I would like it if I could write a function:
>
> function myObj.update()
>    --some code
>    someVal = xPos * 10
>    --remaining code
> end
>
> The way I see it right now is I have to write the function like this:
>
> function myObj.update()
>    --some code
>    myObj.someVal = myObj.xPos * 10
>    --remaining code
> end
>
> Which is a pain and error prone (because I'm a lousy typist). So, to
> finally get to the point, is there something I can do where when I
> call a function that is in some object and I reference a variable, is
> there someway I can force it to look in the object's table first
> before going global? I guess this is similar to having a C++ class and
> I want to scope to member variables in a member function. Or am I just
> not envisioning all the possibilities of Lua and there exists a much
> better way of doing things.
>
> Whew, I hope that made sense.
>
> Thanks for any help,
> Ryan Leigh
>
>
>