Standalone [1.3] tModLoader - A Modding API

public override bool UseItem(Player player)
{
if (Main.rand.Next(2) == 0)
{
player.QuickSpawnItem(mod.ItemType("SparkyCard"));
}
else if (Main.rand.Next(2) == 1)
{
player.QuickSpawnItem(mod.ItemType("LumberJacksAxe"));
}
else if (Main.rand.Next(2) == 2)
{
player.QuickSpawnItem(mod.ItemType("PrincessesBow"));
}
return true;

}
[/CODE]

It is because you are rolling the dice each time you are checking it. The first time will not always be 0, so no SparkyCard, the second time will not always return 1, so no LumberJacksAxe, and the third time will NEVER be 2, because Next() returns 0 to n - 1, ie Next(2) will return 0 or 1.

Call Next(3) once and store it in a variable, ie int result = Main.rand.Next(3);, and then check that variable in your if statements.
 
It is because you are rolling the dice each time you are checking it. The first time will not always be 0, so no SparkyCard, the second time will not always return 1, so no LumberJacksAxe, and the third time will NEVER be 2, because Next() returns 0 to n - 1, ie Next(2) will return 0 or 1.

Call Next(3) once and store it in a variable, ie int result = Main.rand.Next(3);, and then check that variable in your if statements.
I'm sorry for asking this but how do I set that up?
Code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EpicnessModRemastered.Items
{
    public class SuperMagicalChest : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Super Magical Chest";
            item.maxStack = 1;
            item.consumable = true;
            item.width = 24;
            item.height = 24;
            item.useTime = 10;
            item.useAnimation = 10;
            item.useStyle = 4;
            item.toolTip = "Left click to open";
            item.rare = 10;
            item.useSound = mod.GetSoundSlot(SoundType.Item, "Sounds/Item/Legendaryget");
        }
        public override bool UseItem(Player player)
        {
            (Main.rand.Next(3) == int result Main.rand.Next(3));,
            if (int result Main.rand.Next(3); == 0)
            {
            player.QuickSpawnItem(mod.ItemType("SparkyCard"));
            }
            else if (int result Main.rand.Next(3); == 1)
            {
            player.QuickSpawnItem(mod.ItemType("LumberJacksAxe"));
            }
            else if (int result Main.rand.Next(3); == 2)
            {
            player.QuickSpawnItem(mod.ItemType("PrincessesBow"));
            }
            return true;
           
        }
    }
}
 
I'm sorry for asking this but how do I set that up?
Code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EpicnessModRemastered.Items
{
    public class SuperMagicalChest : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Super Magical Chest";
            item.maxStack = 1;
            item.consumable = true;
            item.width = 24;
            item.height = 24;
            item.useTime = 10;
            item.useAnimation = 10;
            item.useStyle = 4;
            item.toolTip = "Left click to open";
            item.rare = 10;
            item.useSound = mod.GetSoundSlot(SoundType.Item, "Sounds/Item/Legendaryget");
        }
        public override bool UseItem(Player player)
        {
            (Main.rand.Next(3) == int result Main.rand.Next(3));,
            if (int result Main.rand.Next(3); == 0)
            {
            player.QuickSpawnItem(mod.ItemType("SparkyCard"));
            }
            else if (int result Main.rand.Next(3); == 1)
            {
            player.QuickSpawnItem(mod.ItemType("LumberJacksAxe"));
            }
            else if (int result Main.rand.Next(3); == 2)
            {
            player.QuickSpawnItem(mod.ItemType("PrincessesBow"));
            }
            return true;
          
        }
    }
}

i normally use right click but thats just me
he means something like this

------------------------------------------------------------ this is the right click function
public override bool CanRightClick()
{
return true;
}

public override void RightClick(Player player)
------------------------------------------------------------
{
int f = Main.rand.Next(0, 5);
if(f >= 0 && f <= 0)
{
player.QuickSpawnItem(mod.ItemType("item1"));
}
else if(f >= 1 && f <= 1)
{
player.QuickSpawnItem(mod.ItemType("item2"));
}
else if(f >= 2 && f <= 2)
{
player.QuickSpawnItem(mod.ItemType("item3"));
}
else if(f >= 3 && f <= 3)
{
player.QuickSpawnItem(mod.ItemType("item4"));
}
else if(f >= 4 && f <= 4)
{
player.QuickSpawnItem(mod.ItemType("item5"));
}
}
 
i normally use right click but thats just me
he means something like this

------------------------------------------------------------ this is the right click function
public override bool CanRightClick()
{
return true;
}

