Is it possible to make a class from the code?

I have tried, but I can’t make it work. I’m pretty sure it will use json or something, correct?

Thanks, Prynok

@Prynok Sorry I’m not sure what you are asking for. What do you mean by class?

@Briarfox I don’t know how else to explain it. Its like different parts of the program that all work together. I’m trying to make all the entities have their own class that way it doesn’t take up a large part of other classes that I have.

@prynok Couldnt you just create your classes and include them as a dependency? I’m not sure what you want to use json for.

You can do something like

function makeClass(a)
   local myClass=class()
   function myClass:init(x,y)
     --your init code
   end
   function myClass:draw()
     print(a)
     --your draw code
   end
   --your other methods
   return myClass
end

The advantage of creating classes this way is that you can make upvalues, which mean variables that can only be accessed by the class functions. For example, with the code above, once the class is created, it’s draw function will always print the same thing.

It’s the same as the following code which creates independent counters:

function makeCounter()
   local a=0
   local res = function()
     a=a+1
     print(a)
   end
   return res
end

C=makeCounter()
D=makeCounter()

D() --prints 1
D() --prints 2
D() --prints 3
C() --prints 1
D() --prints 4