Problem with variables

Hi, I’m doing a gamemode and I have wanted to make a config file but I had some problems with a variable which don’t want to be valid…
Here the only line of the sh_config:


GM.Config.showhud = true

Here some code of the Init:


AddCSLuaFile( "sh_config.lua")
    
GM.Config = GM.Config or {}

include( "sh_config.lua" )

And of the cl_init:


include( "sh_config.lua" )

GM.Config = {}

All of the files are in the same folder.

Here the error:


[ERROR] gamemodes/MYGAMEMODE/gamemode/sh_config.lua:11: attempt to index field 'Config' (a nil value)
  1. unknown - gamemodes/MYGAMEMODE/gamemode/sh_config.lua:11
   2. include - [C]:-1
    3. unknown - gamemodes/MYGAMEMODE/gamemode/cl_hud.lua:2
     4. include - [C]:-1
      5. unknown - gamemodes/MYGAMEMODE/gamemode/cl_init.lua:2


[ERROR] gamemodes/MYGAMEMODE/gamemode/sh_config.lua:11: attempt to index field 'Config' (a nil value)
  1. unknown - gamemodes/MYGAMEMODE/gamemode/sh_config.lua:11
   2. include - [C]:-1
    3. unknown - gamemodes/MYGAMEMODE/gamemode/cl_init.lua:3


Help please :confused:

Okay initially read that wrong; I think the problem is you’re creating a table serverside and clientside when you should be putting GM.Config in shared.lua. I could be wrong though since I don’t use this method much but I’m pretty sure creating a serverside table with the same name as a clientside table causes conflict issues. Try just putting GM.Config = {} in your shared file.

ok, now i’m trying this on a cl_hud.lua:


if GM.Config.showhud then return end

and it say’s:


[ERROR] gamemodes/MYGAMEMODE/gamemode/cl_hud.lua:3: attempt to index global 'GM' (a nil value)
  1. unknown - gamemodes/MYGAMEMODE/gamemode/cl_hud.lua:3


The first way you did it was fine, just one issue:


include("sh_config.lua")
GM.Config = {}

You are technically doing this right now:


GM.Config.showhud = true
GM.Config = {}

So you are basically setting the value and then overwriting the whole thing with an empty table.
You need to switch their places:


GM.Config = {}
include("sh_config.lua")


At the top of any file that isn’t an init file, add local GM = GAMEMODE. Also, make sure to AddCSLuaFile everything, if you haven’t already.

I don’t think you even looked at the error… You can clearly see it’s being loaded on client and it is loading the config no problem, just the issue I said before.

You don’t said anything o_o

What?

You can’t use GM or GAMEMODE variables in an addon unless you use them in a hook which is called when the game-mode starts loading.

When using GM or GAMEMODE, you need to know when they are what they are and what it turns into… So, when a game-mode initially loads, the variable is GM. This is why game-mode functions are defined as function GM:Blah( ) … end and not function GAMEMODE:Blah( ) … end…

When the game-mode has finished loading, GM is copied to GAMEMODE and GM is nullified. So, if you create game-mode functions in a hook that is called after the game-mode has loaded you’d use the second example ( using GAMEMODE )…

Now, there are some issues with the way you’re trying to access the variable. First, you can’t simply define GM.Config.x = y; because Config may not exist.

You’d do it in steps ( or together ) like so:


GM.Config = { };
GM.Config.x = y;

// or

GM.Config = {
	x = y;
};

assuming you do it when the game-mode is loading ( in a gm file ) which you are doing ( your only issue is you’re not defining Config as a table as I show you in the example which is why you’re getting “Config is nil” error ).

Also, I’d recommend using all lowercase file-names and folder-names ( and all references you make to files in Lua should be lower-case too ) for *nix operating systems.

edit: Actually, seems someone told you already that you need to do it in the right order.