This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Moody's profile picture

[Lua][Error]Loadstring Error

Started by Moody, 08 February 2013 - 02:31 PM
Moody #1
Posted 08 February 2013 - 03:31 PM
Hi
i tried to make my own adjustible menuprogram. it should basically be easy to add new menus and so on.
Therefore i ask the User in a Wizard about what function he wants to call and save that in a table.
That table i save into a file for later use.
To call the function i try to use "loadstring" but in almost every case it fails with
string:1: attempt to index ? (a nil value)
Sometimes it works though, alas it seems to me pretty random.
i also tried to use _G[string]() but i dont really get how itÄ's supposed to work :/
The complete code can be found here http://pastebin.com/HA1kPFgb
Also the error most certainly occurs in line 211/212
i also tried storing the string into a file and running it by calling it in shell.run, but it basically produces the same error, although i can run the file from the shell and the lua commandline, which utterly confuses me :/
i really have no idea why that is, so i hope for a good answer
Thanks
Lyqyd #2
Posted 09 February 2013 - 04:48 AM
Split into new topic.
LBPHacker #3
Posted 09 February 2013 - 05:32 AM
I don't get it - It works perfectly for me. I've created a menu called "MyMenu" with an entry called "Shutdown", then typed "os.shutdown()". And it works.
theoriginalbit #4
Posted 09 February 2013 - 05:45 AM
onItemS needs to be declare red BEFORE it is used in onKeyPressed
LBPHacker #5
Posted 09 February 2013 - 06:06 AM
onItemS needs to be declare red BEFORE it is used in onKeyPressed

This is an API, it doesn't matter where the declarations are.
Lyqyd #6
Posted 09 February 2013 - 08:02 AM
onItemS needs to be declare red BEFORE it is used in onKeyPressed

This is an API, it doesn't matter where the declarations are.

That is incorrect. The fact that it is an API does not magically change how loadstring() works when loading the API. If you have functions in the wrong order, they're in the wrong order.
LBPHacker #7
Posted 09 February 2013 - 08:26 AM
I mean after an os.loadAPI, everything works fine.

BTW This also works, but I've declared print1 after declaring print2.



function print2(x)
    print1(x)
end

function print1(x)
    print(x)
end

print2("Hello World")
Moody #8
Posted 09 February 2013 - 03:29 PM
Hey thank you all for your feedback!
I tried to execute the code on my other PC and it worked perfectly fine for me as soon as i stopped using shell.run and used os.run
(i guess os.loadAPI doesnt inherit access to shell instructions so it cant pass it onto an api? - but well, i dont have any idea of CC's inner mechanics)
strangely enough it just randomly doesnt work on my laptop - i have no idea what that is, But well, if it works for you guys, its at least no problem with the code im missing :)/>

Again thank you all for your effords