Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Tagged

What is a class for? And what is the "self" Thing?
  • Sorry for asking, but im New in programming... I dont know anything about this Things! Please help!

  • http://www.lua.org/pil/16.html

    What a class is for is a philiosopical question. The link above may help with that.

    In general it's a way to partition code. You can do that without classes but using classes is widespread (and advisable).

    As far as self, it means that the variable belongs to the class. Each time a new instance of that class is created a new variable is created that belongs to that instance. Without classes you'd be having to work with global tables.

  • SimeonSimeon
    Posts: 3,733

    A class is a bit like a blueprint for an object, you define the properties of the object and then you can create "instances" (copies) of it and use them in your code.

    For example you might have a class for "Enemy" characters in your game. The class says that enemies look a certain way and behave a certain way.

    When you are defining a class in your code, self refers to properties stored in the class. For example, you might use self.position to refer to your object's location on the screen.

  • But in Lua, class is just a table? A table key's value corresponds to an object in OOP speak? A value of a key in the table can be a function, which corresponds to a method in OOP speak?

  • SimeonSimeon
    Posts: 3,733

    Tachnically, yes. The : operator and self keyword masks a lot of this from you.

  • Perhaps I'm not alone that the masking makes it more confusing. A function assigned to a variable makes sense once you get the table thing.

  • SimeonSimeon
    Posts: 3,733

    You can use either system.