Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is it possible to make a class from the code?

edited March 2014 in Competition Posts: 577

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

Thanks, Prynok

Tagged:

Comments

  • BriarfoxBriarfox Mod
    Posts: 1,542

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

  • Posts: 577

    @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.

  • BriarfoxBriarfox Mod
    Posts: 1,542

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

  • edited March 2014 Posts: 16

    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
    
Sign In or Register to comment.