This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

How does OOP works?

Started by H4X0RZ, 14 May 2013 - 07:39 AM
H4X0RZ #1
Posted 14 May 2013 - 09:39 AM
I heared that OOP works in lua, but how does it work?

I heared something about metatables but my brain isn't available to understand it alone :D/>
makerimages #2
Posted 14 May 2013 - 09:43 AM
have a look at http://computercraft.info/wiki/Metatable I haven`t used it myself so I can`t give any advie on that, I usually work with regular tables for my OOP-style needs
Kingdaro #3
Posted 14 May 2013 - 09:52 AM
OOP stands for Object Oriented Programming, and metatables are used to give special properties to tables. Metatables and OOP are two completely different subjects. Metatables are used in OOP, however.

The basic concept of OOP is using objects that contain the functions for your program, instead of managing all of the separate variables on your own.

CC already has an example or two of OOP and non-OOP, specifically peripheral functions.

Non-OOP:

peripheral.call('right', 'write', 'Hello World!')

OOP:

monitor = peripheral.wrap('right')
monitor.write('Hello World!')

As you can see here, we use a monitor object as a "middle-man" for calling a function rather than calling the function ourself. In this example, it does take longer, but using OOP does shorten operations in the longrun. Imagine the above examples, but with a couple hundred more writing operations. Using the monitor object would be less typing, no?