tModLoader How to set up your mod using Visual Studio (MVS)

Jofairden

Duke Fishron
tModLoader
Set up your tModLoader mods using Microsoft Visual Studio
Last update: 26th of September, 2018

kdcROYP.png


Table of contents:

1. Prerequisites
2. Introduction
3. How-to setup your project in MVS
4. How-to setup your project files

kdcROYP.png


Prerequisites
tl;dr

Make sure you have....
  • ...the latest tModLoader installed (offical thread)
  • ...the latest Microsoft .NET Framework installed (offical .net site)
  • ...the Microsoft XNA Framework installed (you have this if you are able to play Terraria)
  • ...the Microsoft XNA Framework files ready (download attachment)
  • ...Microsoft Visual Studio (community = free) installed with the C# workspace (official MVS site)
  • ...C# knowledge
kdcROYP.png


Introduction
tl;dr
  • MVS is a workspace for many different (Windows) languages, such as C#. It also has IntelliSense built in which can auto-complete sentences and find and display relationships for you. It will also check your code for errors.
  • In this tutorial we work with Microsoft Visual Studio Community 2015, so some instructions below might be a little bit different if you use a different version.
  • This tutorial is for people with C# knowledge. Please follow basic C# tutorials before you attempt this.

kdcROYP.png

THIS IS THE FASTEST WAY
tl;dr


  • Go here: http://javid.ddns.net/tModLoader/generator/ModSkeletonGenerator.html
  • Enter the required details. Do NOT use spaces for the mod name.
  • Unzip the zip to the Terraria documents
    • Mac: /Users/account/Library/Application Support/Terraria/ModLoader/Mod Sources
    • Linux: ~/.local/share/Terraria OR $XDG_DATA_HOME/Terraria/ModLoader/Mod Sources
    • Windows: %UserProfile%\Documents\My Games\Terraria\ModLoader\Mod Sources
  • The build.txt is messed up, use the following and change to your needs:
  • Code:
    author = bluemagic123
    version = 0.8.1
    displayName = Example Mod
    homepage = http://forums.terraria.org/index.php?threads/1-3-tmodloader-a-modding-api.23726/
    hideCode = false
    hideResources = false
    includeSource = true
    buildIgnore = *.csproj, *.user, obj\*, bin\*, .vs\*
    includePDB = true
  • Start working on your mod!
  • You can use the .csproj file to work using Microsoft Visual Studio

How-to setup your solution (all your mods) in MVS

tl;dr

FULL SETUP (SOLUTION + PROJECT)
  • File --> New --> Project (CTRL + SHIFT + N)
  • Templates --> Other Project Types --> Visual Studio Solutions --> Blank Solution
    • ! Name: Mod Sources
    • Locations: c:\users\yourusernamehere\documents\My Games\Terraria\ModLoader
    • Uncheck: Add to source control
  • Open the Solution Explorer
    • View --> Solution Explorer (CTRL + W, S)
  • Right click Mod Sources --> Add --> New Project --> Visual C# --> Windows --> Classic Desktop --> Empty Project
    • Name: Whatever the name of your mod is
    • Location: leave the location as is
  • Add the Example Mod (from the original tML thread) to the folder of your project (documents/My Games/ModLoader/Mod Sources/ExampleMod)
    • Click the 'Show all Files' button in the Solution Explorer
    • Right click file --> Include file in project
  • Rename classes, namespaces, filenames etc. to your needs
  • Uncollapse your project, right click 'References' --> Add Reference --> Browse --> Select XNA Framework .dll Files and Terraria.exe
  • Before you hit 'OK' make sure the checkbox behind the files are checked!
  • If your mod uses any additional dllReferences (Documents\My Games\Terraria\ModLoader\dllReferences) you will need to include those files the same way
SAVING YOUR MOD (PROJECT)
  • File --> Save (CTRL + S) or File --> Save All (CTRL + SHIFT + S)
  • Build your mod using the in-game menu
ADDING A NEW MOD (PROJECT)
  • Right click Mod Sources --> Add --> New Project --> Visual C# --> Windows --> Classic Desktop --> Empty Project
    • Name: Whatever the name of your mod is
    • Location: leave the location as is
ADDING A NEW REFERENCE
  • Right click 'References' --> Add Reference --> Browse --> Select your reference
kdcROYP.png

Final words

Note for MVS 2015: Also creates an invisible .vs folder inside which will show on the build tab, I could ask @bluemagic123 to remove it from the build list but for now simply ignore it.
Instead, you can now list the full .vs in your buildIgnore property inside build.txt like so: buildIgnore = .vs\*

This tutorial is mainly for setting up your mod in MVS, not how to create stuff. So I only included the basics here: your main file and some item .cs files.
Once tModLoader becomes bigger I will create seperate tutorials on how to create stuff. Don't bother asking too much about that here.

Get started with C# (tutorials): (some things I used!)
http://www.tutorialspoint.com/csharp/
http://csharp.net-tutorials.com/
http://www.homeandlearn.co.uk/csharp/csharp.html
Brackeys Youtube C# Series (series)

Special thanks:
  • @blushiemagic (original author of tML)
  • Terraria team for these awesome header seperators
  • Microsoft for the creation of MVS
That's all there's to it! Now go build your awesome mod! ;)
If you have any questions, feel free to ask! Make sure you know about C# before asking dumb questions! Really!

- Jofairden
 

Attachments

  • XNA_Framework.zip
    404 KB · Views: 4,609
Last edited:
The type or namespace name 'Items' does not exist in the namespace 'MyMod' (are you missing an assembly reference?)
How can i fix this Error?
Edit: I already created a folder named Items inside of the Project
 
