EDIT: Ok, now again. This time I will provide all the information so that there is no confusion.
Just repeating my last post
Error message: "Unknown chunk:3: attempt to call nil"
Lupus' script:
--This is at the very top of my program
local HOST_ENV = _ENV or getfenv() -- this is global
local OUR_ENV = {}
setmetatable(OUR_ENV, {__index = HOST_ENV, OUR_ENV = OUR_ENV})
setfenv(1, OUR_ENV) -- make the rest of this code use this environment
--This is below function insert() and function dig()
local function compile(chunk)
if type(chunk) ~= "string" then
error("expected string, got ".. type(chunk), 2)
end
local function findChunkName(var)
for k,v in pairs(HOST_ENV) do
if v==var then
return k
end
end
return "Unknown chunk"
end
return load(chunk, findChunkName(chunk), "t", OUR_ENV) --interesting part
end
The loop that is running:
while #eventList > 0 do
compile(eventList[1])() --this is the interesting bit
table.remove(eventList, 1)
index = 2
save("database/OCM/resume/eventList", eventList) --irrelevant
end
I'm scratching my head over how you're not getting an unfinished string error - your use of linebreaks should, well, break them. And what's with the backslashes? What are you intending to escape, exactly?
The way I'd typically define a multi-line string is:
{
[[while not turtle.forward() do --notice that I put a block in front of the turtle so that dig() is called
if turtle.getFuelLevel == 0 then
Fuel()
elseif dig() == false then --dig() is called
turtle.attack()
end
end]]
I didn't provide the full string to simplify it, turns out that was a bad idea. The full string (referred to as "Unknown chunk" in the error message):
Spoiler
[[if level == 0 then
if variables.floor == true then
insert('placeFloor()') <---------------------line 3 throws the error (if I comment it out, line 5 throws the error and so on, meaning insert() can't be reached by compile() even though the environment is set correctly)
end
insert('digUp()')
if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 then
insert('CompareAll(Compare, CompareBack, CompareUp)')
else
table.insert(toDoList, CompareDown)
end
insert('search()')
insert('FreewayUp()')
if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 then
insert('CompareAll(Compare, CompareBack, CompareDown)')
else
table.insert(toDoList, CompareUp)
end
insert('search()')
elseif level == 1 then
insert('digDown()')
if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 then
insert('CompareAll(Compare, CompareBack, CompareDown)')
else
table.insert(toDoList, CompareUp)
end
insert('search()')
if i == 2 and variables.mainTorches then
insert('searching = false')
insert('dig(true)')
end
insert('FreewayDown()')
if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 then
insert('CompareAll(Compare, CompareBack, CompareUp)')
else
table.insert(toDoList, CompareDown)
end
insert('search()')
if variables.floor == true then
insert('placeFloor()')
end
end]]
line 3 throws the error (if I comment it out, line 5 throws the error and so on, meaning insert() can't be reached by compile() even though the environment is set correctly)Again, function insert():
function insert(code)
table.insert(eventList, index, code)
index = index + 1
save("database/OCM/resume/eventList", eventList)
end
save-function, if interested (irrelevant)
function save(path, content)
local file = fs.open(path, "w")
if type(content) == "table" then
file.writeLine(textutils.serialize(content))
else
file.writeLine(content)
end
file.close()
end
This is also where the backslashes come from, apparently they get written to the extern file because of the serialization. However that is irrelevant for now since that extern file isn't used anywhere else in the program yet.Note that this still won't call dig() if the turtle is out of fuel, unless Fuel() successfully rectifies that for later iterations!
I don't really understand what you mean, this code works exactly like it's intended to.
[[while not turtle.forward() do
if turtle.getFuelLevel == 0 then
Fuel()
elseif dig() == false then
turtle.attack()
end
end]]
When called the turtle tries to move forward. If it has no fuel left, Fuel() is called and it tries again. If there is a block in the way it destroys it and tries again. If there is a mob it attacks it and tries again.
The point of this snippet only was to show that the calling of other functions like Fuel(), dig(), turtle.forward() and turtle.attack() works. Only insert() is making problems.
Why is your loop setting index to 2? Should it not be lowering its old value by one instead?
This is a little bit complicated and not relevant but let me try to explain:
Spoiler
Insert() inserts a chunk of code into the toDoList. It uses index 2 and increasing because the inserted code needs to be called next. Index 1 however is the chunk of code that called insert() in the first place and needs to be removed after successfull execution. The index is set back to 2 now, so that insert() -if called- doesn't push away the chunk at index 1 which again needs to be removed.It'd be helpful to provide the full list of code, as I'm not sure you've provided enough information here to determine the cause of your reported error.
I really don't recommend looking into it, it's horribly messy and I've provided everything that's related but I guess I can't stop you:
https://pastebin.com/fptxKN9nI know this is a lot to read through and very difficult to wrap your head around but I really need the help :(/>