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

0 Members and 3 Guests are viewing this topic.

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #180 on: August 01, 2013, 11:59:02 am »
Code: [Select]
Bar = class(Foo)
So this will also call Foo:init() after it assigns the table of Bar? That's interesting.
No, this is not what I've written :

Quote
What's the difference then ? Foo() will call Foo:init() after the copy (the constructor).

class() does not call init().
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 Q&A
« Reply #181 on: August 02, 2013, 12:17:26 pm »
If you want to know what's behind the scene, the source code of the "class" function implementation is here : http://wiki.inspired-lua.org/class
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 #182 on: August 04, 2013, 09:53:03 am »
@adriweb: Yeah I've seen it, I understand the actual definition even less. Sorry.
http://ourl.ca/11717/354363

@Levak: Yeah I don't know what I was going on back there.  We were looking at
foo = Foo()
and
Bar = class(Foo)
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 #183 on: August 04, 2013, 01:00:19 pm »
@adriweb: Yeah I've seen it, I understand the actual definition even less. Sorry.
Oops yeah sorry, didn't see/remember.
But indeed it's not a trivial code for novice programmers.

and to sum up the init() call, class() does not call it, it's when you create an instance of the earlier-defined class that it's called.
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 #184 on: August 04, 2013, 10:17:05 pm »
EDIT: I changed some things because I changed my understanding...
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').

I am so confused, which parts in initialising self.blah variables have to match the arguments? In this case, speed and size? I thought only the blah part in self.blah had to match the arguments.

I thought there was no restrictions, i.e. doing this wouldn't give any errors when executing the code.


Taking some code from my (currently) secret project

Code: [Select]
function Units:init(plr, x, y, str, move, opt) -- player, xy position on map, strength, movement, options
self.plr = uplr
self.x = ux
self.y = uy
self.str = ustr
self.move = umove
self.opt = {}
end
Is this valid at all? EDIT OVERRIDE: No, it is not valid code. I think because uplr, ux, uy aren't defined? Hence self.plr, self.x sel.y ... are assigned values of nil. That's why I'm not getting an error, even when calling the function. It's only when I tested trying to gc:drawstring() ustr that it got a nil error.

So if I just change it to

Code: [Select]
function Units:init(plr, x, y, str, move, opt) -- player, xy position on map, strength, movement, options
self.ran = plr
self.dom = x
self.hah = y
self.yea = str
self.ble = move
self.see = {}
        self.new = 10
end
That would be valid, except I probably wouldn't name those self. variable like that, of course

I'm still not sure what all these things do. And what would be the output if I were to call Units and/or Units() ?

I'm also using this to get my head around it as well: http://ourl.ca/12291/231625

As said on my custom title: whyda slow progress in programming? Imma struggle beyond basic concepts...
« Last Edit: August 04, 2013, 10:35:31 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 jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Lua Q&A
« Reply #185 on: August 04, 2013, 10:44:07 pm »
Your origional code would work except when you look at your constructor, your parameters must be equil to your variable.



ex:
Code: [Select]
function Units:init(plr, x, y, str, move, opt) --these are not your self. variables, they are just values that are passed when it is called.
  self.plr = plr --Notice how you are setting the self. variable to the variable passed in the constructor.
  self.x = x
  self.y = y
  self.str = str
  self.move = move
  self.opt = opt
end

When you used uplr instead of the plr you passed in your constructor, you essentialy are setting self.plr to nil because uplr does not exist.
« Last Edit: August 04, 2013, 10:45:23 pm by jwalker »
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

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 #186 on: August 04, 2013, 10:49:42 pm »
Yep I edited my posts and talked about the uplr, because uplr hasn't been defined yet.
I edited my post several times, so I may have started to answer my own questions :D

Quote
your parameters must be equil to your variable.
as in? The arguments in the function?
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 jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Lua Q&A
« Reply #187 on: August 04, 2013, 10:54:40 pm »
Yes, as in arguments.
« Last Edit: August 04, 2013, 10:57:13 pm by jwalker »
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #188 on: August 05, 2013, 01:54:36 am »
I really don't understand why you're struggling with scoped-variables-overriding.

