tModLoader Making a weapon create an item

Saobie

Terrarian
Title. Basically i want to make a weapon that has a chance to create an item upon use

More specifically, im trying to make a spell tome that, more or less, allows a player to spend mana for a chance to get gold coins (1/140 chance for 1 Gold Coin. 10 mana cost)
 
Put this into the item class of your spell tome.
Code:
public override bool UseItem(Player player)
        {
            if(Main.rand.Next(1,140) == 50)
                player.QuickSpawnItem(ItemID.GoldCoin);
            return base.UseItem(player);
        }

And to make it cost mana. WIthin your setDefaults() void you would add
Code:
item.mana = 10;
 
Tyvm for the response. Ill have to hope i remember to transfer the document i put that code into when i switch computers (on a temp laptop)

So the part that says (1, 140) == 500 what exactly does that mean? Ik the 1, 140 pretty much equals 1 in 140 chance to proc, but whats the == 50 for? Just curious
 
The "== 50" means that whenever the random number generated between 1 and 140 is 50, it will drop the coin. Effectively giving a 1 in 140 chance of spawning a gold coin.
We can test this is by using a small little block of code.
Code:
            Random rand = new Random();
            int yes = 0;
            const int iterations = 10000000;
            for (int i = 0; i < iterations; i++)
            {
                if (rand.Next(1, 141) == 50)
                {
                   yes++;
                }
            }
            Console.WriteLine((float)yes/iterations);
This is a snippet I got from a slightly modified block I got from a StackOverflow page. So what it will do is generate a random number 10,000,000 times and count how many times it is equal to 50. In a perfect world this would get us the same value as dividing 1/140, but it isn't. Turns out when I ran it myself I got an answer of 0.0071707. And if we do a quick reference check on google for that 1/140 is we get 0.0071428. So as you can see your percent chances are almost the same. Only being off by about 0.00003% which is more than enough accuracy for 99.99% of things. And is most certainly accurate enough for modding Terraria.

Edit: The code should be (1, 141) not (1, 140) in response I sent before
 
Last edited:
Ah. So changing he 50 to any other number will basically do nothing huh

What if i added another number after 50. Is that doable? ike == 50, 82 or something like that. Just curious if thats a thing
 
You can do that, but you would have to split it up into something like
Code:
if(Main.rand.Next(1,141) == 50 || Main.rand.Next(1,141) == 84)
The two vertical bars stand as a "or" operator, so if you were to read that out loud it would say. "If the random number is equal to 50 OR if another random number is equal to 84, then do something". This would basically double the chances of getting the gold drop to be 2/140, or 1/70, or 0.0142857% because of the two chances. So in the case you wanted to do this, you could simplify your if statement to just be
Code:
if(Main.rand.Next(1,71) == 50)
 
Last edited:
The reason i asked is mostly so i can make an equal chance for something bad to happen, a 1/140 chance for the player to be directly dealt 200, 350, and 500 damage (Depending on tier of Create spell tome used)
 
To do that you would just need two if statements like
Code:
if(Main.rand.Next(1,141) == 50
Though I am not entirely sure what the code would be to deal damage to the player on item use.
 
But, in that case, there's also a tiny chance that both effects trigger. To fix that, he should generate the random number before getting into the if statements, right?
 
Im ok with the possibility of both effects triggering, especially since those odds would be so minute it likely wont actually happen unless we spent a while farming with this spell tome
 
Back
Top Bottom