150 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
376 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 on 01 December 2014 - 07:11 PM
227 posts
Location
Germany
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 on 01 December 2014 - 07:19 PM
121 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)
808 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 on 01 December 2014 - 08:29 PM
7508 posts
Location
Australia
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.