Jump to content


Kouksi44's Content

There have been 23 items by Kouksi44 (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#266254 Classic - pretty OOP in CC

Posted by Kouksi44 on 12 April 2017 - 01:37 PM in APIs and Utilities

Another update:
This one adds several new features that should be quite useful.
First of all method and field resolving should work a bit better now. Also most error messages will now print the correct class in which the method or field lookup failed.

Anonymous classes:
You can now easily define an anonymous class that extends some existing class pretty much the same way as you would do in Java :
class "SomeClass" {
  bar = public - function() end;
}

local anonymous = SomeClass() {
  bar = public - function()
	-- this overrides SomeClass.bar
  end;
}
Note that these anonymous classes are treated as objects of the class they are extending. Any new methods that are added in the anonymous class that do not exist in the superclass won't be visible

Generic classes :
You can now specify a type parameter in the head of the class definition like so:
class "SomeClass" : T {
  someValue = public - T - nil;
}
-- also works with inheritance

class "SomeClass" extends "SomeOtherClass" : T {
  ...
}

local obj = SomeClass[String](...)

Properties:
The property keyword should be pretty straightforward. Instead of directly setting and getting some value, the respective getter and setter methods are called when a property is accessed.
class "SomeClass" {
  x = public - property - String - "Hello World";
  getX = function(self) ... end;
  setX = function(self,value) end;
}

Some other changes:
- Attributes can be given certain types again, just specify the type like any other keyword: String, Table, boolean, thread, number.
- The type of an attribute can also be an existing class. Any object that is assigned to such an attribute is treated as an object of that class. That means, that the object must be castable to the given type.
- Calling getClasss() on an object will now return an instance of the built in Class class. That class offers some helper functions:
Spoiler

The installer is still the same:
pastebin run fq9fwb2S



#265503 Classic - pretty OOP in CC

Posted by Kouksi44 on 11 March 2017 - 05:28 PM in APIs and Utilities

New update:

Because of whatever reasons, I decided to rewrite this library once again... Classes and objects do no longer use any metatable magic to make inheritance possible. So theoratically the new version should perform a tiny bit better, even though it still won't be the fastest.

Apart from what changed under the hood, most things are still realtively the same, except for a few small changes and new features:

Creating and extending classes now works again how it used to before the last update:
 class "mypackage.blah.Test" extends "myotherpackage.bluh.ImportantClass" { ... } 

That way it is much easier to inherit classes that are not in the default namespace.

Also, method overriding does actually work how it should work now:
class "A" {

y = public - function()  ...  end;

z = public - function(self)  self.y()  end

}

class "B" extends "A" {

test = function(self)  self.z() end

y = public - function()  print("This is called by A.z") end
}

A method can only be overriden, if it is public and neither static nor final.
I also added the baseclass "CObject" that is the baseclass of all classes that don't already inherit from some class. CObject offers a view useful methods:
Spoiler

The last change is how you import classes from another file. That part is much easier now:
 using "mypackage.blah.TestClass" : from "TestClass.lua" 

If the class you want to import is already loaded, the function will simply put it in the default namespace, so that the package name can be omitted. In that case it's not necessary to call the :from() function.

Pastebin installer:
 pastebin run fq9fwb2S 



#264399 OOP objects in LUA

Posted by Kouksi44 on 04 February 2017 - 02:03 AM in Ask a Pro

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



#264357 OOP objects in LUA

Posted by Kouksi44 on 03 February 2017 - 03:08 PM in Ask a Pro

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.



#264250 Classic - pretty OOP in CC

Posted by Kouksi44 on 31 January 2017 - 08:27 PM in APIs and Utilities

What exaclty makes oop such a terrible idea for you ? Lua does contain lambda calculus if that is what you mean...



#264229 Classic - pretty OOP in CC

Posted by Kouksi44 on 30 January 2017 - 10:30 PM in APIs and Utilities

New update:

I rewrote this library from the ground up, so this update comes with quite a lot of changes.

Classic 2.0

First of all defining classes that extend other classes changed a little bit:

class "Shape" {
  color = private - colours.red;
}

class "Rectangle" : Shape {
  width = private - 0;
  height = private - 0;
}

Instead of using the extends keyword you just use a colon now.

Also, instead of using
function SomeClass:init() end
You now use the name of the class as the constructor name:
function SomeClass:SomeClass() end

Accessing values of a class is now possible by simply indexing it:

class "Test" {
  x = static - 234;
}

print(Test.x) -- prints 234

Another new feature is, that you can now define your class to to be at a specific classpath:
class "myProject.subFolder.TestClass" {
  ...
}

If you want to create an instance of such a class you either have to import that namespace or specific class:
using "myProject.subFolder.TestClass"
or
using "myProject.subFolder.*" -- imports all classes in this namespace

Or you use the full path to the class:
local obj = myProject.subFolder.TestClass()

Note: You first have to load a class from whatever file it is located in and after that make it actually usable by calling using.

You also cannot extend from classes that are not in the default global namespace and are not imported yet:
class "SomeClass" : myProject.subFolder.TestClass {} -- does not work

While in previous version of Classic, you could set the type of a member, this is no longer possible in this version. I really felt like it was a bit unnecessary and and too slow overall.
You can still make members : public, private, final and static

That's it for now ! Let me know if there are any bugs in this version !

Updated pastebin link:
pastebin get gjTfwQ8P



#263937 Generic Table... kind of

Posted by Kouksi44 on 23 January 2017 - 12:30 AM in APIs and Utilities

Somehow I was able to forget to explain pretty much anything except for the most basic features... great.
I added a few more helper functions, most of them are simply their table.XXX() equivalent wrapped in another function to make them work with Lists.

Things you can now use:
Spoiler

Additionally, if you just want to add a value to the end of the list , you can now use this syntax:
<yourList> = <yourList> + 77


Updated the pastebin link in the main post !

Edit: Just in case anyone was wondering, yes you can make Lists containing Lists. List objects do have a __type field defining them as List objects.



#263832 Generic Table... kind of

Posted by Kouksi44 on 19 January 2017 - 11:16 PM in APIs and Utilities

This is probably pretty useless, but hey maybe someone can make something out of it...

This neat little tool allows you to define Lists that hold values of a certain type only. Doesn't sound like much but atleast it looks kind of pretty...

local myList = List : number {
  34;
  35;
  36;
}

As you can see you declare a List by writing:
List : <YourSuperSpecialType> {}

You can pass some initial values to the List by passing a table after the List type.

Another super nice feature is, that you can create your own "types" for your Lists. Lets say you want a List of e.g. Vectors.
That can be done by declaring a table with a __type field. Now if that field is defined to be
__type = "Vector"
these tables will be treated as if they were of type "Vector".
Comes in handy if you're writing some oop code and want to have List of your objects.

Install:
pastebin get 9mJL4rgH

Let me know if this usefull in any way or not ^^



#259309 Classic - pretty OOP in CC

Posted by Kouksi44 on 12 September 2016 - 07:48 PM in APIs and Utilities

Bug fix once again: Passing the self reference as an argument for another instance method could`ve led to some problems with private members being accessible from outside the object. Should be fixed now.

Pastebin updated !



#259253 Classic - pretty OOP in CC

Posted by Kouksi44 on 11 September 2016 - 09:10 PM in APIs and Utilities

View PostAdn, on 11 September 2016 - 06:46 PM, said:

I plan to develop an Apt-like package management for CC (https://en.wikipedia..._Packaging_Tool). My goal is to create an application for easy installation of popular CC/Lua scripts without the need to do research for the pastebins. I'm currently collecting useful and popular scripts and libraries and your idea of this OOP library is very nice. That's why I plan to add it as "libclassic" for other scripts. It would be nice if you tell me your opinion about my idea and if you don't want to see classic in my repository.

While I would recommend you to use existing options like Packman, you`re definitely free to use classic in any way you want to.



#259193 Classic - pretty OOP in CC

Posted by Kouksi44 on 10 September 2016 - 06:18 PM in APIs and Utilities

Small update: You can now choose to use
 SomeObject:someFunction(...) 
or
 SomeObject.someFunction(...) 
when calling methods of an object. The library will automatically supply the correct self reference of the object to the method.

Edit: Found another bug, pastebin has been updated again.



#257844 Thoughts on No Man's Sky?

Posted by Kouksi44 on 11 August 2016 - 03:23 PM in General

I haven't bought it yet but I am definitely planning to do so !
While I think it is true that No mans sky can get pretty repetitive after playing for some hours you should keep in mind that there are tons of updates ahead!
E.g. Base building and bigger ships are planned for the next update which is super cool imo :)



