Author Topic: Lua Q&A  (Read 104225 times)

0 Members and 2 Guests are viewing this topic.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #165 on: July 28, 2013, 08:09:20 pm »
Code: [Select]
foo = Foo()
Is this really needed?
If you're asking this question, you may not know that Lua is case sensitive.
Here, Foo is the class (or the "template to copy") and foo is the new object (or an "instance of this class").
Foo() is equivalent to "new Foo()" in some other POO languages.
Foo() calls Foo:init().

[/quote]

I am aware that Lua is case sensitive, but can Foo() just be used directly in conjunction with . and :

eg:
Code: [Select]
Foo():bar(42)Actually I'm not sure that looks valid...

@Jim Bauwens: I think I'm starting to get the interaction between : and . now.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #166 on: July 28, 2013, 08:45:09 pm »
I am aware that Lua is case sensitive, but can Foo() just be used directly in conjunction with . and :

eg:
Code: [Select]
Foo():bar(42)Actually I'm not sure that looks valid...
It is valid, but keep in mind that it will create a new instance every time you use that.
It can be useful in certain specific cases, but not in general.
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #167 on: July 29, 2013, 01:18:44 am »
Actually, looking at it again,  I think I get it now more,

when you do foo = Foo(), that's similar to something like ball = Class() right?
It clicked for me when you said that Foo() was the class, and foo became the object.

About creating new instances with Foo():bar(42),
does that have any other disadvantages, such as some sort of memory leak?
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #168 on: July 29, 2013, 01:33:58 am »
when you do foo = Foo(), that's similar to something like ball = Class() right?
It clicked for me when you said that Foo() was the class, and foo became the object.
Yes.
It is equivalent to ball = new Class() in other POO languages.

Quote
About creating new instances with Foo():bar(42),
does that have any other disadvantages, such as some sort of memory leak?
Only Memory Leaks (and maybe stupid beginner errors like "why my code doz not work" just because your creating a new object each time that has its own properties).
« Last Edit: July 29, 2013, 01:34:32 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 Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #169 on: July 29, 2013, 08:45:42 am »
Quote from: mannuri
edit 2:
FINNALY!!!, i made it!
i just need var.store() all vars and its done.

Thank you guys i love you all!!
so var.store() solves the problem?
I wonder how it does that...


Code: [Select]
yi=math.eval("nSolve(fl=fr,yi)")
I was thinking this line might not work, but on second thought, it looks all right.


Also more questions
Also in my previous post, I made a mistake, class shouldn't be capitalised, should all be lower case, so that was wrong.
So clarification,

Code: [Select]
ball = class() -- creates a class called ball using the class function that is predefined
function ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code
   self.speed = spd -- these are added to the table, ball?
   self.size = size
end
« Last Edit: August 01, 2013, 09:28:18 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #170 on: July 29, 2013, 10:17:54 am »
So clarification,

Code: [Select]
ball = class() -- creates a class called ball using the class function that is predefined
function ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code
   self.speed = spd -- these are added to the table, ball?
   self.size = size
