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

Yeah ... I'm Totally Lost In My Mind (fs.open(), Write Table And Cie ...)

Started by Neekow, 13 November 2013 - 05:33 PM
Neekow #1
Posted 13 November 2013 - 06:33 PM
Hi every one.

Before, anything, i'm french, so my english isn't perfect (really), so i hope you'll understand what i'll say … (as i'm not sure to understand myself … will be hard to explain).


ATM, i'm trying to do a really complete printer program (GUI, Saves/Load, pre-done page and maybe colors …), but i really miss a huge part of lua (especially in syntax, and a bit when i've to imagine what the result of an completly "temporary" table).

So what are my problems now:
(what i want)
  • to do saves, i think that the best way is to create a file with the title as file's name:
  • you have: "i love myself" in title, on your computer you will have save/i love myself and in this file will be also write: your text, the page you used, the page's limit
  • have in my mind something like:
  • Current_text = {{xPos,yPos,letter,colorCode},{xPos+1,yPos,letter,colorCode},{xPos+2,yPos,letter,colorCode}{xPos+3,yPos,letter,colorCode},…} in the text's file, why: - by this way, i'll should be able to avoid any bug like 2 letters in the same place (what will be anoying when you'll print …) - easy to rewrite on screen (and see imediatly any change you do) - highly easy print (as term.cursorPos(1,1) = print.cursorPos(1,2)
(where i'm stucked, got the ideas, think parts of differents functions …)
Note: these are my own handwrited notes, didn't tryed on CC because i know that nothing is right, i'll comment on what it's mean for me
Spoiler

event, Key = os.pullEvent("key")			 <-- this event is in a parallel.waitForAny
	LxPos, LyPos = term.getCursorPos()								
	RLettre = fs.open(saves.."/"..tostring(ficherLettre),"r")		  <-- read your current save file
	WLettre = fs.open(saves.."/"..tostring(ficherLettre),"w")   <-- ready to write
	dLxPos, dLyPos = LxPos, LyPos			  <-- LxPos = 1st Pos get, prefix "L" for "Lettre", "dL" for debugLettre
	  debugLettre()				 <-- [1]
	WLettre.write([LxPos,LyPos,Key,color])	<-- write the lettre with coordinates and color code on your file
	Rlettre.read(saves/tostring(ficherLettre))   <--[[
	  for i, lettre, in ipairs(fichierLettre) do									  
		term.setCursorPos(lettre[1],lettre[2])	 read your file and rewrite it on your screen imediatly.
		term.setTextColor(lettre[4])												
		term.write(lettre[3])															  
	  end			]]--
	term.setCursorPos(LxPos+1,LyPos+1)							   <-- set the cursor on the next position, like for real
	Rlettre.close()																	 <-- close file
	Wlettre.close()
  
-- [[ [1]
When i was think on the program, was annoyed by the idea on: "if you write a new letter where there's already one, it will erase it, or worse, you'll get both ...". So i imagined
a function where if you write a lettre where there's already one, it will decal it until every letters got their own place. I know that a huge part of this function is really bad writted ... these are just my notes to keep ideas. ]]

  function debugLettre()
	if string.match([dLxPos,dLyPos,...,..]) == true then		   <-- the aim of this line is to verrify if an "old" letter is in the same place of the "new" wanted one, if true it does:			
	  decalLettre = {string.match...}										 <-- take the "old" letter
	  dLxPos = dLxPos + 1													  <-- modify its position (yeah, i'll add the same thing for yPos later ;)/>/>/>/>/>/>)
	  WLettre.write([dLxPos, dLyPos, ..., ..])									 <-- write the "old" letter in its new position (virtual) in the [b][u]same[/u][/b] place in the file
	end
	if string.match([dLxPos, dLyPos, ..., ..], <---) then			 <-- check if the "new old" letter doesn't attempt to write over another letter by checking from the end of file [2]
	  debugLettre(dLxPos, dLyPos)										 <-- if true ... do debugLettre() again, until (as said before) each letter a its own place
	end
  end

– [[ [2]
check from the end of file and rewrite the "new position" in the same place are very important (in fact, almost all of this functions are based of this) ]]

anyway … i'm on that actually, and i really dunno where i should go to continue ^^ so if ever some one can post links where it's explain or take the time to explain or have an idea … thanks =)
Yevano #2
Posted 13 November 2013 - 07:01 PM
You made a pretty big post without really asking a question. So, what exactly do you need help with? Do you need to know how to do file IO, or is it the GUI you're having trouble with? If you haven't tested anything in-game or in an emulator yet then I suggest that you do that first and experiment.
CCJJSax #3
Posted 13 November 2013 - 10:08 PM
Yeah, I'll be honest I didn't get too far into that topic until my ADD kicked in. If you're trying to figure out how to write to a file, this worked perfectly for me and it was rather easy to understand.

Go to the 14th post in this link that I'll give you. It's made by a guy named MysticT. http://www.computercraft.info/forums2/index.php?/topic/2886-luahow-to-readwrite-certain-lines-in-files/