Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
24 May, 2013, 21:39:11 *
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 1580 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
pianoman
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427


Topic starter
Total Post Ratings: +24

View Profile
« on: 29 July, 2011, 18:03:10 »
0

I tried reading the thing on inspired-lua.org, but google translate sucks, so I couldn't understand much.
Basically, what I'm asking boils down to three things: what are classes, how do you use them, and is there anything special you can do with them?
Thanks!
Logged

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

Gender: Male
Last Login: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #1 on: 29 July, 2011, 18:55:58 »
+1

Well, I'll try my best to explain them.

Imagine you have a game were you can have many different cars. All the cars have different things like engine power, but still share much stuff. This is were classes come in.
With classes you can make a basic structure of an object, and create new objects based on the first one. Nspire Lua has the class() function that saves us lots of work.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
car = class()

function car: init(power, type, lights, etc)
    self.power = power
    self.type = type
    self.lights = lights*2
    self.options = etc
end

function car: drive(speed)
   return self.power * speed -- this doesn't make much sense, but its just an example
end

Now, once we made our basic car class, we can define cars using this class:

1
2
3
4
5
6
7
8
9
10
11
12
volvo = car(10, "2x2", 4, {air-conditioning = True})
jeep = car(60, "4x4", 12, {air-conditioning = False, trailer-hook = True})

jeep:drive(10)       -- returns 600
volvo:drive(20)      -- returns 200

if jeep.options.trailer-hook then
   print("The jeep can have a trailer")
else
   print("No trailer for you :(")
end

This is just a basic example, and it written here without testing, so it can have some bugs.
« Last Edit: 23 December, 2011, 22:04:39 by jimbauwens » Logged

pianoman
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427


Topic starter
Total Post Ratings: +24

View Profile
« Reply #2 on: 29 July, 2011, 18:59:42 »
0

Oh, I think I get it. Thanks!
Logged

pianoman
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427


Topic starter
Total Post Ratings: +24

View Profile
« Reply #3 on: 17 August, 2011, 18:18:00 »
0

Sorry, I'm still just a bit confused.
Let's say you created a class for a button. How would you initialize it so that you could actually draw it and do stuff with it?
Or let's say that you want to be able to create something on a keypress. How could you set it up so that, theoretically, you could continue generating objects infinitely?
I kinda tried to read the inspired-lua.org explanation, but Google Translate still sucks Tongue
Thank you very much!
« Last Edit: 17 August, 2011, 18:19:19 by pianoman » Logged

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

Gender: Male
Last Login: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #4 on: 17 August, 2011, 21:18:57 »
0

Well, it really depends how you want to do it Smiley

First create the class and its init script. The init script for a button would set for example it label and size. Then you need to add an paint function to it, were you draw it.
Also, you should keep a table with references to all the buttons, so that you can link event to it.
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--The table we keep all references to created buttons in
ButtonTable = {}

--Create the class
button = class()

--Create and init the button
function button:init(label, x, y, action)
    self.label = label
    self.x = x
    self.y = y
    self.action = action
    self.labelWidth = 0
    self.width = 0
    self.height = 20
    
    table.insert(ButtonTable, self)
end

--Draw the button
function button:paint(gc)
self.labelWidth = gc:getStringWidth(self.label)
self.width = self.labelWidth + 4
gc:setColorRGB(0,0,0)
gc:setFont("serif", "b", 12)

gc:drawRect(self.x, self.y, self.width, self.height)
gc:drawString(self.label, self.x+1, self.y+2, "top")
end

Well, but now you will wonder how to make it clickable and all.
Look at this full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
--The table we keep all references to created buttons in
ButtonTable = {}

--Create the class
button = class()

--Create and init the button
function button:init(label, x, y, action)
    self.label = label
    self.x = x
    self.y = y
    self.action = action
    self.labelWidth = 0
    self.width = 0
    self.height = 20
    
    table.insert(ButtonTable, self)
end

--Draw the button
function button:paint(gc)
self.labelWidth = gc:getStringWidth(self.label)
self.width = self.labelWidth + 4
gc:setColorRGB(0,0,0)
gc:setFont("serif", "b", 12)

gc:drawRect(self.x, self.y, self.width, self.height)
gc:drawString(self.label, self.x+1, self.y+2, "top")
end