end
Yes (and it's 'speed' not 'spd').

However, it's a general habit that classes definitions are first-letter-capitalized :
Ball = class()
function Ball:init(arg1, arg2) ... end

etc.

and instances of the classes, first-letter-lowercase :

myBall = Ball(5,10)
« Last Edit: July 29, 2013, 10:20:30 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline mannuri

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #171 on: July 29, 2013, 10:39:43 am »
var.store() and math.eval() for me is like you put all out of lua and solve as a basic ti language, and take it back to lua.

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #172 on: July 29, 2013, 06:28:24 pm »
Code: [Select]
Ball = class()
Yeah I meant that as well, I forgot about that capitalised in that post. I already had these conventions in my current Lua project. Oops.

Also with instances of the classes, what if you want many instances using the same class,like at least 10? I've thought about using a table/list/array. Then I could call them by just referencing a particular value within the table.
Eg, using the function Ball:init(arg1, arg2)
Like this?
Code: [Select]
ballarray = {{3,4},{6,10},{4,11}...}


Code: [Select]
self.speed = spd
So this is not valid? does it have to be self.speed = speed, or self.speed = 0[a number], or self.speed = {}
EDIT: Oh I see the self.speed has to match the arguments.

function ball:init(speed, size)
   self.speed = speed
   self.size = size

I thought assigning any variable with a valid name would do ok...

Also thanks mannuri for explaining var.store() and math.eval()
« Last Edit: July 29, 2013, 10:21:57 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #173 on: July 29, 2013, 07:15:57 pm »
Understand this, you'll understand everything :

Declarations :
Code: [Select]
PointClass = class()
function PointClass:init(x, y)
  self.x = x
  self.y = y
end

BallClass = class(PointClass)
function BallClass:init(x, y, radius)
  PointClass.init(self, x, y)
  self.radius = radius
end

function BallClass:paint(gc)
  gc:fillArc(self.x, self.y, self.radius*2, self.radius*2, 0, 360)
end

Usage :
Code: [Select]
local ball1, ball2
ball1 = BallClass(0, 0, 10)
ball2 = BallClass(50, 50, 15)
ball_tbl = {ball1, ball2}

function on.paint(gc)
  for _, ball in ipairs(ball_tbl) do
    ball:paint(gc)
  end
end
« Last Edit: July 29, 2013, 07:16:32 pm 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 Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #174 on: July 30, 2013, 08:44:06 am »
Code: [Select]
PointClass.init(self, x, y)

What does the first argument (self) represent in relation to PointClass?
as compared to
Code: [Select]
PointClass:init(x, y)

Code: [Select]
for _, ball in ipairs(ball_tbl) do
I'm not 100% what in ipairs means as I've never had to use it. Also what does for _ mean? I'm more used to something like for x=sample, sample+9 do.

I know I'm getting closer to understanding everything...
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Lua Q&A
« Reply #175 on: July 30, 2013, 08:52:58 am »
ipairs will iterate over the pairs (1, ball[1]), (2, ball[2], ..., (n, ball[n]) till there is no 'n' anymore.
The underscore is just a variable like a,b,c,d etc. but used when you don't care about the variable. Say you were only interested in the values, you'd do
Code: [Select]
for _, values in ipairs(ball_tbl) do
 --Do something here
end
AFAIK it's just a style recommendation to indicate you ignore that variable.
« Last Edit: July 30, 2013, 08:53:50 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #176 on: July 30, 2013, 09:16:45 am »
Code: [Select]
PointClass.init(self, x, y)
What does the first argument (self) represent in relation to PointClass?
as compared to
Code: [Select]
PointClass:init(x, y)
Well, this wouldn't work. (the ":" would mean that the PointClass table (class definition !) is passed.)

For the first line of code you quoted, "self", there, is from the BallClass, not the PointClass. He's using PointClass.init (and not :init) because he specifies the object to be used for instanciation : the current (ball) self. As said before, ":" is just a shortcut for a.b(self)


Also, for pairs/ipairs : look here http://lua-users.org/wiki/ForTutorial
« Last Edit: July 30, 2013, 09:18:44 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #177 on: July 31, 2013, 07:52:10 am »
Ah, I'm making stupid mistakes again, I go back and look at it again and see that that line is within the function BallClass:init(x, y, radius)

Oh of course, I get what ipairs is now. Takes all the indices in numbered order (ones that can be ordered anyway), and has the indicies and keys from the table in pairs. Ok.

Oh wow I didn't know that _ could be used as a variable. Kinda like a wild card. Is it?
Spoiler For This question isn't so important, the more pressing stuff is below:
so can _ be used mixed like how you would search using * in other languages
Eg: you have variables bark, bork, berk
can you look for b_rk somehow?

Quote
":" is just a shortcut for a.b(self)
So PointClass:init(x, y) is a shortcut for PointClass.init(PointClass, x, y)
How does that translate to something in a table?

I'm trying to work through in my mind the connections between all this stuff, and I still don't quite get it. I even drew a diagram, draft version, probably some mistake in there ;).
To describe what i'm not getting, it's the syntax that's getting me. I'm having trouble understanding the meaning of certain parts of the syntax lua classes use.
« Last Edit: July 31, 2013, 07:54:12 am by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #178 on: July 31, 2013, 02:18:08 pm »
Ah, I'm making stupid mistakes again, I go back and look at it again and see that that line is within the function BallClass:init(x, y, radius)

Oh of course, I get what ipairs is now. Takes all the indices in numbered order (ones that can be ordered anyway), and has the indicies and keys from the table in pairs. Ok.

Oh wow I didn't know that _ could be used as a variable. Kinda like a wild card. Is it?
Spoiler For This question isn't so important, the more pressing stuff is below:
so can _ be used mixed like how you would search using * in other languages
Eg: you have variables bark, bork, berk
can you look for b_rk somehow?

No, "_" is just a valid char as a variable name such as _a, _foo, ________bar, foo_bar or even _ or _____, etc ...
Futhermore, "_" is in the Lua's culture (same in OCaml for example) a junk variable. It is then used when you know you don't want to use it.
But this doesn't mean it has a special meaning for the Lua's interpretor, for example this works :
Code: [Select]
local _o = 42
for _=1, _o do
 print(_, _+1, _o/_)
end


Quote
Quote
":" is just a shortcut for a.b(self)
So PointClass:init(x, y) is a shortcut for PointClass.init(PointClass, x, y)

I'm trying to work through in my mind the connections between all this stuff, and I still don't quite get it. I even drew a diagram, draft version, probably some mistake in there ;).
To describe what i'm not getting, it's the syntax that's getting me. I'm having trouble understanding the meaning of certain parts of the syntax lua classes use.

Yes ":" is a shortcut to "." + self, you understood that well.
Where you're going "too far" in the understanding is that you substitude "self" as the ClassName which is not always the case.
"self" is a dynamic variable, its meaning is determined at running time. Just remind that "self" refers to the first argument when using the ":" notation.
For example, these 3 calls are differents :
Code: [Select]
Toto = class()
function Toto:init()
  print(self)
end

Toto:init()     -- equivalent to Toto.init(Toto)
lol = Toto()   -- equivalent to Toto.init(temp) where "temp" will be stored in "lol"
lal = lol.init({}) -- here we force "self" to be "{}", an empty table


Quote
How does that translate to something in a table?

This is an intersting deep question. Jim will maybe better explain than me, but I'll try something :
In Lua, table have a "prototype" table that describe their content and how to access an element. This inner table is called "metatable".
You can get/set the metatables using getmetatable(tbl) or setmetatable(tbl, meta).
There are special elements of the metatable that changes the meaning of tbl[a] for example, or t1 + t2. It's like operator overloading.

Metatables in classes are used to transfert all the links to the functions from the "template" (the class) to the "instance" (the object) without copying them.
This means that when you're doing foo = Foo(), you end up with a new table (foo) that has the metatable of Foo (the class) that refers to every single function you defined in Foo (Foo.bar() for example).
Using the same principle, when doing Bar = class(Foo), you end up with a new table (Bar) that has the metatable of Foo (the parent class) that refers to every single function you defined in Foo. What's the difference then ? Foo() will also call Foo:init() after the copy (the constructor).
« Last Edit: July 31, 2013, 02:21:04 pm 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 Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Lua Q&A
« Reply #179 on: August 01, 2013, 09:24:37 am »
Ah thank you, this helps. Thanks for the help in explaining self, my head is still flipping from the uses of . and : but now I've understood the concepts, the stuff previously said makes more sense now.

Code: [Select]
Bar = class(Foo)
So this will also call Foo:init() after it assigns the table of Bar? That's interesting.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)