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

0 Members and 1 Guest are viewing this topic.

Offline NecroBumpist

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 130
  • Rating: +14/-5
  • Master of Lua
    • View Profile
Re: Lua Q&A
« Reply #60 on: August 19, 2011, 12:05:21 am »
I'm actually not too sure that its your fault, as usually, the program doesn't crash the calulator, it just tells you about the syntax error.

Oh I'm pretty sure it was my fault.
While doing some ... exploring, I found a __gc() function in the 'val' table, and called that with the table as an arguments, and then my calculator crashed :)
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 3rik

  • LV3 Member (Next: 100)
  • ***
  • Posts: 92
  • Rating: +8/-0
  • My TI-84+ SE
    • View Profile
Re: Lua Q&A
« Reply #61 on: August 19, 2011, 01:10:13 am »
I was doing a similar thing just a few days ago. I have a hunch it is related to collectgarbage.  (http://www.lua.org/manual/5.1/manual.html Section 2.10.1)

It's probably a meta method, and is probably not ment to be used in normal scripts :)

I agree with jimbauwens, it isn't meant to be used in the script itself.
Userbars

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Lua Q&A
« Reply #62 on: August 23, 2011, 11:21:42 am »
How do I clear a string when a certain key is pressed?

I have so far:

Code: [Select]
function on.deleteKey()
    msg = string.char()
    --also used msg = ""
end

But it's not clearing the string or it's not being called.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

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 #63 on: August 23, 2011, 11:23:34 am »
You need to use platform.window:invalidate() .
Just put it in the end of the function :)

Edit:
Like this:
Code: (Lua) [Select]
msg = ""

function on.paint(gc)
  gc:drawString(msg, 10, 10, "top")
end

function on.char(ch)
  msg = ch
  platform.window:invalidate()
end

This will show the pressed button
« Last Edit: August 23, 2011, 11:27:59 am by jimbauwens »

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Lua Q&A
« Reply #64 on: August 23, 2011, 11:26:17 am »
Didn't work. It's as though it's not calling the function in the first place.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

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 #65 on: August 23, 2011, 11:28:44 am »
See my edited post above, shows a little demo :)

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #66 on: August 23, 2011, 11:30:18 am »
While doing some ... exploring, I found a __gc() function in the 'val' table, and called that with the table as an arguments, and then my calculator crashed :)
Yep, I wrote that on HackSpire some time ago when we didn't have inspired-lua yet.



and for HOMER-16 :

Code: [Select]
msg = ""

function on.paint(gc)
  gc:drawString(msg, 10, 10, "top")
end

function on.char(ch)
  msg = ch
  platform.window:invalidate()
end

function on.enterKey()
  msg = ""
  platform.window:invalidate()
end

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Lua Q&A
« Reply #67 on: August 23, 2011, 11:37:02 am »
Thanks, it seems as though deleteKey doesn't work. enterKey did though. :D

Is there a way to just delete the last character in a string?

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Lua Q&A
« Reply #68 on: August 23, 2011, 12:04:33 pm »
Code: [Select]
function on.backspaceKey()
if self.var:len() > 0 then
self.var = string.sub(self.var, 1, self.var:len()-1)
end
platform.window:invalidate()
end

function on.clearKey()
self.var = ""
platform.window:invalidate()
end
« Last Edit: August 23, 2011, 12:05:48 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 Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Lua Q&A
« Reply #69 on: August 23, 2011, 12:13:44 pm »
Thanks Levak. I changed it to
Code: [Select]
function on.backspaceKey()
    if string.len(msg) > 0 then
        msg = string.sub(msg, 1, string.len(msg)-1)
    end
    platform.window:invalidate()
end

function on.clearKey()
    msg = ""
    platform.window:invalidate()
end

And it works perfectly. :D

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Lua Q&A
« Reply #70 on: August 23, 2011, 12:40:21 pm »
Make your code slightly more simple by replacing string.len(msg) with #msg :)
Ndless.me with the finest TI-Nspire programs

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua Q&A
« Reply #71 on: August 23, 2011, 12:48:57 pm »
In that case, yep, the # is a good idea, but watch out with tables ! (for key/value tables the count is not what we expect)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Loulou 54

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 165
  • Rating: +39/-0
    • View Profile
    • Mes programmes sur TI bank.
Re: Lua Q&A
« Reply #72 on: August 23, 2011, 03:49:32 pm »
Thanks Levak. I changed it to
Code: [Select]
function on.backspaceKey()
    if string.len(msg) > 0 then
        msg = string.sub(msg, 1, string.len(msg)-1)
    end
    platform.window:invalidate()
end

function on.clearKey()
    msg = ""
    platform.window:invalidate()
end

And it works perfectly. :D

Oh yes I've already seen this and I wanted to ask some explanations but I forgot ^^ :
The event on.deleteKey seems not working.. The event on.backspaceKey is the one who's called instead, when pressing delete key. Is it normal ?

Thanks !
Some of my program available here.. :)
http://ti.bank.free.fr/index.php?mod=archives&ac=voir2&id=1471

     

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Lua Q&A
« Reply #73 on: August 23, 2011, 09:40:42 pm »
Is there a way in lua to create an object and create multiple instances of it? I've read that there's no class feature so I'm wondering if there's a way to do this.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Lua Q&A
« Reply #74 on: August 24, 2011, 12:04:35 am »
Here's something I just thought of that should recursively duplicate any table (objects are represented by tables, right?) By recursive, I mean that any element that is a table will be fully duplicated as well.
Code: [Select]
function duplicate(obj)
if type(obj) ~= "table" then
return obj
else
local t = {}
for key,value in pairs(obj) do
t[key] = duplicate(value)
end
return t
end
end

Edit: Added missing then
« Last Edit: August 24, 2011, 12:05:22 am by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman