Help with Tables

I am using tables to catagories my functions / config variables etc. Here is my table setup that’s ran on server start:


SGNDarkRP = {}
SGNDarkRP.Config = {}
SGNDarkRP.Config.Dev = {}
SGNDarkRP.Config.Client = {}
SGNDarkRP.Config.Misc = {}
SGNDarkRP.Config.Server = {}

And then I set the variables in the config file:


///Sets the default client colours for the game-mode hud elements:
//SGNDarkRP.Config.Client.defaultModuleColourOne		= nil
//SGNDarkRP.Config.Client.defaultModuleColourTwo		= nil

//Will debug text be printed to the console
SGNDarkRP.Config.Dev.debugEnabled					= true
SGNDarkRP.Config.Dev.deleteSQLTablesOnStart			= true


And then used in this function:


function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled == true then
		print( "[DEBUG] " .. message )
    end
end

But get the error:


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../functions.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../functions.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:15


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../databases.lua:2: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../databases.lua:2
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:17


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/apartments/sv_apartments.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/apartments/sv_apartments.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:21


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/character/sv_character.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/character/sv_character.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:24


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/hud/sv_hud.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/hud/sv_hud.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:27


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/inventory/sv_inventory.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/inventory/sv_inventory.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:30

[AddCSLuaFile] Couldn't find 'autorun/client/cl_laws.lua' (@addons/sgn_darkrpmod/lua/autorun/server/../../modules/laws/sv_laws.lua (line 1))

