My First Mod

From Valve Developer Community
Revision as of 20:13, 1 January 2010 by WinstonSmith (talk | contribs) (Changed "Now What?" section; see Coder0xff's talk page for details.)
Jump to navigation Jump to search

Template:Otherlang2

This tutorial will teach you how to create and build your first simple Source mod. In this mod we will change the speed at which Half-Life 2's rocket travels through the world.

Create the project

For the purpose of this tutorial we will call our first mod, "Mymod". When you make your own mod this can be any name you wish (i.e. "Quickmod", "TreasureTroll", etc.)

Please note that in order to get the latest source-code you must refresh the SDK content.

To create a mod project:

  1. Open the Source SDK application from Steam's Tools menu.
  2. Open Create A mod from the Utilities group.
  3. Select either modify Half-Life 2 Single Player or modify Half-Life 2 Multiplayer.
  4. Specify where you would like the mod's project to be placed (i.e. C:\Mymod).
  5. Enter the name of your mod. In this example, we'll use "Mymod".
  6. Vista may require extra steps depending on your security settings. (see known issues below)

The Steam application will then copy all the relevant files to the destination directory that you specified. You now have a copy of Half-Life 2's source code at that location which you can modify.

Prepare the solution

  1. Carefully follow the instructions in Compiling in VS2008 or Compiling in VS2005, whichever is appropriate.
  2. Carefully follow the instructions in Launching from Visual Studio.

Alter the rocket's speed

If you are editing Half-life Single-player: in the Solution Explorer, open file Server Episodic > Source Files > HL2 DLL > weapon_rpg.cpp. Otherwise open hl2mp_dll/weapon_rpg.cpp.

Near the beginning of the file, find the line:

#define RPG_SPEED 1500

Change this line to read:

#define RPG_SPEED 400

By altering this number, we've told the rocket to travel at 400 units per second (or about 33 feet per second, or 10 meters per second). Previously the rocket traveled at 1500 units per second (125 ft/s, 38 m/s). The difference in speed will be very apparent when we compile and run the mod.

Compile the project

Now we are ready to build the solution. Choose Build->Build Solution to begin. When the requested compilation has completed there will be a server.dll and client.dll file in the Mymod/bin directory (i.e. C:\Mymod\Mymod\Bin). This is the custom server and client DLL for our mod and is one of the pieces that would be distributed to end-users.

Run the mod

Now before you launch it's important to ensure you have something to launch. By default, new mods will use AppId 215 (Ep1) or 218 (Orange Box). This loads the correct version of Source SDK Base, which is accessible to almost everyone with a Valve game and, in the case of 218/Orange Box, is kept up to date with the latest engine built. You can check or change your AppId in the game's GameInfo.txt file.

SDK Base provides only Half-Life 2's shared content, however. If you want to use content from a specific game, or HL2's maps, you will need to change the AppId to that of the game you want to load from. If you want to 'mount' more than one game you can do so, but you should avoid that method unless you really need it for reasons explained on its page.

In the directory that you created your mod, there will be a batch file called run_mod.bat. Don't use the bat file to launch your mod, as it has not been updated for the SDK Base. The best way to launch your mod is from within Visual Studio by pressing F5, or restart Steam and launch it from your Games list (where it should hopefully have appeared - if not, check your AppId).

No maps are provided with your single player mod, so copy a map from the HL2 maps with GCFScape to your mod's /maps/ directory inside of SteamApps/Sourcemods. Load a map by typing "map " (including the space) into the console and select from the list (e.g. "map d1_canals_01") then type "impulse 101" to give yourself a load of weapons.

Now that there's content to load, start the game up. When the engine has loaded, you'll be taken to the title screen. If you are warned about missing content, such as maplist.txt, ignore it.

In "Options", "Keyboard", and "Advanced", enable the Developer Console. Hit the tilde key (~) to bring up the Developer Console. This console will provide you with numerous tools to help you create content for the Source engine.

Note.pngNote:The console will always appear if -allowdebug is specified as a command line argument/advanced option
Note.pngNote:A map that you can always use and is guaranteed to work is sdk_shader_samples.bsp, found in steamapps\[USER ID]\half-life 2\hl2\maps
Note.pngNote:Enable cheats in the game by typing "sv_cheats 1" in the console (~) for the "impulse 101" command to work

Select and fire the RPG to view the changes you've made.

==

You've built your first mod. Now what?

You’ve now set up, built, and debugged your first mod. Now experiment and tinker. Getting your hands dirty is the best way. If you have coding questions, there is a formal news group called HLCODERS. If you are serious about mod programming, track it down. There are also many communities centered around HL2 and modding, and most have a programming forum, tutorials, etc.

For a stepping stone to different aspects of mapping see What makes a good level?