ezEngine  Milestone 7
ReloadableVariable.h File Reference
#include <Foundation/Utilities/EnumerableClass.h>
#include <Foundation/IO/MemoryStream.h>
#include <Foundation/Containers/Map.h>
#include <Foundation/Strings/String.h>
#include <Foundation/Configuration/Implementation/ReloadableVariable_inl.h>

Go to the source code of this file.

Classes

class  ezReloadableVariableBase
 [internal] A helper class to implement persistent global variables during plugin reloading. More...
 
class  ezReloadableVariableWrapper< Type >
 [internal] Helper class, derived from ezReloadableVariableBase, to implement type specific reloading of global variables. More...
 

Macros

#define EZ_MAKE_RELOADABLE(VarType, Variable)   static ezReloadableVariableWrapper<VarType> EZ_CONCAT(g_ReloadableVar, EZ_SOURCE_LINE) (Variable, EZ_STRINGIZE(EZ_CONCAT(VarType, Variable)));
 Allows to wrap a global/static variable such that its state will be preserved during plugin reloading. More...
 

Macro Definition Documentation

#define EZ_MAKE_RELOADABLE (   VarType,
  Variable 
)    static ezReloadableVariableWrapper<VarType> EZ_CONCAT(g_ReloadableVar, EZ_SOURCE_LINE) (Variable, EZ_STRINGIZE(EZ_CONCAT(VarType, Variable)));

Allows to wrap a global/static variable such that its state will be preserved during plugin reloading.

Using this macro, global and static variables inside plugins can be made 'reloadable', when plugins are unloaded and loaded again. That means their last value is stored when a plugin is unloaded, and it is restored when that same plugin is loaded again, before anyone can access the variable. It happens fully automatically. All that the variable type needs to support, is serialization to a binary stream.
To make a variable reloadable, just use the macro directly after the variable definition, like so:
float g_fValue = 0.0f.
EZ_MAKE_RELOADABLE(float, g_fValue);

Note
The state of the reloadable variables is only made persistent over the lifetime of the application and thus only restored when a plugin is reloaded. It is not stored on disk and can thus not be restored when the application is restarted.
This mechanism should be used rarely. It is meant to help in the few cases that one has global variables that need to stay unchanged while code is being reloaded and it cannot be recomputed during plugin initialization. In such cases writing state to disk might be contra-productive, as that might result in state being restored from a file, even though a plugin has actually been loaded for the first time (the save file might still exist due to a crash). Still, try to avoid global variables in general, and only use reloadable variables, if there is no better solution.