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

Save states in little control program

Started by elminister, 08 January 2013 - 12:07 PM
elminister #1
Posted 08 January 2013 - 01:07 PM
Hello,

i used the code from another mate here to control my computer in MC. This programm is perfect for me.

But i have the problem is when the chunk are unloaded or i restart the server the state of all active redstone signals go back to 0.
I want to save the state and after computer restart it should set it automatically to the last state.

http://pastebin.com/Pj1f5dRt

I'm not really good in writing programs. Hope anyone can help me..

Greetings
theoriginalbit #2
Posted 08 January 2013 - 01:24 PM
ok so i would add a simple function that writes to a file, updated each time a state changes. and another which reads from the file at startup ( lets say line 82, just before the main while loop ).
you would then use serialize and unserialize from textutils api. these functions turn tables into a string, and back from a string.

Example ( may need a little modification for your use case ):

function readFile( )
  local file = io.open( "some/File", "r" )
  local contents = file:read( "*a" )
  file:close()
  if contents then
    States = textutils.unserialize( contents )
  end
end

function writeToFile( )
  local file = io.open( "some/File", "w" )
  file:write( textutils.serialize( States ) )
  file:close( )
end
elminister #3
Posted 09 January 2013 - 07:11 AM
ok i'm absolute new to this and i want to get this work :)/> i think its not enough to add this in my code ?
theoriginalbit #4
Posted 09 January 2013 - 07:14 AM
in all honesty I didn't read your code. I read your problem and gave the solution. have you tried first? there is more satisfaction in knowing you did it yourself ;)/>
theoriginalbit #5
Posted 09 January 2013 - 03:10 PM
there you go… http://pastebin.com/vkG7qS0n
elminister #6
Posted 10 January 2013 - 09:08 AM
thank you originalBit. It works now.

i have delete a function and create the save file manually and it works now:

http://pastebin.com/CTpQsRex

if anyone want it.