--What happens if you click the button
function button:click()
self.action()
end

--Check if clicked, and if clicked call its click method
function button:checkClick(x, y)
if y >= self.y and y <= self.y + self.height and x >= self.x and x <= self.x + self.width then
self:click()
end
end

--Check ButtonTable, and draw all the button in it
function drawButtons(gc)
for _, selectedButton in pairs(ButtonTable) do
selectedButton:paint(gc)
end
end


----------------------


--The on.create even. Here we create a sample button, and make that it will call testFunction when clicked
function on.create()
button1 = button("Test!", 100, 50, testFunction)
end


--TestFunction
function testFunction()
bclicked = true
platform.window:invalidate()
end

--The paint event
function on.paint(gc)
if bclicked then
gc:drawString("The button was clicked", 10, 10, "top")
end

drawButtons(gc)
end

--Link the click event to the buttons
function on.mouseDown(x, y)
for _, selectedButton in pairs(ButtonTable) do
selectedButton:checkClick(x, y)
end
end

This should explain most of it Smiley
Note that I didn't try this, so there might be some errors.

I strongly recommend you too look at the source of Adriweb's contest entry here: https://github.com/adriweb/Nspire-BreakOut .
Its a very good example of what you want to do, and should be easy to understand Smiley

EDIT:
Ok, tested it and fixed some bugs Smiley (Thanks Adriweb!)
« Last Edit: 23 December, 2011, 22:03:51 by jimbauwens » Logged

pianoman
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 20 October, 2011, 04:23:28
Date Registered: 21 May, 2011, 19:13:57
Location: You can know that my current velocity is about 0 m/s.
Posts: 427


Topic starter
Total Post Ratings: +24

View Profile
« Reply #5 on: 18 August, 2011, 05:20:17 »
0

I actually understood you Smiley
Thank you once again, jim and adriweb! Cheesy
Logged

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

Gender: Male
Last Login: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #6 on: 18 August, 2011, 11:33:07 »
0

I'm glad I could be of help Smiley
Logged

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

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


Total Post Ratings: +148

View Profile WWW
« Reply #7 on: 18 August, 2011, 11:51:41 »
0

Jim, we really have to translate this tutorial for inspired-lua Cheesy
« Last Edit: 18 August, 2011, 11:51:51 by Levak » 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: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #8 on: 18 August, 2011, 11:53:08 »
0

Adriweb was planning to do it when he had some time.
I could do it, but I don't have much time either :p
Logged

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

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


Total Post Ratings: +74

View Profile WWW
« Reply #9 on: 04 January, 2012, 21:21:16 »
0

Sorry to necro-post, but cant you do this instead of using classes?


1
2
3
4
5
6
car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}

function PrintTopSpeed
graydraw.print(car.car1.TopSpeed)
end
« Last Edit: 04 January, 2012, 21:21:24 by flyingfisch » 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: Today at 14:17:04
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 #10 on: 04 January, 2012, 21:24:18 »
0

euh, why would you do that? you have the ease of use to play with classes..
they're really easy to handle, what's the reason you want to do it this way?
Logged

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

Gender: Male
Last Login: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #11 on: 04 January, 2012, 22:02:34 »
0

Sorry to necro-post, but cant you do this instead of using classes?


1
2
3
4
5
6
car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}

function PrintTopSpeed
graydraw.print(car.car1.TopSpeed)
end

You can, but for certain stuff classes is much better.
It allows you to do very nice stuff which are not possible (like, each car has its own subroutines).
Logged

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

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


Total Post Ratings: +74

View Profile WWW
« Reply #12 on: 04 January, 2012, 22:36:11 »
0

How do you do this with LuaForWindows? (I dont think they have a class function)
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: Today at 20:33:25
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« Reply #13 on: 04 January, 2012, 22:41:22 »
0

No, TI added the class() function.
However, its very easy to add that function on the pc since its a simple function Smiley
Logged

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

Gender: Male
Last Login: Today at 14:17:04
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 #14 on: 04 January, 2012, 22:42:39 »
0

i had some questions about that lua for windows

can you create a full program with it, with windows (real ones, not the OS) and stuff?
Logged

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.269 seconds with 30 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.