Weird bug with concommands

Hello,

Look at this simple code:



local function testcc( ply, cmd, args )
	print( SERVER and "sv" or "cl" )
end
concommand.Add( "testcc", testcc )


Put this code in a file in lua/autorun, in both your game and on a dedicated server. Then do the following:

1 - Start Gmod and your dedicated server
2 - Start a Single Player game, type “testcc” from in-game console, it will print “sv”. This is normal behavior, but doing this step (starting Single Player game just after starting Gmod) is what’s causing the issue described in the next step.
3 - Connect to the dedicated server, type “testcc” from in-game console: problem! It should print “cl” in the in-game console, but instead, it prints “sv” in the server console.

4 - Restart Gmod (you don’t need to restart dedicated server)
5 - Connect to the dedicated server, type “testcc” from in-game console, this time it will now print “cl”, as it should.
6 - From now on, until Gmod is closed, the problem will not happen: you can disconnect from dedicated server and start a Single Player game: command will print “sv”, reconnect to dedicated server: it will print “cl”, etc…

So it appears to me that there is a bug if you first start a Single Player game and then connect to a dedicated server, like if the concommand wasn’t “unregistered” correctly from the Single Player session.

Should I make a bug report on Github ? Done anyway: https://github.com/Facepunch/garrysmod-issues/issues/1902

I may not be understanding you correctly, but this appears to be intended behavior… Here’s how realms work ( It depends where code is executed ): https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/explanation_of_realms.lua.html

Always use SRCDS to test behavior ( with maxplayers set to more than 1 otherwise you’ll be in “SinglePlayer” mode ); if you use Listen Server ( Which is a server opened using gmod client ) you will need to know exactly how the code will run on an actual server or you’ll have issues because of how code executes ( Client is the server and server is the client )…

https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/setting_up_a_server_with_steamcmd.lua.html

I doubt this is intended behavior, I think you didn’t really understand the problem I described.

My test was done with dedicated server from SteamCMD, not a listen server from the in-game “Start new game” menu.

Try read again carefully the steps I described. The problem is in step 3. You can try the first 3 steps, to understand the problem. I do my best to write in english but it may not be that good…

With your revised post it seems as though you’re saying the Listen-Server files don’t get flushed from memory when joining the dedicated server ( Which happens with fonts [ they stay in memory from one server to the next ] )…

The problem with Listen-Servers is they load both client and server code on the client so there is overlap and things can be overwritten. The way you’re explaining it, it may be as simple as the CLIENT and / or SERVER vars being over-written on the client…

The only possible fix, if that is the case, would be to assign SERVER, CLIENT = false, true; in cl_init.lua and SERVER, CLIENT = true, false; in init.lua… as no other method would work…

If this is the case though, this won’t be exploitable to download server files for those of us that don’t like repeating lines of code in both init.lua and cl_init.lua by using a shared.lua or sh_init.lua file to handle everything by relying on SERVER / CLIENT vars to include files BECAUSE the server will have never called AddCSLuaFile on the server files, so this isn’t a large bug if it is infact a bug. It may cause other issues though ( such as possibly giving the client a way to set server-side vars locally much like the increment var command ) so this still needs to be looked at…

I tried doing some testing but for some reason I get Engine Error ( [GM:Call - !ThreadInMainThread] GetGameDescription ) when starting a single-player game in base or sandbox or any game-mode for that matter ( and script errors in sandbox ), and when I start with 2 players I get ( [GM:CallWithArgs - !ThreadInMainThread] GetGameDescription )

If someone else can verify this, that’d be great!

Is that file being run on server or client or both? Make sure it’s only being executed in 1 place before testing.

If you add it to server files only, it will never show anything in the client console.

If you add it to client files only, it will never show anything on the server console.

lua/autorun/ is SHARED so if the server runs it, then it should print sv in SERVER console only. If a client executes it, then it should only print cl in the client console ( My document covers this scenario ).

The OP is saying sv is being output in the client console so the only explanation is that SERVER must be set to true… That’s how I read the post anyways…

Actually, I just read the revised post again… It seems that it IS being executed on the server when the command is executed on the client, so that means the client isn’t seeing the console command which could be because of Lua File Limit or commonly named file which is being overwritten by something else on a client addon, maybe???

You can access Serverside concommands as a client (Quick example: rp_setmoney), as the serverside command is beeing created, the clientside command is beeing ignored.

So if you execute the code now it will in fact execute but only the concommand on the server.

file placed in client’s lua/autorun folder

single player, client console>



] testcc 
sv


join dedicated server, client console>



] testcc
Unknown command: testcc


file placed in lua/autorun both on server and client
single player, client console>



] testcc
sv


join dedicated server, client console>



] testcc


server console outputs>



sv


Can’t see a fault here.

edit: I take that back… it does something weird…

restart game, join dedicated server, client console>



] testcc
cl


nothing outputs serverside.

remove the file from client’s lua/autorun and move the lua file on the server to a serverside only folder, client console>



] testcc
Unknown command: testcc


server console>



testcc
sv


So the bit that is weird is that, one of the commands only outputted on server console, then after restarting the game outputs only on client console.
The realm stays correct in all cases though.

That doesn’t follow the proper logic…

That’s why you test code using SRCDS… Client / Server code mix and give unreliable results; I’ve been saying that for a while now… So, seems when the console command is loaded on single-player it stays in memory as a server-side only command ( because SERVER overwrote the CLIENT version of the code on single-player / Listen-Server ) so it should clear so when you join a dedicated, it’ll execute correctly but it doesn’t…