by 3saster » Sat May 23, 2020 8:37 pm
Using a StaticEventhandler for initialization like that is totally fine. In fact,
the wiki even suggests doing that to make certain types of global variables. That commit seems to be about using OnRegister for a non-static eventhandler, which can indeed cause some problems when loading a savegame. Using OnRegister in a StaticEventHandler should be safe, because it exists outside of the scope of save games. That makes it a double edged sword for things like this however; content inside StaticEventHandlers is NOT stored across different play sessions. Keep that if something is initialized in WorldLoaded, then when it is reloaded, WorldLoaded won't be called again, but everything from it already happened. In other words, if I do
Code: Select all
class TestHandler : EventHandler
{
private int x;
override void WorldLoaded (WorldEvent e)
{
console.printf("init");
x = 5;
}
override void WorldTick()
{
console.printf("%d",x);
}
}
5 will always be printed even if I load a savegame, because the value of x is stored in that savegame.
Basically, you can safely use StaticEventhandlers for initialization, but think about what you want to do with the handler. StaticEventhandlers should be used for things that exist outside the scope of a particular instance of the game (if that makes sense); EventHandlers are for things that do depend on a particular instance.
Using a StaticEventhandler for initialization like that is totally fine. In fact, [url=https://zdoom.org/wiki/ZScript_global_variables]the wiki even suggests doing that to make certain types of global variables[/url]. That commit seems to be about using OnRegister for a non-static eventhandler, which can indeed cause some problems when loading a savegame. Using OnRegister in a StaticEventHandler should be safe, because it exists outside of the scope of save games. That makes it a double edged sword for things like this however; content inside StaticEventHandlers is NOT stored across different play sessions. Keep that if something is initialized in WorldLoaded, then when it is reloaded, WorldLoaded won't be called again, but everything from it already happened. In other words, if I do
[code]class TestHandler : EventHandler
{
private int x;
override void WorldLoaded (WorldEvent e)
{
console.printf("init");
x = 5;
}
override void WorldTick()
{
console.printf("%d",x);
}
}[/code]
5 will always be printed even if I load a savegame, because the value of x is stored in that savegame.
Basically, you can safely use StaticEventhandlers for initialization, but think about what you want to do with the handler. StaticEventhandlers should be used for things that exist outside the scope of a particular instance of the game (if that makes sense); EventHandlers are for things that do depend on a particular instance.