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

Loadstring causing problems.

Started by Creator, 18 March 2015 - 06:22 PM
Creator #1
Posted 18 March 2015 - 07:22 PM
Hi guys,

in parrallel to my OS, I am also working on my other project, that will be the special feature of the OS, called Aware. For some reason, when I type "hi", it throws this error:


string:1: Attempt to call nil

Here is the code:


--[[
The Aware Project
by Creator
for TheOS
]]--
--[[
First I am doing the mechanics of the program, afterwards I will do the GUI
]]--
--GUI
--if _G.Interact == nil then os.loadAPI("TheOS/API/Interact") end
--local gui = Interact.Initialize()
--Variables--
local input = {}
local Commands = {}
local path = ""
--Functions--

function split(str,sep)
local buffer = {}
for token in str:gmatch(sep) do
  buffer[#buffer+1] = token
end
return buffer
end
function getData()
local var = http.get("https://raw.githubusercontent.com/TheOnlyCreator/Aware/master/Test/Test")
print(var.readAll())
os.pullEvent()
error()
end
local function readFile(_path)
local file = fs.open(_path,"r")
local data = file.readAll()
file.close()
return data
end
function filter()

end
function redirect(word)
print("Your word is "..word)
oneWordCommand(word)
end
function respond(arg)
print(_G[arg][math.random(1,#_G[arg])])
end
function oneWordCommand(word)
if Commands[word] ~= nil then
  local func = loadstring(Commands[word])
  func()
else
  local firstLetter = string.sub(word,1,1)
  print(firstLetter)
  local fromWeb = http.get("https://raw.githubusercontent.com/TheOnlyCreator/Aware/master/Actions/"..firstLetter)
  local buffer = fromWeb.readAll()
  local tableFromWeb = textutils.unserialize(buffer)
  print(tableFromWeb[word])
  local func = loadstring(tableFromWeb[word])
  func()
end
end
function main()
print("I am listening...")
local buffer = read()
input = split(buffer,"[^%s]+")
for i,v in pairs(input) do
  input[i] = string.lower(v)
end
for i,v in pairs(input) do
  print(v)
end
if #input == 1 then
  oneWordCommand(input[1])
else

end
end

--Code--
local buffer = readFile(path.."Commands")
Commands = textutils.unserialize(buffer)
term.setCursorPos(1,1)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
while true do
main()
end

Here is the add on file


{
shutdown = "os.shutdown()",
reboot = "os.reboot()",
time = "print(\"The time is \"..textutils.formatTime(os.time()))",
hi = "redirect(\"Greetings\")",
hello = "print(\"Greetings\")",
exit = "error(\"Thanks foor using this thingy thingy\")"
}

You can get the code from pastebin by typing


pastebin get sxpNrC0j Aware
pastebin get hUqRY9uK Commands

I hope you can help me and thank you

~Creator
Edited on 18 March 2015 - 06:22 PM
SquidDev #2
Posted 18 March 2015 - 07:47 PM
loadstring loads into the global table, shell creates its own environment for all functions. You need to to setfenv(func, getfenv()) to make sure it works.
Creator #3
Posted 18 March 2015 - 07:55 PM
loadstring loads into the global table, shell creates its own environment for all functions. You need to to setfenv(func, getfenv()) to make sure it works.

Wow, thanks. It works. IT WORKS. I don't know why, but it works.

You get a +1