Modding:Adding Code at Startup: Difference between revisions

Jump to navigation Jump to search
m
Adjusted section order
mNo edit summary
m (Adjusted section order)
Line 40: Line 40:
This is the most flexible method by far, because you can inject your code anywhere in the code flow. However, it is also the most difficult and technical of the options. Most mods do not need this and it is not recommended for modders who are new to C# scripting. For more information about this method, refer to [[Modding:Harmony Injection]].
This is the most flexible method by far, because you can inject your code anywhere in the code flow. However, it is also the most difficult and technical of the options. Most mods do not need this and it is not recommended for modders who are new to C# scripting. For more information about this method, refer to [[Modding:Harmony Injection]].
|}
|}
==Game-Based Cache==
Tag any C# class with the <code>[HasGameBasedStaticCache]</code> attribute to indicate that the class includes game-sensitive code that the game should run each time a new game starts or a save game is loaded.
===Timing===
Game-based cache code is invoked by the <code>XRL.Core.XRLCore.ResetGameBasedStaticCaches()</code> method, which is called at the following times:
* Immediately after selecting "New Game" option from main menu (before the player object exists or world generation occurs)
* Immediately after loading a saved game (including scenarios such as a reload due to Precognition)
===Implementation===
Classes with <code>[HasGameBasedStaticCache]</code> attribute are treated as follows (in this order):
* If the class has any static fields with the <code>[GameBasedStaticCache]</code> attribute, those fields are (re)initialized to their default value (for value types) or set to a new instance via <code>Activator.CreateInstance</code> (for object types). Object types can specify <code>[GameBasedStaticCache(CreateInstance = false)]</code> to force the field to be set to null instead of creating a new instance.
* If the class has a static <code>Reset()</code> method, the game invokes that method.
* If the class has any other static methods with the <code>[GameBasedCacheInit]</code> attribute, the game invokes those methods.
===Example===
<syntaxhighlight lang="csharp">
[HasGameBasedStaticCache]
public static class Initialiser
{
    [GameBasedStaticCache]
    public static int Counter; // Reset to default int value whenever a new game is started or a save is loaded
    public static int OtherCounter; // Value is NOT automatically reset (though you could reset it in your Reset() method)
    public static void Reset()
    {
        // Called whenever a new game is started or a save is loaded - no attribute tag is needed
    }
    [GameBasedCacheInit]
    public static void AdditionalSetup()
    {
        // Called after Reset()
    }
}
</syntaxhighlight>


==Mod-Sensitive Cache==
==Mod-Sensitive Cache==
Line 152: Line 115:
         }
         }
         //...
         //...
    }
}
</syntaxhighlight>
==Game-Based Cache==
Tag any C# class with the <code>[HasGameBasedStaticCache]</code> attribute to indicate that the class includes game-sensitive code that the game should run each time a new game starts or a save game is loaded.
===Timing===
Game-based cache code is invoked by the <code>XRL.Core.XRLCore.ResetGameBasedStaticCaches()</code> method, which is called at the following times:
* Immediately after selecting "New Game" option from main menu (before the player object exists or world generation occurs)
* Immediately after loading a saved game (including scenarios such as a reload due to Precognition)
===Implementation===
Classes with <code>[HasGameBasedStaticCache]</code> attribute are treated as follows (in this order):
* If the class has any static fields with the <code>[GameBasedStaticCache]</code> attribute, those fields are (re)initialized to their default value (for value types) or set to a new instance via <code>Activator.CreateInstance</code> (for object types). Object types can specify <code>[GameBasedStaticCache(CreateInstance = false)]</code> to force the field to be set to null instead of creating a new instance.
* If the class has a static <code>Reset()</code> method, the game invokes that method.
* If the class has any other static methods with the <code>[GameBasedCacheInit]</code> attribute, the game invokes those methods.
===Example===
<syntaxhighlight lang="csharp">
[HasGameBasedStaticCache]
public static class Initialiser
{
    [GameBasedStaticCache]
    public static int Counter; // Reset to default int value whenever a new game is started or a save is loaded
    public static int OtherCounter; // Value is NOT automatically reset (though you could reset it in your Reset() method)
    public static void Reset()
    {
        // Called whenever a new game is started or a save is loaded - no attribute tag is needed
    }
    [GameBasedCacheInit]
    public static void AdditionalSetup()
    {
        // Called after Reset()
     }
     }
}
}

Navigation menu