How to: External Configuration Files.

User avatar
Traxie21
Member
 
Posts: 753
Joined: Mon Dec 31, 2012 10:48

How to: External Configuration Files.

by Traxie21 » Sat Feb 16, 2013 15:14

I know that there's lots of mods out there that have values in init.lua that they intend to be changed to taste by the users of the mod. However, not everyone will want to have to open up a lua file and possibly end up breaking some important code.

There is a solution to this, and it is very simple. In fact, I'm surprised so few people use it. Use an external .txt file to store the variables that you want the user to change.

Here's how:

1: In your mod folder, create a file called config.txt

2: Add this inside it:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
testvar = true
--Just a boolean value for fun

3: Add these lines in init.lua: (configpath var might be wrong, need to check)
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
configpath = minetest.get_mod_directory() ..'/config.txt'
dofile(configpath)

if testvar == true then
     print("success")
end


And that is how to read lua from external text files!
Take a look at my ServerExtended mod to see this in action.
Last edited by Traxie21 on Sat Feb 16, 2013 17:31, edited 1 time in total.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Sat Feb 16, 2013 15:16

One can also use minetest.conf via minetest.setting_get().
 

User avatar
Traxie21
Member
 
Posts: 753
Joined: Mon Dec 31, 2012 10:48

by Traxie21 » Sat Feb 16, 2013 17:28

Indeed, but that has no comments and can be messy to read.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Sat Feb 16, 2013 17:58

Traxie21 wrote:Indeed, but that has no comments and can be messy to read.

You can add comments in minetest.conf with a #.
 

lkjoel
Member
 
Posts: 778
Joined: Wed Feb 29, 2012 19:27

by lkjoel » Sat Feb 16, 2013 19:57

PilzAdam wrote:
Traxie21 wrote:Indeed, but that has no comments and can be messy to read.

You can add comments in minetest.conf with a #.

True, but it's way less powerful, for example, using dofile:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = "foo"
test1 = "bar"
test_combined = test .. test1

And in minetest.conf:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = foo
test1 = bar
test_combined = foobar

Now say you made a lot of "test_combined"s, and then you think... oh wait... test1 was wrong! I wanted "baz"! Then you have to replace each of them by hand.

So I vote for Traxie's solution.
My mods: The Nether | Doctor Who (WIP)

I have quit minetest ... again. I am heavily unimpressed by both the game and the community.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Sat Feb 16, 2013 20:19

lkjoel wrote:
PilzAdam wrote:
Traxie21 wrote:Indeed, but that has no comments and can be messy to read.

You can add comments in minetest.conf with a #.

True, but it's way less powerful, for example, using dofile:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = "foo"
test1 = "bar"
test_combined = test .. test1

And in minetest.conf:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = foo
test1 = bar
test_combined = foobar

Now say you made a lot of "test_combined"s, and then you think... oh wait... test1 was wrong! I wanted "baz"! Then you have to replace each of them by hand.

So I vote for Traxie's solution.

Doesnt this aim for not-programmers?
 

lkjoel
Member
 
Posts: 778
Joined: Wed Feb 29, 2012 19:27

by lkjoel » Sat Feb 16, 2013 20:20

PilzAdam wrote:
lkjoel wrote:
PilzAdam wrote:You can add comments in minetest.conf with a #.

True, but it's way less powerful, for example, using dofile:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = "foo"
test1 = "bar"
test_combined = test .. test1

And in minetest.conf:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
test = foo
test1 = bar
test_combined = foobar

Now say you made a lot of "test_combined"s, and then you think... oh wait... test1 was wrong! I wanted "baz"! Then you have to replace each of them by hand.

So I vote for Traxie's solution.

Doesnt this aim for not-programmers?

Yeah it does, but both programmers AND non-programmers will like using lua, vs only users liking minetest.conf
My mods: The Nether | Doctor Who (WIP)

I have quit minetest ... again. I am heavily unimpressed by both the game and the community.
 

User avatar
Traxie21
Member
 
Posts: 753
Joined: Mon Dec 31, 2012 10:48

by Traxie21 » Sat Feb 16, 2013 22:04

The point is only to achieve maximum usability and effeciency. You can use whichever you want or even both.

Here's some pros/cons

minetest.conf:
Pros:
all variables are stored in one place
slightly easier to read (if spaced and commented)
easy to set from a mod
less code used to get/set
can change values from other mods easily

Cons:
Can be messy and intersperced with variables from other mods
Is not reset when the mod is removed
Can conflict with other mods
Can be cluttered, and default variables are not included with the mod (requires more code to set default values)

Best use (IMO): For setting persistant variables that the user does not need to see, but must be accesses and changed by mods)


Mod specific txt files:
Pros:
Easy to comment/format to will
Can be easily readable
Only has to be called with two lines of code and variables will persist for the rest of the mod.
Removed when deleting the mod.
Can be changed to be world-specific
Will be logically seen as the config file for that mod only

Cons:
Hard to set variables from inside a mod.
Hard(er) to use from other mods
If there are a lot of these, it takes more time to edit.

Best use (IMO): A configuration file for users to set values that will not be changed by the mod.
Storing complicated tables and such.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 13 guests

cron