0 Members and 2 Guests are viewing this topic.
-- Class definition systemclass = function(prototype) local derived = {} local derivedMT = { __index = prototype, __call = function(proto, ...) local instance = {} local instanceMT = { __index = derived, __call = function() return nil, "attempt to invoke an instance of a class" end, } setmetatable(instance, instanceMT) if instance.init then instance:init(...) end return instance end, } setmetatable(derived, derivedMT) return derivedend
Here's the source code of the class() function, that TI uses :(not tested)Code: [Select]-- Class definition systemclass = function(prototype) local derived = {} local derivedMT = { __index = prototype, __call = function(proto, ...) local instance = {} local instanceMT = { __index = derived, __call = function() return nil, "attempt to invoke an instance of a class" end, } setmetatable(instance, instanceMT) if instance.init then instance:init(...) end return instance end, } setmetatable(derived, derivedMT) return derivedendPaste that and use blablabla=class() now
"welcome to the world of computers, where everything seems to be based on random number generators"
The principle of classes / objects is simple : you can make copies of it, you can duplicate much much time and reach a certain amount of variables that you can't manage without (or with arrays but ... well you get it).An object represents a bunch of preperties and a bunch of methods ALL related to this "kind" of object.That way you won't have troubles to find other function names for this, this or that. Here is an example : "vector1_toString", "vector2_toString" etc... this is bad and painful, just define it once and call the object "vector" and the method name "toString", then, make bunch of that object.This is a general approach. Now, I'm giving you a _real_ example : Imagine a bunch of aliens sprites and a spaceship (space invaders thus). How are you going to store each aliens positions ? You can make two arrays, one for x, one for y. Ok. But then you have to manage somthing else : the spaceship position. Another varname x and another y. You reached 2 arrays and 2 vars.Now let's see with classes : An array of "SpaceShipSprite" objects (aliens), and another "SpaceShipSprite" object for the space ship.Each position is object.x and object.y (for example) and you only have to manage 1 array and 1 object.You see now ?Secondly, classes makes your code looking clever, sperated in multiple files, each file is related for one object and co..
Secondly, classes makes your code looking clever, sperated in multiple files, each file is related for one object and co..
Quote from: Levak on January 05, 2012, 10:12:12 amSecondly, classes makes your code looking clever, sperated in multiple files, each file is related for one object and co..how can you do this? i mean, writing every method/class in a seperate file would be sooo useful Ö
Or check EEPro. Also Levak, I was before you. * jimbauwens runs
how can you do this? i mean, writing every method/class in a seperate file would be sooo useful Ö