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

0 Members and 4 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 #75 on: August 24, 2011, 04:07:37 am »
In Lua, classes are tables, tables of tables etc ..

The Lua classes tutorial is not yet translated on Inspired-Lua, you can try a google translate, but some people says they didn't understand it =/
http://www.inspired-lua.org/2011/05/5-object-classes/

I'm going to sum up

If toto is a class and x,y numbers of this class, f1, f2 functions of this same class, and tab a table also in this class.

Then :
Code: [Select]
toto = {
 x=1,
 y=2,
 f1=function() return true end,
 f2=function() return false end,
 tab={
 .....
 }
}

Then you can access to toto.x, toto.y, etc ...
But this is a fixed way to do "classes".

The TI-Nspire Lua framework contains a function that is not part of Lua 5.1 : class()

This function avoids typing multiple lines that many of us won't understand (even if it is quite simple)

With the same example :

Code: [Select]
toto = class()

function toto:init()
 self.x, self.y = 1, 2
 self.f1 = function() return true end
 self.f2 = function() return false end
 self.tab = {...}
end

init() function is required by class(). When you want to create a new toto object, you will do like this : a = toto()
You can give to init() arguments to create a specified object like this :


function toto:init(x0, y0)
 self.x, self.y = x0, y0
 self.f1 = function() return true end
 self.f2 = function() return false end
 self.tab = {...}
end

Then, you will be able to do : a = toto(1,2) and access to a.x and a:f1

Remember another thing in Lua : the difference between . and : between the object and its element :

a.x access to x element in object a
a.f1() access to f1 function in object a
a:f1() access to f1 function in object a and gives to f1 function a as first argument, that way you can use 'self' in its définition.

a.f1(a) == a:f1()
function a.f1(parent) return parent.x end == function a:f1() return self.x end
« Last Edit: August 24, 2011, 04:12:16 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 insertcoolnamehere

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #76 on: August 28, 2011, 08:23:19 pm »
Simple question: Why doesn't this work?

Code: [Select]
function creategamedata()
    screen = "loading"
    local t1, t2
    for t1 = 1, gamelen, 1 do
        for t2 = 1, t1, 1 do
            gamedata[t1][t2] = math.random(1,4)
        end
    end
    game()
end

This is the error:



What it is supposed to do is create a table:

{{1}, {1,2}, {1,2,3}, ....} (the numbers are randomized, those were examples)

I have defined 'gamedata' somewhere else like so:

Code: [Select]
gamedata = {}
If you want the whole code for the game (simon), then just ask. I don't want to post it unnessecarily

EDIT: Line 19 is this one:
Code: [Select]
           gamedata[t1][t2] = math.random(1,4)
« Last Edit: August 28, 2011, 08:25:04 pm by insertcoolnamehere »
insertcoolsignaturehere

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Lua Q&A
« Reply #77 on: August 28, 2011, 08:30:53 pm »
gamedata's value for gamedata[t1] is nil. You are trying to access it like a table. You would either have to put gamedata[t1]={} or table.insert(gamedata, {}) between the fors.
« Last Edit: August 28, 2011, 08:34:54 pm by 3rik »
Userbars

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Lua Q&A
« Reply #78 on: August 28, 2011, 08:30:55 pm »
Simple question: Why doesn't this work?

The simplest questions deserve the simplest answers.
You're attempting to write to a table that doesn't exist.

Assuming gamedata is just an empty table, this code should work:

Oh yeah, I removed some redundancies.
Code: [Select]
function creategamedata()
    screen = "loading"
    local rand = math.random;

    for t1 = 1, gamelen do
        local v = {};
        gamedata[t1] = v;

        for t2 = 1, t1 do
            v[t2] = rand(1,4)
        end
    end

    game()
end
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline insertcoolnamehere

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #79 on: August 28, 2011, 08:34:39 pm »
Thanks guys! And Necro, your code works perfectly! Today's my first day on lua so kinda confused :)
insertcoolsignaturehere

Offline insertcoolnamehere

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #80 on: August 28, 2011, 11:15:29 pm »
Another question:

How do I get Lua to do something, wait (say, 1 second), and do something else? I want to do this repeatedly and what I saw on the Inspired-Lua wiki didn't please me. (Was a little complicated and would be hard to duplicate)

