S&box Wiki

Revision Difference

Coding-Cheat-Sheet#560302

<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( "Some Text!", position, Color.Yellow, 10.0f ); // draw text at transform⤶ Gizmo.Draw.WorldText( "Some Text!", Transform.World ); // draw a line DebugOverlay.Line( startPos, endPos ); Gizmo.Draw.Line( startPos, endPos ); // draw text on screen DebugOverlay.ScreenText( "Hello, debug text!\nSupports multiple lines" ); Gizmo.Draw.ScreenText("Text but \non screen",Vector2.Zero); ``` ⤶ ## Spawning an Entity⤶ ⤶ ⤶ ## Do a ray trace⤶ ``` 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.AimRay.Position;⤶ Vector3 eyeDir = Entity.AimRay.Forward;⤶ ```⤶ ⤶ ## Do a ray trace⤶ ⤶ ```⤶ // trace 2000 units in eye direction, ignore all entities TraceResult tr = Trace.Ray( AimRay, 2000 ) .WorldOnly()⤶ TraceResult tr = Scene.Trace.Ray( AimRay, 2000 ) .Run(); // If we hit, draw a 2 inch sphere for 10 seconds⤶ // If we hit, draw a sphere⤶ if ( tr.Hit ) { DebugOverlay.Sphere( tr.EndPosition, 2.0f, Color.Red, duration: 10.0f ); Gizmo.Draw.SolidSphere( tr.HitPosition, 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 );⤶ ```⤶ ⤶ ```