How to make configs for addons ???

I see a lot of addons have files that you can config stuff (colors, prices, amount and etc).

How can I make this config files for my addons ?? ( Ex: If a hud have certain colors and they can change if they want )

First, for every file we want to use something from the config file from we have to “include” it in the file. So lets say you have your addon and you have a file named cl_myaddon and sv_myaddon. At the top you would write

include("sh_config.lua" OR "sh_yourname/pathhere.lua")

What this does is pretty much takes the code from the specified file and stores it in the file you called it in. So if you had a local function in the config you would be able to call it now from the code.

Great so now that we included the config file we should talk about the vars.

So you said you have a hud that wants to change color, in the config file we can do

Config.HUDColors = Color(255,255,255)
-- What we do is set a local variable to those colors (white) the name of the variable is Config.HUDColors.

So now where the colors for the HUD are made you would replace the current 0,0,0 with Config.HUDColors, Making sure that you included the file or else it wont work.

But How would I name the color file ? Ex: Thats the place where they can change the color in the actual file of the HUD where it have all the coding.

    draw.RoundedBox(1, HUD.PosX + 380, HUD.PosY - 36, 5, 137, Color(245,245,245,250))

How would I make to link the

with this drawRoundedBox Color thing

You can name it whatever, sh_config.lua is what I reccomend

Instead of:

draw.RoundedBox(1, HUD.PosX + 380, HUD.PosY - 36, 5, 137, Color(245,245,245,250)

You would put

draw.RoundedBox(1, HUD.PosX + 380, HUD.PosY - 36, 5, 137, Config.HUDColors)

[editline]6th March 2016[/editline]

Config.HUDColors = Color(245,245,245,250)

My bad

In lua/autorun/somecoolprint.lua:


in lua/MySuperiorConfig.lua

ConfigWhatToPrint = "Config works."

So I should put this in the code for the whole thing ?

draw.RoundedBox(1, HUD.PosX + 380, HUD.PosY - 36, 5, 137, Config.HUDColors = Color(245,245,245,250))

And this for the Config File ?

Config.HUDColors = Color(245,245,245,250)

No just do this:

draw.RoundedBox(1, HUD.PosX + 380, HUD.PosY - 36, 5, 137, Config.HUDColors)

and leave that for the config file!

Thanks a lot !! I will test it right now (and If I dont come back and say it didnt work it probably worked XD)

[editline]6th March 2016[/editline]

I got some problems thats the error:

[ERROR] A runtime error has occurred in "addons/darkrpmodification/lua/darkrp_modules/hudfolder/sh_config.lua" on line 3.
The best help I can give you is this:

'Config' is being indexed like it is a table, but in reality it does not exist (is nil).

	- You either have 'something.somethingElse', 'something[somethingElse]' or 'something:somethingElse(more)'. The 'something' here does not exist.

The responsibility for this error lies with (the authors of) one (or more) of these files:
	1. addons/darkrpmodification/lua/darkrp_modules/hudfolder/sh_config.lua on line 3
	2. gamemodes/darkrp/gamemode/libraries/fn.lua on line 84
	3. gamemodes/darkrp/gamemode/libraries/fn.lua on line 84
	4. gamemodes/darkrp/gamemode/libraries/modificationloader.lua on line 86
	5. gamemodes/darkrp/gamemode/libraries/modificationloader.lua on line 147
	6. gamemodes/darkrp/gamemode/init.lua on line 87
------- End of Simplerr error -------

My files structure:

<hud folder on darkrpmodification>
    |_ cl_font.lua
    |_ cl_main.lua
    |_ sh_config.lua

My cl_main.lua where it includes the config file. (The code was working before)


My sh_config.lua:

Config.HUDColors = Color(245,245,245,250)

Never call your config files sh_config.lua, this ends up clashing with other addons if they use the same name, come up with a unique name such as “sh_myaddon_name_config.lua”

Thats not the problem man

Just name Config.Hudcolor something else like


I know that’s not your issue but I’m putting that information out there.

He’s right ^

Oh Ok Sorry

[editline]6th March 2016[/editline]

Still getting the same errors guys…

Just name Config.Hudcolor something else like


I did

[editline]6th March 2016[/editline]

Wait it needs to be SOMETHING.HUDColor or it can be SOMETHING.HUDColors

It can be whatever you want, it’s a variable name.
Just make your config table have a unique name (Doesn’t even have to be so special, I used CCfg for one of my things just because I felt like it)
You can also do

[lua]ConfigTable1337 = {}
ConfigTable1337.TeamDamageExists = Color( 187, 154, 49, 240 )[/lua]
Even though you probably won’t name it like that :stuck_out_tongue:

The reason you’re getting an error is because you’re not declaring the table ‘Config’ before setting Config.HUDColors.

Like JasonMan34 said, you need to set your table to {} (an empty table) before using the table syntax of table.value to set a value.

In addition to having a file with a config table that you reference elsewhere, you could have a simple txt (either normal text or JSON) file that you read the configuration from. It’s a bit more difficult to set up on your end (you have to read from the file and parse the values), but in my experience it’s better for the end user.

My bad. Forgot to add the table part.