tModLoader [Help] Call a method from update or equivalent

Da Random

Official Terrarian
I've been having an issue for the past few days and I haven't gotten much progress trying to figure it out.

The issue I'm having is trying to make a method check whether it's daytime or not. However, it only works right after compiling and doesn't get called any other time. I have no clue how to get it to update the local variable
isDayTime. I have looked around a bit in Visual Studio with Intellisense and the documentation though I can't find anything sensible.

The code is copied from the thread Here, written by jopojelly. Essentially what I want it to do is:
  • Change the spawn rate each new day when the sun is rising.
  • And possibly change it each time you entered the world.
What I tried was to get the method
CheckDayTime() to run each time a game-update occurs. Though that did not work. It seems like trying to make a class that derives from Main to then call it from Update() is a bad idea...

I am not very familiar with modding Terraria. Though I do know C# rather well. Any assistance in this thread would be greatly appreciated! :p

Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;

namespace SpawnRate
{
    class SpawnRate : Mod
    {
        public SpawnRate()
        {
            Properties = new ModProperties()
            {
                Autoload = true,
            };
        }
    }

    class SpawnrateMess : GlobalNPC
    {

        private static bool isDayTime = Main.dayTime;
        private static bool changeSpawnrate = true;

        static int spawnRateMult = 200;
        static int maxSpawnsEdit = 200;
        

        // Using it to test if it changes when joining the world
        // It should randomize between 2-200 (inclusive)
        private static int[] test = new int[]
        {
            2,
            200
        };


        public override void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns)
        {
            spawnRate = (int)(spawnRate / spawnRateMult);
            maxSpawns = maxSpawnsEdit;
            
        }



        public static void CheckDayTime(ref bool dayTime)
        {
            isDayTime = dayTime;
            
            if (isDayTime && changeSpawnrate)
            {
                spawnRateMult = Main.rand.Next(201);
                maxSpawnsEdit = 200 - Main.rand.Next(101);
                changeSpawnrate = false;
            }
            else if (!isDayTime)
            {
                changeSpawnrate = true;
            }
            
        }
    }
}
 
I've been having an issue for the past few days and I haven't gotten much progress trying to figure it out.

The issue I'm having is trying to make a method check whether it's daytime or not. However, it only works right after compiling and doesn't get called any other time. I have no clue how to get it to update the local variable
isDayTime. I have looked around a bit in Visual Studio with Intellisense and the documentation though I can't find anything sensible.

The code is copied from the thread Here, written by jopojelly. Essentially what I want it to do is:



    • Change the spawn rate each new day when the sun is rising.
    • And possibly change it each time you entered the world.
What I tried was to get the method CheckDayTime() to run each time a game-update occurs. Though that did not work. It seems like trying to make a class that derives from Main to then call it from Update() is a bad idea...

I am not very familiar with modding Terraria. Though I do know C# rather well. Any assistance in this thread would be greatly appreciated! :p

Code:
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;

namespace SpawnRate
{
    class SpawnRate : Mod
    {
        public SpawnRate()
        {
            Properties = new ModProperties()
            {
                Autoload = true,
            };
        }
    }

    class SpawnrateMess : GlobalNPC
    {

        private static bool isDayTime = Main.dayTime;
        private static bool changeSpawnrate = true;

        static int spawnRateMult = 200;
        static int maxSpawnsEdit = 200;
       

        // Using it to test if it changes when joining the world
        // It should randomize between 2-200 (inclusive)
        private static int[] test = new int[]
        {
            2,
            200
        };


        public override void EditSpawnRate(Player player, ref int spawnRate, ref int maxSpawns)
        {
            spawnRate = (int)(spawnRate / spawnRateMult);
            maxSpawns = maxSpawnsEdit;
           
        }



        public static void CheckDayTime(ref bool dayTime)
        {
            isDayTime = dayTime;
           
            if (isDayTime && changeSpawnrate)
            {
                spawnRateMult = Main.rand.Next(201);
                maxSpawnsEdit = 200 - Main.rand.Next(101);
                changeSpawnrate = false;
            }
            else if (!isDayTime)
            {
                changeSpawnrate = true;
            }
           
        }
    }
}
You would have to do this in a ModWorld.PostUpdate

I guess it would be if(Main.dayTime && Main.time == 0){ change spawnrate here}

dayTime is night or day, and time is 0 at 430 am and 730pm
 
You would have to do this in a ModWorld.PostUpdate

I guess it would be if(Main.dayTime && Main.time == 0){ change spawnrate here}

dayTime is night or day, and time is 0 at 430 am and 730pm
I managed to make it work like I wanted it to. I did add the spawnrate changing on world load, as long as it's daytime. I'll probably add some other features if I really want to. Thanks for the help!

And if you don't mind me asking, can I publish this as a released mod in case any one else wants this specific mod? It's really fun and challenging :D
 
Back
Top Bottom