Author Topic: loadstring - MyLib  (Read 5891 times)

0 Members and 1 Guest are viewing this topic.

Offline blauemauritius

  • LV2 Member (Next: 40)
  • **
  • Posts: 25
  • Rating: +0/-0
    • View Profile
loadstring - MyLib
« on: April 16, 2013, 02:48:34 pm »
Hello,

i have one question regarding the function loadstring. How could i use the function to load a file from the MyLib folder on my TI-nspire?

Thank you very much for your help.


Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: loadstring - MyLib
« Reply #1 on: April 16, 2013, 02:59:46 pm »
Like this :)

Code: [Select]
myVar = math.eval("externalDocumentInMyLib\\thePublicVariableYouWant")

More advanced use :
https://github.com/adriweb/EEPro-for-Nspire/blob/master/EEPro.big.lua#L1519

But there, I'm actually calling a function and taking its return value :P

My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline blauemauritius

  • LV2 Member (Next: 40)
  • **
  • Posts: 25
  • Rating: +0/-0
    • View Profile
Re: loadstring - MyLib
« Reply #2 on: April 16, 2013, 03:04:20 pm »
Thank you. That´s what i was looking for.

Which kind of variables can i use? Can I use lists?

Which format should have the file?

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: loadstring - MyLib
« Reply #3 on: April 16, 2013, 05:28:27 pm »
You can use any type you want as long as it's lua compatible (through the math.eval)
It has to be a math variable inside a normal .tns document you created as usual.
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline blauemauritius

  • LV2 Member (Next: 40)
  • **
  • Posts: 25
  • Rating: +0/-0
    • View Profile
Re: loadstring - MyLib
« Reply #4 on: April 17, 2013, 01:57:57 am »
Thank you very much. Do you have an example? I have read the Code in your file, but i don't understand thim?

I couldn't image how does it work.

For example i have the file test.txt in the folder Mylib. In the file i have the variable Zahl=1234.

What are the steps what i have to do?

I would be grateful, if you can help me.

Thank you very much.

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: loadstring - MyLib
« Reply #5 on: April 17, 2013, 04:23:58 am »
If you want to do like Make3D - that is, loading the entire source code from MyLib - you can use loadstring(var.restore("my_file\\my_var"))()
In my_var of the TI-Nspire document my_file.tns, you have a string (be careful on its size tho, there is a limit, like 16K chars or so) contaning your source code.

For example :
In my_file.tns (normal document, inside MyLib - don't forget to refresh libraries)
Code: [Select]
my_var := "function on.pain(gc) gc:drawString('Hello world', 0, 0, 'top') end"
In my_example.tns (Lua document)
Code: [Select]
function on.restore()
   _, err = pcall(function() loadstring(var.recall("my_file\\my_var"))() end)
  print err
end
This should display "Hello world" (haven't tested yet but you should understand the logic)
« Last Edit: April 17, 2013, 04:51:25 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 blauemauritius

  • LV2 Member (Next: 40)
  • **
  • Posts: 25
  • Rating: +0/-0
    • View Profile
Re: loadstring - MyLib
« Reply #6 on: April 17, 2013, 12:44:17 pm »
Thank you very much. This helps me. Do you know if i can use ".txt" or ".lua" files too?


Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: loadstring - MyLib
« Reply #7 on: April 17, 2013, 12:45:52 pm »
Do you know if i can use ".txt" or ".lua" files too?
No, you can't read file, basically. This is why such "hacks" have been introduced.
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline blauemauritius

  • LV2 Member (Next: 40)
  • **
  • Posts: 25
  • Rating: +0/-0
    • View Profile
Re: loadstring - MyLib
« Reply #8 on: April 17, 2013, 01:46:32 pm »
Thank you very much. Have a nice evening!