For context, I’m creating a voxel game and I am trying to figure out the best way to set up a variable that everyone needs access to called UVSize. Its a derived variable from another called TextureTiling. TextureTiling is an FIntPoint describing the rows and columns to be used when accessing a section of a texture atlas, for example, FIntPoint(16, 16) would be used to access subsections of a texture that has a grid of 16/16 subtextures, similar to how it’s done in minecraft. UVSize is an FVector2D which is calculated then cached off only once as 1 / TextureTiling. In our 16x16 example, this would give us a UVSize of FVector2D(0.0625f). I want this to only be done once during game and always be accessible from anywhere during game because it is constantly needed by a number of systems.
I initially put the variables on a new class called AVoxelManager that is spawned in by the gamemode, similar to how the gamemode spawns in a gamestate. When AVoxelManager is constructed, I set a static AVoxelManager* var to the new instance so its easily accessible from any systems in code. Since I extended AVoxelManager with a blueprint child ‘BaseVoxelManager’, the variable TextureTiling is also editable in editor. This all works great. But it breaks down as soon as I need to access these variables while not running a game.
For example, there is a blueprint actor that needs to display a voxel block in its viewport in editor. To properly texture the block and have it show correctly in the viewport, in c++ I am calling a function I made called ResetMesh that rebuilds the voxel block mesh on demand for easy viewing in the viewport. The problem is that ResetMesh requires us to fetch UVSize but no instance of AVoxelManager exists yet because we are in editor, not in a game.
I thought about just grabbing the class default object of AVoxelManager and using it’s UVSize but there are two problems with that: first, if I were to change TextureTiling in the blueprint child class, BaseVoxelManager, the CDO of AVoxelManager’s TextureTiling will now be different from the desired TextureTiling, the one on the blueprint class. The second is that UVSize is a derived var anyways so it won’t have the precalculated value if accessed on a CDO.
Ideally, I need an object that always exists so anything can access it from in game or in editor. Does anyone know of something like this? Or maybe there is a better solution? Would extending GameInstance work? I’m pretty sure it always exists. But again, I also need to be able to easily edit the variables (like TextureTiling) in editor. I also want to keep the accessing of variables like this extremely fast, just getting a variable, no 'if’s or anything like that.
I look forward to any ideas you all might have. Thanks for your time!