Jump to content




How to create config?


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

#1 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 11 May 2014 - 04:45 PM

How to create config?

#2 Lignum

  • Members
  • 558 posts

Posted 11 May 2014 - 04:56 PM

I assume you mean creating config files for your own programs. Have a look at the fs API, fs.open specifically.

#3 RoD

  • Members
  • 313 posts

Posted 12 May 2014 - 08:47 AM

I have this one based on luaIDE, that loads the config into a table:
local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:
config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:
firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 May 2014 - 09:00 AM

I also have a version which can be found on my programs page in the APIs section (link in signature) called ccConfig, it can handle other forms of data like numbers, strings, colours, and booleans.

#5 RoD

  • Members
  • 313 posts

Posted 12 May 2014 - 10:41 AM

View Posttheoriginalbit, on 12 May 2014 - 09:00 AM, said:

I also have a version which can be found on my programs page in the APIs section (link in signature) called ccConfig, it can handle other forms of data like numbers, strings, colours, and booleans.
Its pretty usefull. I might test it soon. :)

#6 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 05:01 PM

View PostRoD, on 12 May 2014 - 08:47 AM, said:

I have this one based on luaIDE, that loads the config into a table:
local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:
config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:
firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.

f:read() io.open() what??? Why : and why io.open what is io... (Input-output I know but how can the io API manage files?)
I don't understand anything what was in your post RoD
I know that, I'm not any pro, then please tell me this as for noob.

Sorry for bad english

#7 apemanzilla

  • Members
  • 1,421 posts

Posted 16 May 2014 - 07:16 PM

View PostKonlab, on 16 May 2014 - 05:01 PM, said:

View PostRoD, on 12 May 2014 - 08:47 AM, said:

I have this one based on luaIDE, that loads the config into a table:
local function loadConfig(path)
local f = io.open(path)
local l = f:read("*l")
local config = {}
while l ~= nil do
  local k, v = string.match(l, "^(%a+) = (%a+)")
  if k and v then config[k] = v end
  l = f:read("*l")
end
f:close()
return config
end

If you the want to use the config just:
config = loadCOnfig("myFolders/config")
print( config.firstConfig )
print( config.secondConfig )
print( config.thirdConfig )

The config file would look like:
firstConfig = "Hey, i am line one!"
secondConfig = "Hello, i am the second line!"
thirdConfig = "Hi, third line here!"

So basically you loaded variables from a file into a table.

f:read() io.open() what??? Why : and why io.open what is io... (Input-output I know but how can the io API manage files?)
I don't understand anything what was in your post RoD
I know that, I'm not any pro, then please tell me this as for noob.

Sorry for bad english
Please take a look at the cc wiki page or the Lua reference manual page for the IO API.

#8 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 07:18 PM

I have the answer
Please Lock this topic!

#9 apemanzilla

  • Members
  • 1,421 posts

Posted 16 May 2014 - 07:24 PM

View PostKonlab, on 16 May 2014 - 07:18 PM, said:

I have the answer
Please Lock this topic!
We usually don't lock AaP section threads in case someone has a similar/continued issue (in a non-hijacking way)

#10 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 07:26 PM

?

#11 apemanzilla

  • Members
  • 1,421 posts

Posted 16 May 2014 - 07:29 PM

Basically, if someone else had the same problem and wanted to post here, or if you had more trouble with it, you would have to start a whole new thread because this one is locked. That's why we keep them unlocked, so the section isn't flooded with duplicates as much.

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 May 2014 - 09:34 PM

Also, for reference, when using the io API and wanting to process individual lines, it is much easier to use the handle:lines() iterator than to use a while loop and handle:read("*l").





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users