S&box Wiki

Revision Difference

Coding-Cheat-Sheet#547294

<cat>Code.Intro</cat> <title>Cheat-Sheet</title> A lot of things are 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.Line( 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, eyePos + eyeDir * 2000 ) .Ignore( Entity ) // trace 2000 units in eye direction, ignore all entities⤶ TraceResult tr = Trace.Ray( EyePosition, EyePosition + EyeRotation.Forward * 2000 ) .WorldOnly() .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 ); DebugOverlay.Sphere( tr.EndPosition, 2.0f, Color.Red, duration: 10.0f ); } ``` ## Create a Server Console Command ``` [ConCmd.Server( "my_command" )] public static void MyConsoleCommand() { // the client that is calling the console command var callingClient = ConsoleSystem.Caller; } ``` ## Create a Client Console Command ``` [ConCmd.Client( "my_command" )] public static void MyConsoleCommand() { } ``` ## Console Command Arguments ``` // my_command Poop 7 false [ConCmd.Client( "my_command" )] public static void MyConsoleCommand( string one, int two, bool three ) { } ``` ## Calling a Console Command ``` MyConsoleCommand( "poop", 7, false ); ```