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

OOP objects in LUA

Started by TechnicalCoding, 02 February 2017 - 09:51 PM
TechnicalCoding #1
Posted 02 February 2017 - 10:51 PM
I am currently working on a operative system in LUA which is for computercraft, and I wanted to create a clickable GUI where I will want a function to create a button, but I god some issues using function cause I'd have to specify everything myself, so I wanted to create an Advanced OOP class.

For example in PHP I'd do

<?php
class button
{
  public clickEvent;
  public posX;
  public posY;
  private removeEvent;

  public function __contruct ( x, y, text, fg, bg ) {
    /* Code here */
  }

  /* ETC */

  public function clicked(...){...}
  public function removed(...){...}
}

$btn = [];
$btn[] = new button ( 1, 1, 'Hello world', colors.blue, colors.white );
?>


But how would I do this in LUA without having tons of functions that requires to do functonname (table, btn, parameters

Thanks in advance!
COOLGAMETUBE #2
Posted 02 February 2017 - 11:44 PM
Im not sure (haven't done LUA for like a year) but i think you can do it like in JavaScript:

function button(x,y,text,fg,bg)
  obj = {
	clickEvent = nil,
	posX = nil,
	posY = nil,
	removeEvent = nil,
	-- ETC --
	clicked = function (...) end,
	removed = function (...) end
  }
  -- Construct here --
  obj.posX = x
  obj.posY = y

  -- return --
  return obj
end
Edited on 02 February 2017 - 10:45 PM
Bomb Bloke #3
Posted 02 February 2017 - 11:54 PM
COOLGAMETUBE's example is more or less how the window API creates "window" objects. It's also possible to do much the same thing without defining the same functions over and over by creating funcs intended to be called using colon notation; and if you don't even want to bother sticking those pointers into your new object tables manually, metatables can handle that for you. The latter methods are how the vector API operates.
Kouksi44 #4
Posted 03 February 2017 - 03:08 PM
The best way of implementing classes and objects is probably through Lua's metatable feature. This especially comes in handy if you want your classes to inherit from other classes.

*shameless self promotion*

I wrote a library that should have everything you showed in your example, defining classes, making members public/private etc.
You might want to check it out if you are interested in how it works.
TechnicalCoding #5
Posted 03 February 2017 - 10:17 PM
The best way of implementing classes and objects is probably through Lua's metatable feature. This especially comes in handy if you want your classes to inherit from other classes.

*shameless self promotion*

I wrote a library that should have everything you showed in your example, defining classes, making members public/private etc.
You might want to check it out if you are interested in how it works.

Interesting, where can I have a look?
Kouksi44 #6
Posted 04 February 2017 - 02:03 AM
That's the forum post: http://www.computercraft.info/forums2/index.php?/topic/26305-classic-pretty-oop-in-cc/page__pid__264250__st__20#entry264250
Dog #7
Posted 04 February 2017 - 02:17 AM
I think Kouksi44 meant this post
hbomb79 #8
Posted 04 February 2017 - 06:26 AM
But how would I do this in LUA without having tons of functions that requires to do functonname (table, btn, parameters

You could use Titanium, which provides a custom class system allowing you to create custom nodes (nodes are buttons, and text boxes, etc…) if the default ones (listed in the forum post) don't cut it for you (source).
Edited on 04 February 2017 - 05:29 AM