S&box Wiki

Revision Difference

Coding-Cheat-Sheet#545926

<cat>Code.Intro</cat> <title>Cheat-Sheet</title> A lot of things a common between game engines. Here's how to do a bunch of common stuff. ## Console Printing ``` Log.Info( "It worked!" ); Log.Warning( $"{playername} is an idiot!" ); Log.Error( "Error!" ); ``` ## Debug Drawing ``` // draw text at position for 10 seconds DebugOverlay.Text( position, "Some Text!", Color.Yellow, 10.0f ); // draw a line DebugOverlay.Text( startPos, endPos ); // draw text on screen DebugOverlay.ScreenText( "Hello, debug text!\nSupports multiple lines" ); ``` ## Spawning an Entity ``` ModelEntity model = new ModelEntity( "models/citizen/citizen.vmdl" ); model.Position = new Vector3( 0, 0, 100 ); ``` ## Get Entity's Eye Position and Direction ``` Vector3 eyePos = Entity.EyePos; Vector3 eyeDir = Entity.EyeRot.Forward; ``` ## Do a ray trace ``` // trace 2000 units in eye direction, ignore entity TraceResult tr = Trace.Ray( eyePos, eyeDir * 2000 ) .Ignore( Entity ) .Run(); // If we hit, draw a 2 inch sphere for 10 seconds if ( tr.Hit ) { DebugOverlay.Sphere( tr.EndPos, 2.0f, Color.Red, duration: 10.0f ); } ``` ## Create a Server Console Command ``` [ServerCmd( "my_command" )] public static void MyConsoleCommand() { // the client that is calling the console command var callingClient = ConsoleSystem.Caller; } ``` ## Create a Client Console Command ``` [ClientCmd( "my_command" )] public static void MyConsoleCommand() { } ``` ## Console Command Arguments ``` // my_command Poop 7 false [ClientCmd( "my_command" )] public static void MyConsoleCommand( string one, int two, bool three ) { } ``` ## Calling a Console Command ``` MyConsoleCommand( "poop", 7, false ); ```