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
config
Started by Rougeminner, Dec 01 2014 07:46 PM
5 replies to this topic
#1
Posted 01 December 2014 - 07:46 PM
#2
Posted 01 December 2014 - 08:10 PM
um...
Now the program
...
--#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
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:
you could easily save and load it with the following code
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:
'd
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 )
Edited by wieselkatze, 01 December 2014 - 08:19 PM.
#4
Posted 01 December 2014 - 09:10 PM
Could you not just do on the start up file?
edit startup
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)
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
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:
Load it like so:
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
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











