Jump to content




OOP objects in LUA

help lua

7 replies to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

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!

#2 COOLGAMETUBE

  • Members
  • 49 posts
  • LocationGermany

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 by COOLGAMETUBE, 02 February 2017 - 11:45 PM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#4 Kouksi44

  • Members
  • 89 posts
  • LocationMunich, Germany

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.

#5 TechnicalCoding

  • Members
  • 35 posts

Posted 03 February 2017 - 10:17 PM

View PostKouksi44, on 03 February 2017 - 03:08 PM, said:

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?

#6 Kouksi44

  • Members
  • 89 posts
  • LocationMunich, Germany

Posted 04 February 2017 - 02:03 AM

That's the forum post: http://www.computerc..._20#entry264250

#7 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 04 February 2017 - 02:17 AM

I think Kouksi44 meant this post...

#8 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 04 February 2017 - 06:26 AM

View PostTechnicalCoding, on 02 February 2017 - 10:51 PM, said:

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 by Hbomb_79, 04 February 2017 - 06:29 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users