So, for reasons that I won't go into (has to do with my team more so than a good objective reason, unfortunately), I'm building a soft game engine in C# on top of SharpDX. C++ wasn't an option. I can't store objects contiguously in C# when it comes to reference types like classes, so I'm thinking about taking a data-oriented-design approach to storing objects in my scene. Thus, my scene looks a bit like this right now (all of the fields are structs because I can store them by value type, and thus contiguously):
public delegate void UpdateMethod (ref ModelData md, ref Transform t, ...)
class Scene
{
ModelData[] models;
Transform[] transforms;
//...some other fields here
UpdateMethod[] updateMethods; //delegate array
void UpdateAll()
{
for (int i = 0; i < NUM_GAME_OBJECTS; i++)
{
if (updateMethods[i]!= null)
{
updateMethods[i](ref models[i], ref transforms[i], ...);
}
}
}
}
The idea being that I can now store the objects contiguously as opposed to just an array of contiguous references to the data if I made each component a class.
Is this even a viable technique, or am I abusing the DOD paradigm? I'm storing all of my objects like this, not just my dynamic ones. Perhaps I could split it up into two sets of arrays for dynamic and non-dynamic, so I'm not tempting branch-predictor slow-downs on the if statement for UpdateAll()
?
Will an approach like this create more issues than it attempts to solve? I will note that, while updating occurs here, I outsource the actual carrying out of the component functionality to other systems. For example, an input subsystem determines keys pressed, so an object could check in its unique update method denoted by the delegate; the update doesn't do everything, just behavior specific to that bundle of game object data.
This seems analogous to operating over columns in a row-major language, in that I'm killing performance gain of data locality; am I correct in concluding it would it be beneficial to wrap all of these fields into some GameObjectData
wrapper struct, and just store those in an array?