#256257 Classic - pretty OOP in CC

Posted by Kouksi44 on 16 July 2016 - 04:08 PM in APIs and Utilities

New update:

Finally added properties with getters and setters to classic !

They can be declared exactly like any other modifiers are added to members:
class "Test" {
  prop = property - "Hello World";
}

E.g. trying to do the following:
local obj = Test()
print(obj.prop)

Will call the getter associated with that property:
function Test:getProp()
  return self.prop.."!"
end

If you find any bugs or have an idea what to add next just tell me !



#256137 Classic - pretty OOP in CC

Posted by Kouksi44 on 14 July 2016 - 01:52 PM in APIs and Utilities

Bug Fix and some changes :

Trying to get a private member will no longer error and simply return nil. This is because of a change in the inheriting system.

Currently if you had a class structure like that,
Spoiler

setting the publicMember through an object of SubClass resulted in a new field in SubClass that held the new value, instead of the value of the super class being set to the new value.
After this update an object will always check if the index it is setting to a new value is present in the super object and if so, will try to set that one instead ( that means if the member of the super object is public and not final )



#256034 Classic - pretty OOP in CC

Posted by Kouksi44 on 12 July 2016 - 03:12 PM in APIs and Utilities

View Postjv110, on 11 July 2016 - 05:06 AM, said:

IMO
class "uh" {
  l = "sh";

  f = function(self)
	self.l = "f"
  end;

  git = function(self)
	self.l = "git"
  end;
}
looks a lot better than
class "uh" {
  l = "sh";
}

function uh:f()
self.l = "f"
end

function uh:git()
self.l = "git"
end

It doesn't matter wether you define functions directly in the class table or add them later on, both ways do work with the library.

I pushed an update to pastebin that should solve the problem with members holding objects of a class.

Using :

class "Test" {
  t = SomeClass();
}

does work now. Every instance of the class "Test" will hold an exact clone of any objects in the classtable.

Edit:
I just noticed that I left some helper functions uncovered in the mainpost.
E.g. if you ever need to check wether a table is an instance you can use the
 typeof(obj) 
function. That function will return "instance" if the passed table is an instance of a class. If it's not then it will simply return the type of the passed argument.
Another thing that was added recently is the
 someObject.invokeMethod(sMethod,...)
method (Must be called with a dot). It allows access to the internal Object functions that the library makes use of to manually set/get values and get member properties / types. I don't know if that function will be useful that often, but if the the neccessity to call internal Object functions should ever arise - here you go!
Just keep in mind that these internal functions are normally called from within the library and are able to break an object if they are used in the wrong manner.

Methods you can access through invokeMethod() :
Spoiler



#255606 Classic - pretty OOP in CC

Posted by Kouksi44 on 06 July 2016 - 01:46 AM in APIs and Utilities

View Postthecrimulo, on 06 July 2016 - 12:43 AM, said:

View PostKouksi44, on 05 July 2016 - 09:17 PM, said:

View PostExerro, on 05 July 2016 - 08:07 PM, said:

View PostLupus590, on 05 July 2016 - 07:12 PM, said:

View PostKouksi44, on 05 July 2016 - 05:31 PM, said:

-snip-

any way to allow "/" instead of "." ?
why do you have to use "." ?

Lua's require() uses ".", and I personally prefer it as it's more namespacey and less path/to/file-ey.

On the topic of the library:

This is awesome! It's really similar to how I did classes in Sheets, so naturally I'd think it has nice syntax and functionality. However, this has the added benefit of the magic metatable stuff for giving properties to fields of the class, which I have to admit is really clever and looks almost native.

My one real reservation is the name 'init' for the constructor. I've wanted to use a function called 'init' to initialise various things of a class in the past, so I think it'd be much better to use the name of the class as a constructor (i.e. Button:Button() not Button:init()).

Glad you like it!

A previous version of classic used the classname for the constructor but I think i changed it because I thought it looked a bit weird to write a function called
 function Button:Button () 

However I might change this again in the next update.

One thing I just realized :

Right now, you should avoid using ClassTypes or declaring class members that hold Objects in general.
Accessing those members won't work as copying those Objects once the class instance is created is a bit buggy.

So I can't pass an object as argument? I have something like this:
...
class "Window" implements "IWindow" {
  prop1;
  prop2;
  noObjectHereReally;
}
function Window:init(prop1,prop2)
  self.prop1 = prop1
  self.prop2 = prop2
end
interface "IDecorator" {
  draw = function(self, windowObject) -- Its only required here
	-- Code using values from windowObject
  end;
}
class "Decorator" implements "IDecorator" {
  prop1;
  prop2;
  noObjectHereReally;
}
function Decorator:init(prop1,prop2)
  self.prop1 = prop1
  self.prop2 = prop2
end
It ruins a bit my work :/

Passing Objects as arguments works perfectly fine.
Trying to do the following would not work :
class "Test" {
  member = SomeClass () -- can't be accessed by an object
}
local obj = Test ()
obj.member:someFunction ()
"member" holds an instance of "SomeClass" during the creation of the instance. Every object gets its own copy of the classtable but adding already defined objects to an instance is buggy without using the same object for all instances.

This will be fixed in the next update.



#255587 Classic - pretty OOP in CC

Posted by Kouksi44 on 05 July 2016 - 09:17 PM in APIs and Utilities

View PostExerro, on 05 July 2016 - 08:07 PM, said:

View PostLupus590, on 05 July 2016 - 07:12 PM, said:

View PostKouksi44, on 05 July 2016 - 05:31 PM, said:

-snip-

any way to allow "/" instead of "." ?
why do you have to use "." ?

Lua's require() uses ".", and I personally prefer it as it's more namespacey and less path/to/file-ey.