http://wiki.inspired-lua.org/timer.getMilliSecCounter


Anyone got any suggestions?
« Last Edit: August 28, 2011, 11:17:18 pm by insertcoolnamehere »
insertcoolsignaturehere

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Lua Q&A
« Reply #81 on: August 28, 2011, 11:25:53 pm »
You could use timers.

Code: [Select]
-- since I don't know if you can pass the on.timer() function endless arguments, we'll store them in a local

local timerFunction;

function on.timer()
    timer.stop();
    assert(timerFunction)();
end

local function wait(s, f)
    timerFunction = f;
    timer.start(s);
end

wait(1, function()
    -- whatever you want to do in 1 second
end)
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #82 on: August 28, 2011, 11:44:15 pm »
Don't forget to stop the timer with timer.stop() if you don't want to repeat it each second =)
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Lua Q&A
« Reply #83 on: August 28, 2011, 11:50:46 pm »
Well after using timer.start(1) the function on.timer will be called every second. So whatever you define on.timer to do will happen every second until you use timer.stop(). You can change how frequently  on.timer is called by changing the argument. So timer.start(0.5) will call on.timer every 0.5 seconds. Make sure not to put so much stuff in on.timer that it doesn't keep time properly.
For example, the following code is for a simple stopwatch. Press enter to start and reset it.

Code: [Select]
Seconds, Mode = 0, "Not Timing" --[[Sets default values to Seconds and Mode]]
function on.enterKey() --[[This is called when ever the enter key is pressed]]
if Mode == "Not Timing" then --[[If the mode is "Not Timing" when enter is pushed this restets the Seconds counter, switches the mode to "Timing" and starts the timer (counting at 0.1 seconds)]]
Seconds = 0
Mode = "Timing"
timer.start(0.1)
else --[[If the mode wasn't "Not Timing" switch it to "Not Timing" and stop calling on.timer]]
Mode = "Not Timing"
timer.stop()
end
end
function on.paint(gc) --[[This is called every time the calculator needs to refresh the screen]]
gc:drawString(tostring(Seconds), 0, 0, "top") --[[This displays what the value of the Seconds Counter is]]
end
function on.timer() --[[When Mode is "Timing" this will be called every 0.1 seconds]]
Seconds = Seconds + 0.1 --[[Increases the number of seconds counted]]
platform.window:invalidate() --[[Forces the screen to refresh and call on.paint so the number of seconds gets updated on the screen]]
end

Ninj'd
Apparently people typed their responses before I was done. Oh well. I'll post it anyway.
Userbars

Offline insertcoolnamehere

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +0/-0
    • View Profile
Re: Lua Q&A
« Reply #84 on: August 29, 2011, 09:11:46 am »
Hmm... so a timer invokes the on.timer() event every time it fires until timer.stop()? Haven't seen that before! :)
insertcoolsignaturehere

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #85 on: August 29, 2011, 09:21:01 am »
yep.

It's useful that way for games, for example, that require constant screen refreshes :)
« Last Edit: August 29, 2011, 09:21:09 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Lua Q&A
« Reply #86 on: August 29, 2011, 10:22:37 am »
The TI-Nspire event-driven API makes sure the Lua programs work well in the multi-task environment of the TI-Nspire, so you won't see any blocking function such as sleep(). This may be a bit disturbing but you should soon get used to it. We can help you with this if you give more details on why you want your program to wait.
Ndless.me with the finest TI-Nspire programs

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 #87 on: August 29, 2011, 03:59:42 pm »
Talking about sleep(), its relatively easy to implant by mixing a coroutine and the timer. sleep can be kinda usefull for people making a text adventure :)

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Q&A
« Reply #88 on: August 29, 2011, 06:02:28 pm »
How do we get the cursor to show up on the screen? I tried cursor.show(), but it only displayed the cursor for about half a second, then vanished.

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Lua Q&A
« Reply #89 on: August 29, 2011, 06:34:43 pm »
pianoman, have you tried adding cursor.show() to on.draw() ?
Developing Lua scripts for the NSpire ?
Check out the Necrotorium
Need a few routines to run faster ? Checkout the MODS Lua Assembly Toolkit.
Need to save space for your scripts ? Checkout LuaSrcDiet