86 posts
Posted 23 July 2015 - 03:40 PM
can anyone think why a function would yield when called or what part of this
local function writeFileBackup() --write all the variables to file in case of unexpected shutdown
--dead before it gets here
locationFile = io.open("location","w") --open a new empty file, overwrite the previous file
locationFile.write(deltaXNow,"\n",deltaYNow,"\n",deltaZNow,"\n",orientation,"\n",turtleID,"\n",yHome,"\n",miningStatus) --write all the variables
io.close(locationFile)
end
would yield? it's not yielding the coroutine is dead when it runs writeFileBackup() before it ever does anything.
Calling writeFileFunction outside the coroutine in the main body causes no issuesIssue 1 solved, declare function before coroutine
Issue 2 now, the write function only writes the first variable, i have a feeling it's because the file is opened as "w" but i'm not sure why.
Edited on 23 July 2015 - 02:57 PM
3057 posts
Location
United States of America
Posted 23 July 2015 - 03:45 PM
None of that could possibly yield, unless somebody did voodoo magic and overwrote io.open.
86 posts
Posted 23 July 2015 - 04:09 PM
I declared the function after i declared the coroutine. I need to get the hang of this in lua.
1140 posts
Location
Kaunas, Lithuania
Posted 23 July 2015 - 04:15 PM
Also, it should be locationFile:write, not locationFile.write. And, you can simply do this to maintain the same code structure: 'locationFile:close()'.
86 posts
Posted 23 July 2015 - 04:26 PM
Thanks, had just realised that my file was getting destroyed. I like that noone has noticed all the slashes are the wrong way round :P/>
Edited on 23 July 2015 - 02:27 PM
1583 posts
Location
Germany
Posted 23 July 2015 - 05:44 PM
Not sure if what I say is right, but I think io.write takes one argument which is a string, and not multiple "sub-strings".
86 posts
Posted 23 July 2015 - 05:47 PM
Can someone please tell me why
file:write("string1","\n","string2")
results in a file that just has 1 line, string1
I'm getting a headache.
this is an example from the web. I'm not sure does this work?
io.write("sin (3) = ", math.sin(3), "\n")
I know i'm using this wrong.
Edited on 23 July 2015 - 03:50 PM
86 posts
Posted 23 July 2015 - 05:47 PM
Check my latest thread, thanks hax
1583 posts
Location
Germany
Posted 23 July 2015 - 06:20 PM
I tested it right now. It doesn't work in CC, but in "normal" lua. You will have to concatenate everything, so it works.
This probably is because the io API in CC is not "native", it is built Lua-side on top of the fs API.
local file = io.open("path/to/file","w")
file:write("sin (3) = "..math.sin(3).."\n")
file:close()
86 posts
Posted 24 July 2015 - 01:57 PM
AH thankyou :)/>
86 posts
Posted 24 July 2015 - 02:22 PM
Can someone update
http://www.computercraft.info/wiki/IO_%28API%29 to say that io.write will only take one argument.