• Problem with variables
    8 replies, posted
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: [CODE]GM.Config.showhud = true[/CODE] Here some code of the Init: [CODE]AddCSLuaFile( "sh_config.lua") GM.Config = GM.Config or {} include( "sh_config.lua" )[/CODE] And of the cl_init: [CODE]include( "sh_config.lua" ) GM.Config = {}[/CODE] All of the files are in the same folder. Here the error: [CODE][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 [/CODE] Help please :/
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: [CODE]if GM.Config.showhud then return end[/CODE] and it say's: [CODE][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 [/CODE]
The first way you did it was fine, just one issue: [CODE]include("sh_config.lua") GM.Config = {}[/CODE] You are technically doing this right now: [CODE]GM.Config.showhud = true GM.Config = {}[/CODE] So you are basically setting the value and then overwriting the whole thing with an empty table. You need to switch their places: [CODE]GM.Config = {} include("sh_config.lua") [/CODE]
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.
[QUOTE=meharryp;47302110]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.[/QUOTE] 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
[QUOTE=annilator3000;47302226]You don't said anything o_o[/QUOTE] 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: [code]GM.Config = { }; GM.Config.x = y; // or GM.Config = { x = y; };[/code] 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.
Sorry, you need to Log In to post a reply to this thread.