Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
23 May, 2013, 03:43:31 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: 1 [2] 3   Go Down
  Print  
Author Topic: Lua Classes -  (Read 1577 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
adriweb
Editor
LV9 Veteran (Next: 1337)
*
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 00:38:33
Date Registered: 13 April, 2011, 18:42:59
Location: South of France
Posts: 1196


Total Post Ratings: +185

View Profile WWW
« Reply #15 on: 04 January, 2012, 22:43:35 »
+1

Here's the source code of the class() function, that TI uses :


(not tested)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 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 Smiley
« Last Edit: 04 January, 2012, 22:45:46 by adriweb » Logged


TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 22:51:04
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #16 on: 04 January, 2012, 22:44:52 »
0

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 Smiley
« Last Edit: 04 January, 2012, 22:45:27 by jimbauwens » Logged

Nick
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 20:44:19
Date Registered: 05 June, 2011, 20:01:07
Location: 51° 12′ 34″ N, 3° 13′ 31″ E
Posts: 1178


Total Post Ratings: +158

View Profile WWW
« Reply #17 on: 04 January, 2012, 22:49:52 »
0

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?
Logged

Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 22:51:04
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #18 on: 04 January, 2012, 22:51:46 »
0

You just make a loop were you call certain functions when certain stuff are true Smiley
Visual basic has nothing to do with Lua Tongue
Logged

flyingfisch
I'm 1337 now!
LV10 31337 u53r (Next: 2000)
**********
Offline Offline

Gender: Male
Last Login: Yesterday at 23:45:21
Date Registered: 26 August, 2011, 21:18:14
Location: OH, USA
Posts: 1497


Total Post Ratings: +75

View Profile WWW
« Reply #19 on: 04 January, 2012, 22:57:33 »
0

Here's the source code of the class() function, that TI uses :


(not tested)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 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 Smiley

So this should work in luaFX also?
Logged




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
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 22:51:04
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #20 on: 04 January, 2012, 22:57:57 »
+1

Yes, it will Smiley
Logged

flyingfisch
I'm 1337 now!
LV10 31337 u53r (Next: 2000)
**********
Offline Offline

Gender: Male
Last Login: Yesterday at 23:45:21
Date Registered: 26 August, 2011, 21:18:14
Location: OH, USA
Posts: 1497


Total Post Ratings: +75

View Profile WWW
« Reply #21 on: 05 January, 2012, 16:43:12 »
0

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 Smiley
Logged




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
adriweb
Editor
LV9 Veteran (Next: 1337)
*
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 00:38:33
Date Registered: 13 April, 2011, 18:42:59
Location: South of France
Posts: 1196


Total Post Ratings: +185

View Profile WWW
« Reply #22 on: 05 January, 2012, 17:08:57 »
0

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 Cheesy

(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: 05 January, 2012, 17:13:32 by adriweb » Logged


TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation
Levak
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 03:08:23
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 843


Total Post Ratings: +148

View Profile WWW
« Reply #23 on: 05 January, 2012, 17:12:12 »
0

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: 05 January, 2012, 17:16:46 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
flyingfisch
I'm 1337 now!
LV10 31337 u53r (Next: 2000)
**********
Offline Offline

Gender: Male
Last Login: Yesterday at 23:45:21
Date Registered: 26 August, 2011, 21:18:14
Location: OH, USA
Posts: 1497


Total Post Ratings: +75

View Profile WWW
« Reply #24 on: 05 January, 2012, 17:19:12 »
0

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!!!! Cheesy
Logged




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
Nick
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 20:44:19
Date Registered: 05 June, 2011, 20:01:07
Location: 51° 12′ 34″ N, 3° 13′ 31″ E
Posts: 1178


Total Post Ratings: +158

View Profile WWW
« Reply #25 on: 05 January, 2012, 17:22:54 »
0

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 Ö
Logged

Levak
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 03:08:23
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 843


Total Post Ratings: +148

View Profile WWW
« Reply #26 on: 05 January, 2012, 17:24:20 »
0

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 Cheesy) :
http://levak.free.fr/ftp/nspire/Make3D/
Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Yesterday at 22:51:04
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #27 on: 05 January, 2012, 17:25:41 »
0

Or check EEPro.

Also Levak, I was before you.
* jimbauwens runs
Logged

Levak
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 03:08:23
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 843


Total Post Ratings: +148

View Profile WWW
« Reply #28 on: 05 January, 2012, 17:29:46 »
0

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: 05 January, 2012, 17:30:03 by Levak » Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
adriweb
Editor
LV9 Veteran (Next: 1337)
*
Offline Offline

Gender: Male
Last Login: 20 May, 2013, 00:38:33
Date Registered: 13 April, 2011, 18:42:59
Location: South of France
Posts: 1196


Total Post Ratings: +185

View Profile WWW
« Reply #29 on: 05 January, 2012, 17:31:57 »
0

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 :

Logged


TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation
Pages: 1 [2] 3   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.242 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.