Help with concommand.Add

I’m currently working on a race type Gamemode and I am trying to make it so you can choose how many laps in a race there are on the map selection screen.

There is no command to change how many laps though, and you can only edit how many laps there are in “shared.lua”

The code in “shared.lua” looks like this:


GM.NumLaps = 10

I’m not sure how you would make that so you can make it a console command with concommand.Add

I think you have to make it a function first, but i’m not that smart when it comes to lua.

Any help would be greatly appreciated thanks.

You should probably use a ConVar instead of a concommand since you only want to set a value and nothing else. You can set the default like this:
[lua]if !ConVarExists( “gm_numlaps” ) then CreateConVar( “gm_numlaps”, 10, { FCVAR_ARCHIVE, FCVAR_NOTIFY } ) end[/lua]

[editline]9th March 2014[/editline]

Actually in hindsight you shouldn’t use FCVAR_NOTIFY because it would get annoying to get a message every time someone’s set the laps.

On top of this:

https://dl.dropboxusercontent.com/u/26074909/tutoring/convars/creating_and_using_client_convars.lua

This shows you how to Get the value.

You can create them and assign the ConVar to a global constant. Or, if you’ve created one, you can get the value based on the name. There are various GET operators such as
GetBool
GetDefault
GetFloat
GetInt
GetName
GetString

He’s going to be running it in initialize, there’s no reason to even check if the convar exists. Even if it does, it silently fails and does nothing.

Okay so it’s still not working:

I’ve created another .lua file named “cl_laps.lua”

“cl_laps.lua”:



include( "shared.lua" )

if ( !ConVarExists( "mr_numlaps" ) ) then
	
	CONVAR_MR_NUMLAPS_ = CreateClientConVar( "mr_numlaps", 10, { FCVAR_ARCHIVE } )
	
end


hook.Add( "NumLaps", function(  ply  )

	if ( !CONVAR_MR_NUMLAPS:GetBool( ) ) then return; end	

	if ( !GetConVar( "mr_numlaps" ):GetBool( ) ) then return; end

end );


and here’s what’s in the Gamemodes settings:



	"settings"
	{
		1
		{
			"name"		"mr_numlaps"
			"text"		"Number of laps:"
			"help"		"How many laps in your race"
			"type"		"Numeric"
			"default"	"10"
		}
	}
}	


It’s not giving me any script errors though, i’m not sure whats not working.

CONVAR_MR_NUMLAPS_ = CreateClientConVar( “mr_numlaps”, 10, { FCVAR_ARCHIVE } )

!=

if ( !CONVAR_MR_NUMLAPS:GetBool( ) ) then return; end

You have a trailing _ on the declared version, but not on the one you check.

Okay trying it now.

[editline]9th March 2014[/editline]



include( "shared.lua" )

if ( !ConVarExists( "mr_numlaps" ) ) then
	
	CONVAR_MR_NUMLAPS_ = CreateClientConVar( "mr_numlaps", 10, { FCVAR_ARCHIVE } )
	
end


hook.Add( "NumLaps", function(  ply  )

	if ( !CONVAR_MR_NUMLAPS_:GetBool( ) ) then return; end	

	if ( !GetConVar( "mr_numlaps" ):GetBool( ) ) then return; end

end );

player.GetByID( 1 ):PrintMessage( HUD_PRINTTALK, "lapsworking" )


I updated it and added a PrintMessage to make sure it was actually running, it does run.

It still doesnt change the lap amount though, even if i change it in the console. :confused:

[editline]9th March 2014[/editline]

I’ve tried changing CONVAR_MR_NUMLAPS to CONVAR_GM_NUMLAPS but that does nothing at all.