[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/laws/sv_laws.lua:5: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/laws/sv_laws.lua:5
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:33


[ERROR] addons/sgn_darkrpmod/lua/autorun/server/../../modules/quests/sv_quests.lua:1: attempt to call global 'debugText' (a nil value)
  1. unknown - addons/sgn_darkrpmod/lua/autorun/server/../../modules/quests/sv_quests.lua:1
   2. include - [C]:-1
    3. unknown - addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua:36


I’m assuming this all falls apart because of the : attempt to index global ‘SGNDarkRP’ (a nil value)

Am I using those tables wrong?

You put it as ‘SGNDarkRP.Config.Misc.debugEnabled’ on the config you put it as ‘SGNDarkRP.Config.Dev.debugEnabled’

Oh thanks, I’ve changed that, must have been when I was moving stuff around. However it still doesn’t recognize debugPrint?

I’ve updated the four code tags above to include my new pastes.

Am I doing this right? : if SGNDarkRP.Config.Dev.debugEnabled == true then

you don’t need == true

Don’t need, but it wouldn’t stop it working would it?

[editline]5th June 2014[/editline]

Nope, still saying debugText is a nil value

[editline]5th June 2014[/editline]

Is debugText trying to be called before the function has actually been read?

Probably because you try to call it before defining it.

I think it probably is, so how would you make the print to show the file has been loaded wait until the function has been defined? I tried putting it in an initialize hook and it didn’t work. Is it worth just making a second timer or are there better ways.

I supose on the whole I wouldn’t need to be doing something the instant the server loads.

[editline]5th June 2014[/editline]

Okay I changed them all to:



timer.Simple(1, function() debugText( "functions.lua has loaded." ) end )


and my debugText function:



function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled then
		print( "[DEBUG] " .. message )
    end
end


It no longer throws any errors, but doesn’t print either :slight_smile:

Include file with function before any file that uses that function.

Oh and:



SGNDarkRP.Config.Dev.debugEnabled					= true


[editline]5th June 2014[/editline]

My sv_init.lua includes all the files, so they can all access each other.



//Addon startup script://

SGNDarkRP = {}
SGNDarkRP.Config = {}
SGNDarkRP.Config.Dev = {}
SGNDarkRP.Config.Client = {}
SGNDarkRP.Config.Misc = {}
SGNDarkRP.Config.Server = {}

print("=========================================================")
print("SGNDARKRP IS LOADING...")
print("INCLUDING ALL FILES")
print("=========================================================")

include("../../functions.lua")

include("../../databases.lua")

include("../../settings/config.lua")

include("../../modules/apartments/sv_apartments.lua")
AddCSLuaFile("../../modules/apartments/cl_apartments.lua")

include("../../modules/character/sv_character.lua")
AddCSLuaFile("../../modules/character/cl_character.lua")

include("../../modules/hud/sv_hud.lua")
AddCSLuaFile("../../modules/hud/cl_hud.lua")

include("../../modules/inventory/sv_inventory.lua")
AddCSLuaFile("../../modules/inventory/cl_inventory.lua")

include("../../modules/laws/sv_laws.lua")
AddCSLuaFile("../../modules/laws/cl_laws.lua")

include("../../modules/quests/sv_quests.lua")
AddCSLuaFile("../../modules/quests/cl_quests.lua")



print("=========================================================")
print("SGNDARKRP HAS LOADED")
print("=========================================================")


functions.lua holds the debugText function, so surely all the others can now use it? However it doesn’t complain it can’t find the function, it just doesn’t print.

Post full files so we can test it. It looks like you try to call that function on first line of the first file you include.

addons/sgn_darkrpmod/lua/autorun/server/sv_init.lua



SGNDarkRP = {}
SGNDarkRP.Config = {}
SGNDarkRP.Config.Dev = {}
SGNDarkRP.Config.Client = {}
SGNDarkRP.Config.Misc = {}
SGNDarkRP.Config.Server = {}

print("=========================================================")
print("SGNDARKRP IS LOADING...")
print("INCLUDING ALL FILES")
print("=========================================================")

include("../../functions.lua")

include("../../databases.lua")

include("../../settings/config.lua")

include("../../modules/apartments/sv_apartments.lua")
AddCSLuaFile("../../modules/apartments/cl_apartments.lua")

include("../../modules/character/sv_character.lua")
AddCSLuaFile("../../modules/character/cl_character.lua")

include("../../modules/hud/sv_hud.lua")
AddCSLuaFile("../../modules/hud/cl_hud.lua")

include("../../modules/inventory/sv_inventory.lua")
AddCSLuaFile("../../modules/inventory/cl_inventory.lua")

include("../../modules/laws/sv_laws.lua")
AddCSLuaFile("../../modules/laws/cl_laws.lua")

include("../../modules/quests/sv_quests.lua")
AddCSLuaFile("../../modules/quests/cl_quests.lua")



print("=========================================================")
print("=========================================================")


/addons/sgn_darkrpmod/lua/functions.lua



timer.Simple(1, function() debugText( "functions.lua has loaded." ) end )

//Prints debut text to the console
function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled == true then
		print( "[DEBUG] " .. message )
    end
end


/addons/sgn_darkrpmod/lua/settings/config.lua






//Sets the default client colours for the game-mode hud elements:
//SGNDarkRP.Config.Client.defaultModuleColourOne		= nil
//SGNDarkRP.Config.Client.defaultModuleColourTwo		= nil

//Will debug text be printed to the console
SGNDarkRP.Config.Dev.debugEnabled					= true
SGNDarkRP.Config.Dev.deleteSQLTablesOnStart			= true


And here is one of the module files just as an example:
/addons/sgn_darkrpmod/lua/modules/apartments/sv_apartments.lua



timer.Simple(1, function() debugText("sv_apartments.lua has been loaded.") end )



print( "[DEBUG] functions.lua has loaded." )

//Prints debut text to the console
function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled == true then
        print( "[DEBUG] " .. message )
    end
end

[editline]6th June 2014[/editline]

Or



//Prints debut text to the console
function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled == true then
        print( "[DEBUG] " .. message )
    end
end

debugText( "functions.lua has loaded." ) 

I tried taking it out of that file fully, and just now tried moving debugText to the bottom of the file instead, however it’s still not printing the debugText as the files load. But I don’t get any errors, just doesn’t print the text.



Adding Path: [/home/publicserv/srcds/css/cstrike]
FIND [/home/publicserv/srcds/css/cstrike/*.vpk]
ADDING [/home/publicserv/srcds/css/cstrike/cstrike_pak_000.vpk]
Adding Path: [/home/publicserv/srcds/tf2/tf]
FIND [/home/publicserv/srcds/tf2/tf/*.vpk]
ADDING [/home/publicserv/srcds/tf2/tf/tf2_misc_000.vpk]
ConVarRef mat_dxlevel doesn't point to an existing ConVar
Game_srv.so loaded for "Garry's Mod"
Initializing Steam libraries for secure Internet server
Logging into anonymous gameserver account.
Fetching Workshop Addons..
Calling GetCollectionDetails
Collection has 2 Addons
Connection to Steam servers successful.
   Public IP is 88.150.197.34.
Assigned anonymous gameserver Steam ID [A-1:3505516548(4361)].
Mounting Addon 'rp_industrial17_v1' (171962560)
Mounting Addon 'GPK_Freecity' (113136606)
Adding Filesystem Addon '/home/publicserv/srcds/hldev/garrysmod/addons/sgn_darkrpmod'
Unknown command "cl_cmdrate"
Unknown command "cl_updaterate"
Unknown command "rate"
Changing gamemode to DarkRP (darkrp)
WARNING: Port 27015 was unavailable - bound to port 27016 instead
WARNING: Port 27005 was unavailable - bound to port 27006 instead
WARNING: Port 27020 was unavailable - bound to port 27021 instead
Network: IP 88.150.197.34, mode MP, dedicated Yes, ports 27016 SV / 27006 CL
Map file wasn't found, copying it from addon..
PREP OK
Map file wasn't found, copying it from addon..
SOLID_VPHYSICS static prop with no vphysics model! (models/props/industrial17/dispensory.mdl)
SOLID_VPHYSICS static prop with no vphysics model! (models/props/industrial17/rationpillar.mdl)
Lua JIT is DISABLED!
=========================================================
SGNDARKRP IS LOADING...
INCLUDING ALL FILES
=========================================================
[AddCSLuaFile] Couldn't find 'autorun/client/cl_laws.lua' (@addons/sgn_darkrpmod/lua/autorun/server/../../modules/laws/sv_laws.lua (line 1))
=========================================================
SGNDARKRP HAS LOADED
=========================================================
TEST [maps/gpk_freecity.bsp][maps/rp_industrial17_v1.bsp]
TEST [maps/rp_industrial17_v1.bsp][maps/rp_industrial17_v1.bsp]
FOUND IN ADDON [171962560]
Making workshop map available for client download
ConVarRef room_type doesn't point to an existing ConVar
Executing dedicated server config file server.cfg
Using map cycle file 'cfg/mapcycle_default.txt'.  ('cfg/mapcycle.txt' was not found.)
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
nexus_catwalkaccess has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
prop_door_rotating has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
office_door2 has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
office_door2 has Door model (models/props_c17/door01_left.mdl) with no door_options! Verify that SKIN is valid, and has a corresponding options block in the model QC file
Nav File is wrong or something (1)
Initializing Steam libraries for secure Internet server
Logging into anonymous gameserver account.
Unknown command "sbox_allownpcs"
Unknown command "sbox_plpldamage"
Unknown command "sbox_playergod"
Unknown command "durgz_witty_sayings"
Connection to Steam servers successful.
   Public IP is 88.150.197.34.
Assigned anonymous gameserver Steam ID [A-1:3505627138(4361)].
VAC secure mode is activated.



New functions.lua



//Prints debut text to the console
function debugText( message )
    if SGNDarkRP.Config.Dev.debugEnabled == true then
		print( "[DEBUG] " .. message )
    end
end

debugText( "functions.lua has loaded." )


It’s not complaining anymore it can’t find the function, it just doesn’t print.

Load config before loading the functions and calling them. Can you pay more attention to your own coding, please?

Sorry, thanks for the help.

I include config.lua first now, then functions.lua but it still doesn’t print the debugText. I tried removing ’ if SGNDarkRP.Config.Dev.debugEnabled == true then’ and then it did print that it had loaded functions.lua, but isn’t printing the other files debugText?

So config.lua holds the variable, then functions.lua creates the debugText function and uses the config variable, and then all the other files use debugText.

These paths don’t match up.

Oops, sorry that’s meant to be /addons/sgn_darkrpmod/lua/settings/config.lua I’ve changed it all over now.

Thing is, it doesn’t comaplin about debugText so it obviously is finding that, it’s as though it can’t find debugEnabled in the config file :confused: