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!
51 posts
Location
Germany
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
7083 posts
Location
Tasmania (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.
89 posts
Location
Munich, 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.
35 posts
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?
89 posts
Location
Munich, Germany
Posted 04 February 2017 - 02:03 AM
1220 posts
Location
Earth orbit
Posted 04 February 2017 - 02:17 AM
I think Kouksi44 meant
this post…
353 posts
Location
Orewa, New Zealand
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