public override void RightClick(Player player)
------------------------------------------------------------
{
int f = Main.rand.Next(0, 5);
if(f >= 0 && f <= 0)
{
player.QuickSpawnItem(mod.ItemType("item1"));
}
else if(f >= 1 && f <= 1)
{
player.QuickSpawnItem(mod.ItemType("item2"));
}
else if(f >= 2 && f <= 2)
{
player.QuickSpawnItem(mod.ItemType("item3"));
}
else if(f >= 3 && f <= 3)
{
player.QuickSpawnItem(mod.ItemType("item4"));
}
else if(f >= 4 && f <= 4)
{
player.QuickSpawnItem(mod.ItemType("item5"));
}
}
Why are you not just checking f for equality (f == 0)? Isn't that a lot easier?
I'm sorry for asking this but how do I set that up?
Code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace EpicnessModRemastered.Items
{
    public class SuperMagicalChest : ModItem
    {
        public override void SetDefaults()
        {
            item.name = "Super Magical Chest";
            item.maxStack = 1;
            item.consumable = true;
            item.width = 24;
            item.height = 24;
            item.useTime = 10;
            item.useAnimation = 10;
            item.useStyle = 4;
            item.toolTip = "Left click to open";
            item.rare = 10;
            item.useSound = mod.GetSoundSlot(SoundType.Item, "Sounds/Item/Legendaryget");
        }
        public override bool UseItem(Player player)
        {
            (Main.rand.Next(3) == int result Main.rand.Next(3));,
            if (int result Main.rand.Next(3); == 0)
            {
            player.QuickSpawnItem(mod.ItemType("SparkyCard"));
            }
            else if (int result Main.rand.Next(3); == 1)
            {
            player.QuickSpawnItem(mod.ItemType("LumberJacksAxe"));
            }
            else if (int result Main.rand.Next(3); == 2)
            {
            player.QuickSpawnItem(mod.ItemType("PrincessesBow"));
            }
            return true;
       
        }
    }
}
Good start, but I'll explain where and why you went wrong.

What you need to do is to create a variable at the start of UseItem, and then you can do whatever you want with that variable until UseItem is finished (then it's discarded). So first, to declare a variable, you need to use the format <type - identifier - assignment (optional)>. This means, from left to right, what kind of variable you want, what it's called, and what value you want it to start it with respectively.

Because you want an integer (whole number) called result, you should type int result;. But because you want to put a random number in it, you have to assign (=) the result of the game's random number generator (Main.rand.Next(5), which means you are generating one of five different numbers (0, 1, 2, 3 and 4)). So that becomes:
Code:
int result = Main.rand.Next(5);
Now you have that value, you can check your if-statements against it, like so:
Code:
if (result == 0)
{
  // Stuff.
}
else if (result == 1)
{
  // Stuff.
}
// And so on.
You don't have to use int anymore, because the script remembers that result is an integer; if you use int again, the code thinks you're trying to make a new variable with the same name, which isn't allowed. You also don't have to use Main.rand.Next() anymore, because you only need to assign that random value once.

Hope that clears things up for you. :)
 
The world I want to use on vanilla Terraria only has 3 files for some reason (.twld, .twld.bak, and wld.bak). The .wld file is missing. All my other worlds made in TModLoader have .wld files.
Something must have happened to the *.wld file, but thankfully you have a backup. Copy the *.wld.bak file to the vanilla worlds folder and remove .bak from the filename. Hopefully that will work.

When I compile I get the error "error CS0117: 'Terraria.ModLoader.ModNPC' does not contain a definition for 'Center'"
I'm guessing a bit here because I can't see the context, but I think you need 'ModNPC.npc.Center' not 'ModNPC.Center'. The ModNPC is kind of a parent class to the npc, so you need to refer to the child npc class.
 
An unexpected error has occurred.
here: Microsoft.Xna.Framework.Graphics.Texture2D..ctor(GraphicsDevice graphicsDevice, Stream stream, Int32 width, Int32 height, XnaImageOperation operation)
here: Terraria.ModLoader.Mod.Autoload()
here: Terraria.ModLoader.ModLoader.do_Load(Object threadContext)
Fix????
 
I think I'm doing everything right...

"A problem was encountered while installing!
java.nio.file.AccessDeniedException: C:\Program Files\Steam\steamapps\common\Terraria\Terraria.exe"

Help?
 
Something must have happened to the *.wld file, but thankfully you have a backup. Copy the *.wld.bak file to the vanilla worlds folder and remove .bak from the filename. Hopefully that will work.


I'm guessing a bit here because I can't see the context, but I think you need 'ModNPC.npc.Center' not 'ModNPC.Center'. The ModNPC is kind of a parent class to the npc, so you need to refer to the child npc class.
I'm doing a spin-off EOC boss, just playing around with vanilla AIs to get more comfortable with AI stuff.
Notably these lines:
if (base.Center.X > Main.player[npc.target].Center.X)
{
num277 *= -1f;
}
if (base.Center.Y > Main.player[npc.target].Center.Y)
{
num276 *= -1f;
}
 
They just pushed the smallest little do-nothing update they ever have. TmodLoader breaks again.... >:|

edit: On the other hand, ticking to the new version should be super easy.
 
They just pushed the smallest little do-nothing update they ever have. TmodLoader breaks again.... >:|

edit: On the other hand, ticking to the new version should be super easy.
It seems to work fine for me with the new update. I updated, installed gamelauncher and tmodloader and it was fine.
 
No. But it still works for me.
It might be something else on your side.
I was just playing modded terraria and then it stopped working. Then when I tried to launch the game again steam told me that there was an update for the game and then when it finished with that I entered just vanilla terraria
 
I was just playing modded terraria and then it stopped working. Then when I tried to launch the game again steam told me that there was an update for the game and then when it finished with that I entered just vanilla terraria
Install tmodloader again. It should still work.
(i would reccomend backing up your worlds and players, just in case.)
 
Back
Top Bottom