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

Function and yielding

Started by Balthamel, 23 July 2015 - 01:40 PM
Balthamel #1
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 issues


Issue 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
KingofGamesYami #2
Posted 23 July 2015 - 03:45 PM
None of that could possibly yield, unless somebody did voodoo magic and overwrote io.open.
Balthamel #3
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.
MKlegoman357 #4
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()'.
Balthamel #5
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
H4X0RZ #6
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".
Balthamel #7
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
Balthamel #8
Posted 23 July 2015 - 05:47 PM
Check my latest thread, thanks hax
H4X0RZ #9
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()
Balthamel #10
Posted 24 July 2015 - 01:57 PM
AH thankyou :)/>
Balthamel #11
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.