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

0 Members and 1 Guest are viewing this topic.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Lua Q&A
« on: June 21, 2011, 09:05:12 pm »
Hi everyone! I couldn't find a good place to post various questions, so I thought I'd start this.

First of all, how do you use the timer and make it do something? All I can do is start it. :w00t:

Thank you!
« Last Edit: July 19, 2011, 01:01:30 pm by pianoman »

Ashbad

  • Guest
Re: Lua Q&A
« Reply #1 on: June 21, 2011, 09:30:41 pm »
You can start the timer with Timer.start(period), which sets a period of time in seconds for the timer to start.  You can then set up an on.timer() event that is activated every timer tick. You can stop it with timer.stop().

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #2 on: June 22, 2011, 03:07:01 am »
Here is a little example:

Code: (Lua) [Select]

number = 0

function on.paint(gc)

    --If number is still zero (we just started the application), start the timer (set to tick every 1 seconds)
    if number==0 then
        timer.start(1)
    end

    --Draw the number on the screen
    gc:drawString(tonumber(number),10, 10, "top")
end


--This get's now called every 1 seconds until you stop the timer
function on.timer()
    --Add one to number
    number = number + 1

    --Force the screen to be redrawn
    platform.window:invalidate()

    --If number is 10, stop the timer
    if number==10 then
        timer.stop()
    end
end


Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #3 on: June 22, 2011, 11:00:08 pm »
Oh, got it. Thanks!

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #4 on: June 29, 2011, 04:06:41 pm »
Another one:
are you able to change values of an array in the program?
Thanks!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #5 on: June 29, 2011, 04:16:52 pm »
Well, technically speaking its tables in Lua :)

Lets say you have this table:
Code: (Lua) [Select]
a={1,2,3,4,5}Then you can change the third item with this command:
Code: (Lua) [Select]
a[3] = 1337Your table will then look like this:
Code: (Lua) [Select]
{1,2,1337,4,5}

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #6 on: June 29, 2011, 04:18:01 pm »
Omigod that just made a game a million times easier.
Thanks!

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #7 on: June 30, 2011, 10:20:21 am »
How about this one: are you able to check if two tables are equal to each other?
for example:
Code: [Select]
a={1,2,3}
b={1,2,4}

function on.paint(gc)
if a==b then
gc:drawString("E",0,0,"top")
else
gc:drawString("I",0,0,"top")
end
« Last Edit: June 30, 2011, 10:20:33 am by pianoman »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #8 on: June 30, 2011, 10:30:54 am »
Sadly enough you can't just compare a table like that (unless you add a metatable for it with the appropriate funcion).
You can do it like this:
Code: (Lua) [Select]

function compare(t1, t2)
    for i, p in pairs(t1) do
        if p~=t2[i] then
            return false
        end
    end
    return true
end

a={1,2,3}
b={1,2,3}

if compare(a, b) then
--they are equal
else
--they are not equal
end

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #9 on: June 30, 2011, 10:34:40 am »
What's "p" in that code?
And what does "in pairs" mean?

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #10 on: June 30, 2011, 11:05:41 am »
Its a way to loop through all the items of an Array.

Example: (works only on computer, as it uses print)
Code: (Lua) [Select]
    mytable={1,2,3,"a","b","c"}

    for i, p in pairs(mytable) do
       print("Spot: " . i)
       print("Content: " . p)
    end
will output
Code: (Lua) [Select]
Spot: 1
Content: 1
Spot: 2
Content: 2
Spot: 3
Content: 3
Spot: 4
Content: a
Spot: 5
Content: b
Spot: 6
Content: c

i is the spot in the table, p is the content of that spot.

Here is some handy information about tables: http://lua-users.org/wiki/TablesTutorial

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #11 on: June 30, 2011, 03:10:08 pm »
Oh, I get it. Thanks, jimbauwens!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Q&A
« Reply #12 on: June 30, 2011, 04:03:07 pm »
No problem :)

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #13 on: June 30, 2011, 04:29:31 pm »
Sorry, but I have another one:
is there a randint feature in lua?

EDIT: Never mind, I figured it out. :)
« Last Edit: June 30, 2011, 04:39:08 pm by pianoman »

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #14 on: July 01, 2011, 12:48:58 pm »
Is there a way to display what the user is typing (i.e. a text box)?
I have a feeling it uses getText, but I don't know how to use it, and the TI manual is really confusing.