tModLoader

tModLoader

tModLoader Mods
View and download tModLoader Mods from the Steam Workshop. Please note that the mods listed here are only usable on the default tModLoader branch. Mods usable on 1.3-legacy are found elsewhere.
Learn More
Making a mod but for some reason it won't compile
i keep getting the error "}" expected
using Terraria;
using Terraria.ModLoader;
using thismodisgarbage.Content.Biomes;
using thismodisgarbage.Content.Buffs;
namespace thismodisgarbage.Common.GlobalNPCs
{
public class SpawnRateMultGlobalNPC : GlobalNPC
{
// the edit
public override void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns)
{
// the code here doesn't work for some reason
public float SpawnRateMultiplier = 1;
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(Player) && Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())>=0) {
SpawnRateMultiplier += Player.buffTime[Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600;
}
spawnRate = (int)(spawnRate / SpawnRateMultiplier);
maxSpawns = (int)(maxSpawns * SpawnRateMultiplier);
}
}
}
< >
Showing 1-2 of 2 comments
Snek 20 17 Dec, 2024 @ 6:46am 
You have a few issues.
First, you can't declare a variable inside a method as "public".
public float SpawnRateMultiplier = 1; // wrong float SpawnRateMultiplier = 1; // right

Second, you're using the wrong Player.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(Player) && Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += Player.buffTime[Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; }
This is a little confusing, since "Player" with a capital P might work fine elsewhere (in any subclass of "ModPlayer"). However, in this case, "Player" refers to the type, which only contains data related to all possible players. "EditSpawnRate()" provides a Player instance for you to use called "player" -- you should use this instead.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(player) && player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += player.buffTime[player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; }

Finally, you should be using "Player.InModBiome()" instead of "IsBiomeActive()". tModLoader already calls "IsBiomeActive()" every frame and stores the result, so it's generally better to use that stored result.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(player) && ... // to if (player.InModBiome<NullBiome>() && ...

In all, you "EditSpawnRate()" should look like this:
public override void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns) { float SpawnRateMultiplier = 1; if (player.InModBiome<NullBiome>() && player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += player.buffTime[player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; } spawnRate = (int)(spawnRate / SpawnRateMultiplier); maxSpawns = (int)(maxSpawns * SpawnRateMultiplier); }

Some of these issues are basic C# syntax, so please make sure you brush up on C# so you don't run into these issues again.
Last edited by Snek; 17 Dec, 2024 @ 6:47am
xav07theboi 1 20 Dec, 2024 @ 7:41am 
Originally posted by Snek:
You have a few issues.
First, you can't declare a variable inside a method as "public".
public float SpawnRateMultiplier = 1; // wrong float SpawnRateMultiplier = 1; // right

Second, you're using the wrong Player.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(Player) && Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += Player.buffTime[Player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; }
This is a little confusing, since "Player" with a capital P might work fine elsewhere (in any subclass of "ModPlayer"). However, in this case, "Player" refers to the type, which only contains data related to all possible players. "EditSpawnRate()" provides a Player instance for you to use called "player" -- you should use this instead.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(player) && player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += player.buffTime[player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; }

Finally, you should be using "Player.InModBiome()" instead of "IsBiomeActive()". tModLoader already calls "IsBiomeActive()" every frame and stores the result, so it's generally better to use that stored result.
if (ModContent.GetInstance<NullBiome>().IsBiomeActive(player) && ... // to if (player.InModBiome<NullBiome>() && ...

In all, you "EditSpawnRate()" should look like this:
public override void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns) { float SpawnRateMultiplier = 1; if (player.InModBiome<NullBiome>() && player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>()) >= 0) { SpawnRateMultiplier += player.buffTime[player.FindBuffIndex(ModContent.BuffType<NullSpawnRate>())] / 3600; } spawnRate = (int)(spawnRate / SpawnRateMultiplier); maxSpawns = (int)(maxSpawns * SpawnRateMultiplier); }

Some of these issues are basic C# syntax, so please make sure you brush up on C# so you don't run into these issues again.
thank you very much! i'm new to C# and still learning things as you can see.
Last edited by xav07theboi; 20 Dec, 2024 @ 7:42am
< >
Showing 1-2 of 2 comments
Per page: 1530 50