Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: blauemauritius on April 16, 2013, 02:48:34 pm

Title: loadstring - MyLib
Post by: blauemauritius 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.

Title: Re: loadstring - MyLib
Post by: Adriweb 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

Title: Re: loadstring - MyLib
Post by: blauemauritius 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?
Title: Re: loadstring - MyLib
Post by: Adriweb 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.
Title: Re: loadstring - MyLib
Post by: blauemauritius 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.
Title: Re: loadstring - MyLib
Post by: Levak on April 17, 2013, 04:23:58 am
If you want to do like Make3D (http://levak.free.fr/ftp/nspire/Make3D/Makefile.sh) - 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)
Title: Re: loadstring - MyLib
Post by: blauemauritius 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?

Title: Re: loadstring - MyLib
Post by: Levak 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.
Title: Re: loadstring - MyLib
Post by: blauemauritius on April 17, 2013, 01:46:32 pm
Thank you very much. Have a nice evening!