Jump to content




config


  • You cannot reply to this topic
5 replies to this topic

#1 Rougeminner

  • Members
  • 151 posts

Posted 01 December 2014 - 07:46 PM

I would like to now how to make a config file inside computercraft so i can save small setting like the background color of the terminal. can some one display the code i would need

#2 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 01 December 2014 - 08:10 PM

um...

--#File named setting
bColor = colors.red

Now the program
os.loadAPI(setting)
term.setBackgroundColor(setting.bColor)
term.clear()

...

Edited by Requiem, 01 December 2014 - 08:11 PM.


#3 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 01 December 2014 - 08:18 PM

You would have to store your variables in a file.
The easiest way to do that is creating a table with all your variables and writing/reading the serialized table from the file.
So when your variables would look like this:

local myVariables = {
  background = 32768;
}

you could easily save and load it with the following code

local function save()
  local f = fs.open( "my_variables", "w" )
  f.write( textutils.serialize( myVariables ) )
  f.close()
end

local function load()
  if fs.exists( "my_variables" ) then
	local f = fs.open( "my_variables", "r" )
	local vars = textutils.unserialize( f.readAll() )
	f.close()
	return vars
  end

  return {}
end

The code above would save the variables with save() and return them with load(). That means that you can easily load them with the following code:

local myVariables = load() --# Assuming you've saved them before

--# Now for the background part

term.setBackgroundColor( myVariables.background )


:ph34r: 'd

Edited by wieselkatze, 01 December 2014 - 08:19 PM.


#4 The_Cat

  • Members
  • 119 posts

Posted 01 December 2014 - 09:10 PM

Could you not just do on the start up file?

edit startup
term.setBackgroundColor(colors.red)
term.clear()
term.setCursorPos(1,1)

then if you do CTRL + R to reboot it will start up with the colour background of your choice.

PS. you will want to set a label to you computer so it remebers what to start up: label set bob (can be any name)

#5 ElvishJerricco

  • Members
  • 803 posts

Posted 01 December 2014 - 09:29 PM

Don't use the os.loadAPI method above. That's not what APIs are for.

You want to use textutils.unserialize(). A config file can look like this:

--# config.lua_object -- file extension doesn't matter
{
	background="red",
	text="blue"
}

Load it like so:

local function loadConfig(path)
	assert(type(path) == "string", "Expected string, got " .. type(path))
	local fh = assert(fs.open(path, "r"), "Failed to open config: " .. path)
	local serialized = fh.readAll()
	return textutils.unserialize(serialized)
end

local config = loadConfig("config.lua_object")

Edited by ElvishJerricco, 01 December 2014 - 09:29 PM.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 December 2014 - 02:04 AM

alternatively there are a few configuration APIs floating around the forums, for example I have one called ccConfig (link to my programs in my signature) which allows you to be a little more user-friendly in your configs compared to textutils.un/serialize, allowing you to specify comments and the likes on your config items.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users