What I mean :
Code: [Select]
local foo = 42
{ -- new scope, it can a function, for, while, etc ..
    local foo = 69
    print(foo) -- prints 69
}
print(foo) -- prints 42
Is that a new concept for you ?

Because here, "self" is the object itself, it won't colide with current scope variables.

Thus, this is valid :
Code: [Select]
Foo = class()
function Foo:init(x, y, z)
  self.x = x
  self.y = y
  self.z = z
end

And this is invalid :
Code: [Select]
Foo = class()
function Foo:init(xx, yy, zz)
  x = xx
  y = yy
  z = zz
end


But obviously, this is also valid :
Code: [Select]
Foo = class()
function Foo:init(xx, yy, zz)
  self.x = xx
  self.y = yy
  self.z = zz
end
« Last Edit: August 05, 2013, 01:57:17 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 #189 on: August 05, 2013, 10:32:46 am »
I'm probably just thinking things differently to how others might have got the concept in Lua.
I'm not familiar with the word scope (though I searched it up and that helped)
Spoiler For from lua-users.org:
The place where a variable is visible is called the "scope" of a variable.
But using local variables isn't exactly new for me.

The main thing that was stumping me the most was trying to understand what all this syntax that I wasn't familiar with, was doing.
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 njaddison

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 176
  • Rating: +24/-27
    • View Profile
Re: Lua Q&A
« Reply #190 on: February 15, 2014, 12:20:15 am »
wheneva i lookz at someone'z lua source code it haz a bunch of trigz and I'z juz like omgz what the crudz doez all this meanz it'z so weirdz
No spamming/trolling intended, I just want to know why you'd wanna use trig in a lua code. I'm not very skilled at programming (YET). Someday, I wanna be the very best. That no one ever was. But anyways, no spamming/trolling intended.
<a href="http://www.nerdtests.com/ft_nt2.php">
<img src="http://www.nerdtests.com/images/badge/nt2/5f42ec78e054645d.png" alt="NerdTests.com says I'm a Highly Dorky Nerd God.  Click here to take the Nerd Test, get geeky images and jokes, and talk to others on the nerd forum!">
</a>


Offline LDStudios

  • Coder Of Tomorrow
  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 388
  • Rating: +41/-1
    • View Profile
    • LD Studios
Re: Lua Q&A
« Reply #191 on: February 15, 2014, 07:17:39 am »
Well, I'm assuming you're talking about trigonometry?
I once started (never finished) a game in which the player moves with 2 arrows, controlling a line, that curves, sort of like snake, but not tied to a grid. I used sine and cosine to move the snakes destination point around a circle when the arrows were pressed. Thats one example.



Offline njaddison

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 176
  • Rating: +24/-27
    • View Profile
Re: Lua Q&A
« Reply #192 on: February 15, 2014, 09:15:16 am »
Yes I'm guessing that trigonometry is essential if I want to learn to program games. I learned basic trig in Geometry, how can I apply that to programming? It's something I've been wondering for a very long time, as i want to program my first game in lua, on the nspire.
EDIT:
Not actually ON the nspire, just for it.
« Last Edit: February 15, 2014, 09:15:55 am by njaddison »
<a href="http://www.nerdtests.com/ft_nt2.php">
<img src="http://www.nerdtests.com/images/badge/nt2/5f42ec78e054645d.png" alt="NerdTests.com says I'm a Highly Dorky Nerd God.  Click here to take the Nerd Test, get geeky images and jokes, and talk to others on the nerd forum!">
</a>


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: Lua Q&A
« Reply #193 on: February 15, 2014, 10:12:40 am »
Isn't some trig necessary for 3D too?

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Lua Q&A
« Reply #194 on: February 15, 2014, 10:59:53 am »
Trig is necessary for everything that involves rotation or turning.
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s