• Troubles with CreateConVar and GetConVarNumber
    9 replies, posted
Here's my problem. I'm working on a gamemode and I have an option for how many players are required before the round starts (a la TTT). Currently, one has to change the value manually in a lua file, but I'd like to create a ConVar so one can easily set common values through server.cfg or otherwise. Here is my code: [CODE]-- in gamemode/init.lua GM.minPlayers = CreateConVar( "sb_minplayers", 2, bit.bor(FCVAR_NOTIFY), "Nuber of players needed for the round to start" ) -- name, default value, Enums/FCVAR, help text -- in sh_roundtimer.lua round.MinPlayers = GetConVarNumber( "sb_minplayers" ) function enoughPlayers() local numPlayers = 0 if table.Count(player.GetAll()) >= round.MinPlayers then if SERVER then for k, v in pairs(player.GetAll()) do if IsValid(v) then v:PrintMessage(HUD_PRINTCENTER, "Round started! You have " .. math.Round(round.Time/60) .. " minutes!") v:SetTeam(TEAM_HUMANS) v:Spawn() v:StripWeapon("sb_barrelsuicider") end end local randomply = table.Random(player.GetAll()) randomply:SetTeam(TEAM_BARRELS) randomply:Spawn() randomply:StripWeapon("sb_barrelslayer") randomply:PrintMessage(HUD_PRINTCENTER, "Round started! You are a Barrel! You have " .. math.Round(round.Time/60) .. " minutes to eliminate the humans!") end round.TimeLeft = round.Time round.Active = true round.RoundCur = round.RoundCur + 1 elseif table.Count(player.GetAll()) < round.MinPlayers then if SERVER then for k, v in pairs(player.GetAll()) do if IsValid(v) then v:SetTeam(TEAM_SPECTATOR) end end end round.Active = false end end[/CODE] Despite all this, it appears that the value is not recognized. The round does not start, even if there are more than 2 people on the server.
You may have issues with creating convars for server.cfg If I recall correctly, server.cfg is called before the convars have a chance to be made... I may be wrong, but here is a way to create convars: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/convars/creating_and_using_client_convars.lua.html[/url] Could've sworn I had more tutorials on this, I'll have to add more.
Try [lua] GetConVar("sb_minplayers"):GetInt() [/lua]
Instead of saving the value to a variable every time server starts, retrieve the value every time it is needed, so[code]if table.Count(player.GetAll()) >= round.MinPlayers then[/code] becomes [code]if table.Count(player.GetAll()) >= GetConVarNumber( "sb_minplayers" ) then[/code] or even [code]if #player.GetAll() >= GetConVarNumber( "sb_minplayers" ) then[/code]
[QUOTE=Robotboy655;45069966]Instead of saving the value to a variable every time server starts, retrieve the value every time it is needed, so[code]if table.Count(player.GetAll()) >= round.MinPlayers then[/code] becomes [code]if table.Count(player.GetAll()) >= GetConVarNumber( "sb_minplayers" ) then[/code] or even [code]if #player.GetAll() >= GetConVarNumber( "sb_minplayers" ) then[/code][/QUOTE] Oh yes totally lets teach people how to unoptimize their code. You should only be calling GetConVar* if you're going to be checking the number for changes every time you call the code... Obviously in this situation you're not going to be calling it that often, but if it was something in a HUD or Think hook, and he followed your advice it would slow down that function considerably. If you had actually read the code you might have considered telling him to change the if statement to GM.minPlayers:GetInt(), because he's saving a refrence to the ConVar and then looking up the convar again when he doesn't need to be.
[QUOTE=OzymandiasJ;45071952]Oh yes totally lets teach people how to unoptimize their code.[/QUOTE] Oh yes, lets totally say nonsense. [QUOTE=OzymandiasJ;45071952]You should only be calling GetConVar* if you're going to be checking the number for changes every time you call the code...[/QUOTE] This is exactly the case here. He needs to check for a new value every time to see if convar changed or not. This might even solve the server.cfg problem. [QUOTE=OzymandiasJ;45071952] Obviously in this situation you're not going to be calling it that often, but if it was something in a HUD or Think hook, and he followed your advice it would slow down that function considerably.[/QUOTE] Obviously he isn't making a HUD. [QUOTE=OzymandiasJ;45071952] If you had actually read the code you might have considered telling him to change the if statement to GM.minPlayers:GetInt(), because he's saving a refrence to the ConVar and then looking up the convar again when he doesn't need to be.[/QUOTE] If you actually had read the code, you might have realized that GM.minPlayers equals to GetConVarNumber( "sb_minplayers" ) and returns a number, not a ConVar object.
[QUOTE=Robotboy655;45072146]If you actually had read the code, you might have realized that GM.minPlayers equals to GetConVarNumber( "sb_minplayers" ) and returns a number, not a ConVar object.[/QUOTE] CreateConVar returns a ConVar object though?
[QUOTE=Willox;45072181]CreateConVar returns a ConVar object though?[/QUOTE] Darn, I confused GM.minPlayers and round.MinPlayers, my bad.
[QUOTE=Robotboy655;45072146]This is exactly the case here. He needs to check for a new value every time to see if convar changed or not. This might even solve the server.cfg problem.[/QUOTE] [QUOTE=RalphORama;45068902] Despite all this, it appears that the value is not recognized. The round does not start, [B]even if there are more than 2 people on the server.[/B][/QUOTE] Nope [QUOTE=Robotboy655;45072146]Obviously he isn't making a HUD.[/QUOTE] You're essentially telling him what he is doing is wrong without telling him why thus in all likelyhood if he had followed your advice he might have done this in some other code.
I'm sorry, I don't think I made my intention clear. I'm basically aping the idea behind the [I]ttt_minimum_players[/I] setting from TTT. I'd like to be able to check the value of sb_minplayers and use that for round.minPlayers (i.e. if in server.cfg sb_minplayers = 3, the round will start when there are 3 players present, like TTT). If I replace [code]round.MinPlayers = GetConVarNumber( "sb_minplayers" )[/code] with [code]round.MinPlayers = 2[/code] everything works fine. The round starts if there are 2 people. However, when [I]round.MinPlayers = GetConVarNumber( "sb_minplayers" )[/I] is set, the round doesn't start. No errors show up in console, but, as I said, the round won't start either. From what Acetool and Blasteh have said, I'll try chaning my code to the following once I get home: [code]round.minPlayers = GetConVar("sb_minplayers"):GetInt()[/code] Also, Robotboy655, I'll update my code to do what you said, for now I'm using a variable because that's how the original code was, I'll try your method as well. [B]EDIT:[/B] Upon replacing all the instances of round.MinPlayers with GetConVarNumber( "sb_minplayers" ), I get the following error: [IMG]http://puu.sh/9pKzE/b2f240dd7b.png[/IMG] I have a feeling that this conVar isn't being created properly somehow, here's my code for creating the conVars: [CODE]CreateConVar( "sb_minplayers", "2" )[/CODE] However, when I plug the conVar into console, I get the following: [IMG]http://puu.sh/9pJXB/2224472cab.png[/IMG] I'm not sure why no default value is being set, what would the reason for this be?
Sorry, you need to Log In to post a reply to this thread.