On the topic of the library:

This is awesome! It's really similar to how I did classes in Sheets, so naturally I'd think it has nice syntax and functionality. However, this has the added benefit of the magic metatable stuff for giving properties to fields of the class, which I have to admit is really clever and looks almost native.

My one real reservation is the name 'init' for the constructor. I've wanted to use a function called 'init' to initialise various things of a class in the past, so I think it'd be much better to use the name of the class as a constructor (i.e. Button:Button() not Button:init()).

Glad you like it!

A previous version of classic used the classname for the constructor but I think i changed it because I thought it looked a bit weird to write a function called
 function Button:Button () 

However I might change this again in the next update.

One thing I just realized :

Right now, you should avoid using ClassTypes or declaring class members that hold Objects in general.
Accessing those members won't work as copying those Objects once the class instance is created is a bit buggy.



#255560 Classic - pretty OOP in CC

Posted by Kouksi44 on 05 July 2016 - 05:31 PM in APIs and Utilities

If you want to use a class that is defined in another file you can import that class by calling:
 using "path.to.the.class" 

Note that "." is used instead of "/" in the path



#255406 Classic - pretty OOP in CC

Posted by Kouksi44 on 03 July 2016 - 05:54 PM in APIs and Utilities

Next update:

This update introduces a (IMO) pretty cool feature !
Members can now be declared to have certain datatypes so e.g. trying to set a member that was declared to hold only tables to anything else than a table will result in an error.

Example:
class "TestClass" {
  member = boolean - true
}

local obj = TestClass()
obj.member = "Hello World"  -- This will error !

This member can only be set to a value that is also boolean OR a nil value !

The only thing you have to keep in mind is, that lua already uses the string and table indexes.
Therefore if you want to define a string or table member you have to write "String" or "Table".

Besides the common lua types ( function, string, table, boolean, number and thread ) there are so called ClassTypes:
You can declare members to only hold objects of a certain class if the class has been defined previously.

Example:
class "Vector" {
  -- ...
}

class "TestClass" {
  member = Vector - Vector()
}

This update might have introduced a few bugs as it changed a lot of code in the background so let me know if anything seems to be broken !

Edit: Forgot to mention that ClassTypes basically check if the class of the given object equals the predefined ClassType or is atleast a subclass of that class!



#255118 Classic - pretty OOP in CC

Posted by Kouksi44 on 29 June 2016 - 08:50 PM in APIs and Utilities

Next update:

Members can now be defined as shared - those members will share their values across all instances of a class.

Example:
class "SharedTest" {
   test = public - shared - "Hello World";
}

local first = SharedTest()
local second = SharedTest()

first.test="World Hello"
print(second.test)

Even though we only set the member of first to "World Hello" printing the value of the member of second will result in the same string being printed.

Let me know about any bugs with this new update !



#254993 Classic - pretty OOP in CC

Posted by Kouksi44 on 28 June 2016 - 05:18 PM in APIs and Utilities

Hm I'm pretty sure that I won't be able to implement that kind of syntax.
 public function foo() 
I have no idea how to make public work in front of the function keyword without any metatable magic.

However I will try adding static members so that all instances of a class share that value !



#254990 Classic - pretty OOP in CC

Posted by Kouksi44 on 28 June 2016 - 03:36 PM in APIs and Utilities

Small update:

As far as I have tested it, loading the library through os.loadApi should work now.

Additionally, I have added some new features :
  • Members can now be made private or public ( public does not really differ from the normal way of defining members )
  • Members can now be made final so that their initial value can't be modified
Examples

Modifying members:

class "Example" {
   exampleValue = private - final - "Hello World";
}

local obj = Example()


As you can see, modifiers are put in front of the value of the member. They can be chained by separating them with "-".

In this example the value of exampleValue couldn't be changed and not be accessed from outside the Example class.

The pastebin link is still the same so simply run the install command once again !

If you find any bugs report them here !



#254967 Classic - pretty OOP in CC

Posted by Kouksi44 on 27 June 2016 - 11:16 PM in APIs and Utilities

View PostDannySMc, on 27 June 2016 - 05:58 PM, said:

I can't believe no one has commented, can I just say great work, this is a really nice to use library and I do use it a lot, is there a way you can make it so it's loadable as an API instead of using dofile?

My only query is I think you have the wrong concept of interfaces as I use them in PHP applications, and they are defined as below:
http://stackoverflow...ted-programming

Glad you like it ! I didn't really have any spare time to code in the last months, but there will definitely be an update to this library in the near future.

Adding support for os.loadApi shouldn't be too hard, it was simply more convenient for me to use dofile() as I could test my library directly in my editor that way.

The next update will probably contain a few other changes aswell:
- members can be declared as private and public
- members can be declared as static and final
- objects can be serialized and rebuilt