Author Topic: Lua Classes  (Read 14763 times)

0 Members and 2 Guests are viewing this topic.

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Classes
« Reply #15 on: January 04, 2012, 03:43:35 pm »
Here's the source code of the class() function, that TI uses :


(not tested)
Code: [Select]
-- Class definition system
class = 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 derived
end

Paste that and use  blablabla=class()  now :)
« Last Edit: January 04, 2012, 03:45:46 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #16 on: January 04, 2012, 03:44:52 pm »
You can, but it works in a different way then the calculator way.
1. Its not event based (but you can make it like that)
2. It has no gc like we now on the calculator
3. Standard is only console, but you can add gui support by installing modules (or rocks).

Edit, thanks Adriweb :)
« Last Edit: January 04, 2012, 03:45:27 pm by jimbauwens »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua Classes
« Reply #17 on: January 04, 2012, 03:49:52 pm »
well yeah, i found this one: http://www.tecgraf.puc-rio.br/iup/
i'll study it..

and how can you make it event-based?

i know some visual basic, will that help?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #18 on: January 04, 2012, 03:51:46 pm »
You just make a loop were you call certain functions when certain stuff are true :)
Visual basic has nothing to do with Lua :P

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua Classes
« Reply #19 on: January 04, 2012, 03:57:33 pm »
Here's the source code of the class() function, that TI uses :


(not tested)
Code: [Select]
-- Class definition system
class = 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 derived
end

Paste that and use  blablabla=class()  now :)

So this should work in luaFX also?



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #20 on: January 04, 2012, 03:57:57 pm »
Yes, it will :)

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua Classes
« Reply #21 on: January 05, 2012, 09:43:12 am »
OK, so now my question is, what is so special about classes and how can I take advantage of them?

And thanks for answering all my stupid questions jimbauwens :)



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Classes
« Reply #22 on: January 05, 2012, 10:08:57 am »
https://github.com/adriweb/Nspire-BreakOut/blob/master/BreakOut.lua

take a look at this code (I made it so it can be helpful for beginners and people who want to learn class-oriented lua programming on the Nspire) and try to understand a way I could have had as many balls, blocks etc. as I wished without using classses and without having to write everything for each object.

Have fun :D

(btw,  it's almost impossible)

That's what classes are for : creating once and for all, a "definition" of objects, and creating as many objects as you want of that class, so they will inherit the class properties.

Google "object oriented programming" and you'll see in detail what I mean.
« Last Edit: January 05, 2012, 10:13:32 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Classes
« Reply #23 on: January 05, 2012, 10:12:12 am »
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..
« Last Edit: January 05, 2012, 10:16:46 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua Classes
« Reply #24 on: January 05, 2012, 10:19:12 am »
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..


Oh, wow, I've been wondering how to do this for a while! Now I understand what classes are for! THX!!!! :D



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua Classes
« Reply #25 on: January 05, 2012, 10:22:54 am »
Secondly, 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 Ö

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Classes
« Reply #26 on: January 05, 2012, 10:24:20 am »
Secondly, 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 Ö
For a while here (like 6 months now :D) :
http://levak.free.fr/ftp/nspire/Make3D/
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Classes
« Reply #27 on: January 05, 2012, 10:25:41 am »
Or check EEPro.

Also Levak, I was before you.
* jimbauwens runs

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Classes
« Reply #28 on: January 05, 2012, 10:29:46 am »
Or check EEPro.

Also Levak, I was before you.
* jimbauwens runs

Never said I was first Ö
Basicly, it's Makefile based (or bash like me), you have to concatenate each files in a single Lua file (or xml for the Library version of Make3D)
« Last Edit: January 05, 2012, 10:30:03 am by Levak »
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Classes
« Reply #29 on: January 05, 2012, 10:31:57 am »
Secondly, 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 Ö

Example for EEPro, indeed :

My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation