To help catch idea of what I want to achieve, I’m making Map Editor for my game and it is going to contain Entities that players will be able to create. I want each Entity to be class, that has list of variables of various types (Color, Float, Bool, etc.) that would be easy to access, change, modify.
I’ve thought of making generic interface or generic class, but then I wouldn’t be able to make one unified List and would cause big spaghetti code with lots of conditionals for each different type of list.
I already know that I’ll need to have a lot of conditionals just to check variable type and draw it in UI.
It would be great if it would also easily serialize the List/Array of variables using XmlSerializer.
Sure you could. That’s exactly what doing it would buy you
Honestly - I’d invert the relationship. Let each type be responsible for drawing its UI.
public interface EntityDrawer
{
void Draw();
}
List<EntityDrawer> entities;
foreach (var entity in entities)
{
entity.Draw();
}
Or if you’ll never have an Entity that doesn’t need a UI then just put Draw on the abstract Entity base class.
If you don’t want the UI logic in the object itself then I’d build a Dictionary of types to delegates that do the drawing. Then just look up what delegate to execute by what type of entity it is.
Dictionary<Type, Action<Entity>> drawers;
if (drawers.TryGetValue(typeof(myEntity), out action)
{
action(myEntity);
}