The type or namespace name 'Items' does not exist in the namespace 'MyMod' (are you missing an assembly reference?)
How can i fix this Error?
Edit: I already created a folder named Items inside of the Project

Yes, item .cs files and images should be within a seperate 'Items' folder.
 
I think you misunderstood me im getting this Error and i dont know how to fix it.

Can you show me the code you're using? If you have your .cs in an 'Items' folder you use a different namespace.

For example my main .cs file has this namespace: namespace MySuperCoolMod
then my items .cs will use this namespace: namespace MySuperCoolMod.Items

Also make sure you're using the right namespaces at the top of your file, this is what I have in my items .cs files:

Code:
using System;
using Terraria;
using Terraria.ModLoader;
 
Code:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using RhenosMod.Items; // the error is here

namespace RhenosMod
{
    public class RhenosMod : Mod
    {
        public override void SetModInfo(out string name, ref string version, ref string author)
        {
            name = "Rheno's Mod";
            version = "v0.1";
            author = "Rhenomaster";
        }

        public override void Load()
        {
        }

        public override void AddRecipes()
        {

        }
    }
}

    }
0a158296860bbe44a0cbe828babe3fba.png
 
Is that your full code? You're missing a lot in that case. Copy this full code:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using ExampleMod.Items;

namespace ExampleMod {
public class ExampleMod : Mod
{
public override void SetModInfo(out string name, ref string version, ref string author)
{
name = "Example Mod";
version = "v0.1";
author = "bluemagic123";
}

public override void Load()
{
SetGlobalItem(new ExampleModItem());
AddItem("Example Sword", new ExampleSword(), "ExampleMod/Items/ExampleSword");
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.DirtBlock, 999);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.LifeCrystal, 15);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.ManaCrystal, 9);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(null, "Example Sword");
recipe.AddRecipe();
}
}}

Change the ExampleMod name to what you named your mod, also note this:
Code:
AddItem("Example Sword", new ExampleSword(), "ExampleMod/Items/ExampleSword");

You need to change ExampleMod here as well to whatever you called your mod and .cs file for it.
 
Is that your full code? You're missing a lot in that case. Copy this full code:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using ExampleMod.Items;

namespace ExampleMod {
public class ExampleMod : Mod
{
public override void SetModInfo(out string name, ref string version, ref string author)
{
name = "Example Mod";
version = "v0.1";
author = "bluemagic123";
}

public override void Load()
{
SetGlobalItem(new ExampleModItem());
AddItem("Example Sword", new ExampleSword(), "ExampleMod/Items/ExampleSword");
}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.DirtBlock, 999);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.LifeCrystal, 15);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(ItemID.ManaCrystal, 9);
recipe.AddRecipe();

recipe = new ModRecipe(this);
recipe.AddIngredient(ItemID.DirtBlock);
recipe.SetResult(null, "Example Sword");
recipe.AddRecipe();
}
}}

Change the ExampleMod name to what you named your mod, also note this:
Code:
AddItem("Example Sword", new ExampleSword(), "ExampleMod/Items/ExampleSword");

You need to change ExampleMod here as well to whatever you called your mod and .cs file for it.

Thank you it was fixed as soon as i put a class into the items folder.
 
You didn't have that? Of course you need that. You're creating a new sword:
new ExampleSword()
refers to your ExampleSword class
 
Since people seem to have issues setting up the actual files as well I'll cover that then as well
 
Decided to come check this out, great tutorial. One thing that I think may be an issue is that you instruct to install the Community version of Video Studio which I don't believe has the ability to create a blank project like Visual Studio Express, only blank solutions. I may be wrong but I am pretty sure that this is the case. Why it is the case? I don't know.
 
Decided to come check this out, great tutorial. One thing that I think may be an issue is that you instruct to install the Community version of Video Studio which I don't believe has the ability to create a blank project like Visual Studio Express, only blank solutions. I may be wrong but I am pretty sure that this is the case. Why it is the case? I don't know.
I got the community version of Visual Studio and I can create empty projects as well as blank solutions. An empty project is the same as a blank project, right? So that shouldn't be a problem.
 
I got the community version of Visual Studio and I can create empty projects as well as blank solutions. An empty project is the same as a blank project, right? So that shouldn't be a problem.
Ah, okay, I had it on my other computer and I couldn't find it. Might I ask where did you find it because I am having troubles on Visual Studio Community because it is not in the same location as it would be on Express.
 
Ah, okay, I had it on my other computer and I couldn't find it. Might I ask where did you find it because I am having troubles on Visual Studio Community because it is not in the same location as it would be on Express.
Right click on your blank solution -> Choose New Project -> Go to Visual C#/Windows Desktop -> You should see the Empty Project in the list ->Choose it and there you got your Empty Project
 
Decided to come check this out, great tutorial.

Thank you, I hope this helps people.

I got the community version of Visual Studio and I can create empty projects as well as blank solutions. An empty project is the same as a blank project, right? So that shouldn't be a problem.

You should be able to do everything you need with free MVS versions. Once you start making apps for tablets/android blabla it starts complaining.

I might also make seperate tutorials if needed on how to create 'stuff' with tModLoader once it gets some more options (like armor next update and tiles)
 
Last edited:
One question(it's a hard one): In Visual Studio you can choose to show all files in the solution explorer. If you choose to show all files you will see the .png files(your sprites). The are only visible when I choose to show all files but then I see other files too like bin and obj. How can I choose which files will be visible all the time?
 
Back
Top Bottom