Skip to content

Developing with Visual Studio Code

JavidPack edited this page Aug 15, 2024 · 19 revisions
  • This guide does not cover Visual Studio, but Visual Studio Code.
  • Visual Studio Code takes less space to store than Visual Studio.
  • Visual Studio Code runs on Windows, Mac, and Linux.
  • If you inexperienced in modding, have space on your computer and are on Windows, it is highly recommended to get Visual Studio instead.
  • This guide is not meant to learn C# fundamentals

Prerequisites

Installation

  1. Download Visual Studio Code.
  2. Install Visual Studio Code by running the downloaded installer. Allow the program to run and accept license agreements as you would normally do. The default options should be suitable.
  3. Install the .NET 8 SDK by downloading the installer, running it, and following the instructions. Download the installer for the latest SDK release labeled "x64" corresponding to your operating system. Don't download the .NET 6.0 or .NET 7.0 versions, those will not work. (Visual Guide)
  4. Launch Visual Studio Code.
  5. Install the C# Dev Kit extension.
    1. You should see something similar to the following: Welcome Page
    2. Click the Extensions button: Button Location
    3. Type C# into the search bar, click the "C# Dev Kit" extension, then click the install button: Install C# Extension Steps
    4. Optional. If you are an experienced user, IntelliCode for C# Dev Kit will be a helpful extension. Install that as well if interested. However, if you are inexperienced, this will just cause confusion and is not recommended: Extension Details
    5. Wait for the installation to complete. The output window will show other extensions being downloaded and installed as it is packaged with C# Dev Kit: Extensions Installed

Creating a Mod

Follow the instructions in Basic tModLoader Modding Guide

Open your Mod Source

  1. It is very important that you installed the extensions mentioned above and .NET 8 SDK listed in Prerequisites.
  2. Open Visual Studio Code
  3. Click Open Folder. If you don't see it, click File->Open Folder. Navigate to %UserProfile%\Documents\My Games\Terraria\ModLoader\Mod Sources\TutorialMod if you're on Windows, or Navigate to ~\Library\Application Support\Terraria\ModLoader\Mod Sources\TutorialMod if you're on Mac, and click Select Folder: Example
  4. You should now see various files available to edit. Open TutorialMod.cs and you should be able to start writing code.
  5. You can create new folders and .cs files by right clicking in empty space in the Explorer pane: New File

Build your Mod

  1. Building your mod is easy. Just open Terminal->New Terminal from the Menu Bar: Location
  2. Once the terminal is open, simply just type dotnet msbuild to build your mod.

Enabling Hot Reload

This feature is disabled by default. You will need to open settings and enable it.

  1. Press Ctrl + , on your keyboard to open settings, then click on the icon on the top right: Location
  2. Inside the curly braces, add these lines:
"csharp.experimental.debug.hotReload": true,
"csharp.debug.hotReloadOnSave": true
  1. Press Ctrl + S to save. Hot Reloading should be enabled.

Common Build Issues

  • If you get an error relating to "project.assets.json not found. Run a NuGet package restore to generate this file.", then go to Terminal-> NewTerminal, run the dotnet restore command in the terminal that appears. Now you can run the build again with dotnet msbuild.

  • If you get an error relating to "System.IO.IOException: The process cannot access the file TutorialMod.tmod because it is being used by another process.", you need to disable the mod in tModLoader and reload mods so that the file can be edited. Closing tModLoader completely is also an option when building.

Why Use Visual Studio Code

Please read the Why Use an IDE page. Most of the features apply to Visual Studio Code. If you aren't seeing errors being underlined or fixes being suggested, you might have skipped installing the C# Dev Kit Extension.

Debugging

Debugging can be achieved by using VS Code's Run and Debug feature.

  1. Click Run and Debug (or press Ctrl + Shift + D): Location
  2. While having a .cs related to the mod opened in the main editor, click Run and Debug button: Big Button
  3. tModLoader should start while enabling your current state of your mod. You can add breakpoints from here and hot reload when necessary and if enabled: Debug Console
  4. To add breakpoints, simply click the red dot left of the line number as so: Red Circle Breakpoint

Issues

Autocomplete, Error Reporting, or IntelliSense not working

Assuming you followed every step in Prerequisites, the most common reason for this error is failing to open your mod's code correctly. Please follow the Open your Mod Source section. Failure to open your mod this way will make using Visual Studio Code counterproductive.

Clone this wiki locally