tModLoader Problems with Making the Player Spawn with Items

SealDev

Terrarian
I am at a loss for words here because all I wanted to do was make the player spawn with a modded gun and an infinite ammo pouch. The first problem I had was the game saying something along the lines of IList not being recognized, even though every tutorial and piece of reference code I see uses that.

Now I don't get that error, but now the script won't run at all. What do I do here? NOTE: I have tried looking at examplemod and the tmodloader documentation. It did not help at all.

Code:

using Microsoft.Xna.Framework;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria;
using System.Collections.Generic;

namespace GodofGlocks.Content.Inventory
{
public class InventorySetup : ModPlayer
{
public override void SetupStartInventory(IList<Item> items)
{
Item item = new Item();
Item item2 = new item();
item.SetDefaults(ItemID.Glock17000);
item2.SetDefaults(ItemID.EndlessMusketPouch);
items.Add(item);
items.Add(item2)
}
}
}
 
It seems you are looking at old examples as ModPlayer.SetupStartInventory() is something that only exists in 1.3. If you want to add items to the inventory of new characters, then use the 1.4.4 ModPlayer.AddStartingItems() like this:
C#:
public override IEnumerable<Item> AddStartingItems(bool mediumCoreDeath)
{
    yield return new Item(ItemID.EndlessMusketPouch);
    yield return new Item(ModContent.ItemType<Glock17000>());
}
Replace your current SetupStartInventory() with this one and it might work. Since there is no "Glock17000" in Terraria, I'm assuming it to be an item you've made yourself. In the case of mod items, you need to use ModContent.ItemType<MyModItemClass>() (Remember to replace "MyModItemClass" with the name of the custom item's class!) instead of ItemID since ItemID only has the base game's items IDs.

Functions that return an IEnumerable<> let you use the yield return. What exactly yield return does is a bit mystical, but in this case, you may think of it as there being a hidden list that you can add elements to, by using the yield return, that is the value returned when the function finishes.
 
Last edited:
It seems you are looking at old examples as ModPlayer.SetupStartInventory() is something that only exists in 1.3. If you want to add items to the inventory of new characters, then use the 1.4.4 ModPlayer.AddStartingItems() like this:
C#:
public override IEnumerable<Item> AddStartingItems(bool mediumCoreDeath)
{
    yield return new Item(ItemID.EndlessMusketPouch);
    yield return new Item(ModContent.ItemType<Glock17000>());
}
Replace your current SetupStartInventory() with this one and it might work. Since there is no "Glock17000" in Terraria, I'm assuming it to be an item you've made yourself. In the case of mod items, you need to use ModContent.ItemType<MyModItemClass>() (Remember to replace "MyModItemClass" with the name of the custom item's class!) instead of ItemID since ItemID only has the base game's items IDs.

Functions that return an IEnumerable<> let you use the yield return. What exactly yield return does is a bit mystical, but in this case, you may think of it as there being a hidden list that you can add elements to, by using the yield return, that is the value returned when the function finishes.
Ok so that could have worked but I still have the issue of the game not running the script. How do I fix that?
 
Sorry if I've miss-understood, but did you mean that the class/function you posted successfully compiles without error after fixing the "IList" issue? What version of tModLoader are you using?
 
Sorry if I've miss-understood, but did you mean that the class/function you posted successfully compiles without error after fixing the "IList" issue? What version of tModLoader are you using?
Im using the latest version, and it doesn't cause any errors. The problem is that the script either doesn't run or the code does nothing
 
Try going from the main menu to: "Workshop", "Develop Mods", then clicking on "Build + Reload" under the mod you are working on
 
Try going from the main menu to: "Workshop", "Develop Mods", then clicking on "Build + Reload" under the mod you are working on
Just did that and it did nothing
 
You just said you were on the latest version and that what you posted compiled without error. This is impossible since ModPlayer.SetupStartInventory() does not exist latest version, and, therefore, is guaranteed to cause errors.
Also note that ModPlayer.AddStartingItems() only gives items to characters that were just created and does not give items on entering a world, meaning you have to create a new character every time you want to test it
 
You just said you were on the latest version and that what you posted compiled without error. This is impossible since ModPlayer.SetupStartInventory() does not exist latest version, and, therefore, is guaranteed to cause errors.
Also note that ModPlayer.AddStartingItems() only gives items to characters that were just created and does not give items on entering a world, meaning you have to create a new character every time you want to test it
I am using the code you said to change it to. Here is the current code.

using Microsoft.Xna.Framework;
using Terraria.GameContent.Creative;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria;
using System.Collections.Generic;

namespace GodofGlocks.Content.Items
{
public class InventorySetup : ModPlayer
{
public override IEnumerable<Item> AddStartingItems(true)
{
yield return new Item(ItemID.EndlessMusketPouch);
yield return new Item(ModContent.ItemType<Glock17000>());
}
}
}

Right now there are no errors but the script just flat out is not being run. The mod is only running the weapon script.
 
Why does it just say true for the AddStartingItems() parameters? It should be a variable definition matching the original function's type of bool, like bool isMediumCoreSpawn. If there are really no errors from compiling, then the issue probably has very little do with the contents of the file, but rather how your mod/project is setup as the file is not being compiled at all!
The only potential issue I can think of right now is that the file isn't in the mod's directory, and, therefore, is not being compiled. Though, I cannot imagine this to be the case.
 
Why does it just say true for the AddStartingItems() parameters? It should be a variable definition matching the original function's type of bool, like bool isMediumCoreSpawn. If there are really no errors from compiling, then the issue probably has very little do with the contents of the file, but rather how your mod/project is setup as the file is not being compiled at all!
The only potential issue I can think of right now is that the file isn't in the mod's directory, and, therefore, is not being compiled. Though, I cannot imagine this to be the case.
Ok I found the problem with the code not working. Turns out it did not have .cs at the end of the file name (no idea how that one even happened), so I copy/pasted the code into a new file and its running the code. And I fixed the bool part and now it works
 
Back
Top Bottom