[SOLVED] Executing code at game start.

Started by Viceroy, March 01, 2015, 12:44:43 PM

Previous topic - Next topic

Viceroy

Hey guys, Is there an elegant way to ensure some code gets executed at map start? Like say for instance I to instantiate something on map start that you gotta defend or I want to create an abstract class at game start for something like a noise generator and then feed those values to other things instead of constantly regenerating the noise over and over again.
Dog goes moo!

Viceroy

Ah nevermind guys I figured it out looking at CaveFlora. What a great mod btw.
In case anyone is wondering or searches and finds this, here is the solution:

Create a MapComponent class like so:

namespace Rimworld
{
    public class MapComponent_Custom : MapComponent
    {
        public override void MapComponentTick()
        {
            // CODE TO EXECUTE EVERY TICK
            // On map initialization treatment.
            if (Find.TickManager.TicksGame == 1)
            {
               // CODE TO EXECUTE ON GAME START
            }
        }
    }
}


As easy as that, you can create other classes and then just create and invoke them from a central mapcomponent. Pretty neat.
Dog goes moo!

Famous Shoes

Interesting. I would have first thought to go straight to Unity, e.g., listening for Start or OnLevelWasLoaded. I've been wondering about this same thing; an approach that doesn't put yet more pressure on ticks would be nice if someone knows of one.