0

Like asked in the title, lets say I want to add a custom method to the table type, lets say table:printContent(), is there any way in Lua to achieve this? I mean only, pure, Lua. In C#, for example, you can use extensions to do that:

using System;
namespace Main {
    public static class Extension {
        public static void printContent(this table mytable) {
            foreach(var content in mytable) {
                Console.WriteLine(content.ToString());
            }
        }
    }  
}

Now is the same possible, only in Lua?

The question I inspired me from (that question didn‘t tought me what I wanted to learn, and yes I want OOP, if I want to mod for example in Lua)

How do I add a method to the table type?

  • 5
    how is your question any different from the one you linked in your post? – Piglet Apr 04 '21 at 17:41
  • The question itself isnt different, but as I wrote/specified, the solution dosent really match the post. I want to know if its possible, yes or not, I dont want to hear anything other (you can do that without OOP, for example, I really want to know if I can do that and if yes, then how) – FrostDracony Apr 04 '21 at 21:17
  • 1
    The accepted answer to that question says "There is no single metatable for all tables." That's really the best answer for this question. Tables aren't designed to have a one-size-fits-all set of methods. They're designed to represent all kinds of different objects. – luther Apr 04 '21 at 22:23
  • Ok, thanks, so its a no for tables, but can it be a yes for classes? Custom classes that aren‘t created by me (if I want to mod for example, there are classes before, what if I want to modify these classes? I could use a wrapper, but are there any other ways?) – FrostDracony Apr 05 '21 at 07:10

1 Answers1

1

Ok, thanks, so its a no for tables, but can it be a yes for classes? Custom classes that aren‘t created by me (if I want to mod for example, there are classes before, what if I want to modify these classes? I could use a wrapper, but are there any other ways?)

A class is always implemented using Lua tables.

Minimum example:

MyClass = {}
function MyClass:new(o)
  o = o or {}
  setmetatable(o, self)
  self.__index = self
  return o
end

If you want to add a new method to MyClass simply implement it

function MyClass:MyNewMethod()
end

The same way can of course be used to overwrite existing methods.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Thanks a lot, but what if I dont have access to the class? I mean like there is some class called `GameObject` (not the real name but that s only a example), that I cant access (By this I mean that I can use the class, but I didn‘t made the class so I can‘t really do that), would the IDE recoignise that? – FrostDracony Apr 05 '21 at 16:00
  • what do you mean with you don't have access to the class? as long as that class is not managed in C you can change it. give me one example where this does not work – Piglet Apr 05 '21 at 16:04
  • I want to use Lua for modding purposes, so I dont create the class I want to give custom extensions. I am currently unable to use my PC (until tomorrow); but I can try your methond ASAP and say if it works or not. I still really appreciate your help, thanks for it – FrostDracony Apr 05 '21 at 16:36