C
C#2y ago
MrScautHD

❔ How can i add to my List a Type from a Method without cast it to a other one

Hi i try to register my Renderer that should not get casted because it is a interface and i call it laiter in a foreach to run on every renderer the Draw Method, but how i see it not works because i would need it to cast to add it to this list has any a other idea to fix this?
5 Replies
MrScautHD
MrScautHD2y ago
// OVERLAY
public static readonly CrosshairOverlay CrosshairOverlay = Register("crosshair", new CrosshairOverlay());

private static T Register<T>(string name, T renderer) {
RegistryTypes.Renderers.Add(name, renderer);

return renderer;
}
// OVERLAY
public static readonly CrosshairOverlay CrosshairOverlay = Register("crosshair", new CrosshairOverlay());

private static T Register<T>(string name, T renderer) {
RegistryTypes.Renderers.Add(name, renderer);

return renderer;
}
oh i see right now thats not the problem ok i have a list i added every Registry to it
public interface IClientRegistry {

public static readonly List<IClientRegistry> Registries = new();

public virtual void Initialize(GraphicsDevice graphicsDevice, GameWindow window) {

}

public virtual void LoadContent(GraphicsDevice graphicsDevice, ContentManager content) {

}

public virtual void Draw(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, GameTime time) {

}

public virtual void Update(GameTime gameTime) {

}
}
public interface IClientRegistry {

public static readonly List<IClientRegistry> Registries = new();

public virtual void Initialize(GraphicsDevice graphicsDevice, GameWindow window) {

}

public virtual void LoadContent(GraphicsDevice graphicsDevice, ContentManager content) {

}

public virtual void Draw(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, GameTime time) {

}

public virtual void Update(GameTime gameTime) {

}
}
public static readonly List<IClientRegistry> Registries = new(); and i call all this methods with a foreach and this method call a foreach again does this make problems?
public void Update(GameTime time) {
// HERE IT PRINTS

foreach (ClientTicker ticker in RegistryTypes.Ticker.Values) {

// HERE IT PRINTS NOT

ticker.Update(time);
}
}
public void Update(GameTime time) {
// HERE IT PRINTS

foreach (ClientTicker ticker in RegistryTypes.Ticker.Values) {

// HERE IT PRINTS NOT

ticker.Update(time);
}
}
why does it not print in the foreach it looks like foreach in a method is not allowed that get called in a foreach because it does not print in it no it does ignore it even if i use a debugger
MrScautHD
MrScautHD2y ago
here it tell me everything
MrScautHD
MrScautHD2y ago
MrScautHD
MrScautHD2y ago
here like nothing happen :/ ok oh wait the list is empty wait häää why does it not work
// OBJECTS
public static readonly StreetLightRenderer StreetLightRenderer = Register("street_light", new StreetLightRenderer());

// OVERLAY
public static readonly CrosshairOverlay CrosshairOverlay = Register("crosshair", new CrosshairOverlay());

private static T Register<T>(string name, T renderer) {
RegistryTypes.Renderers.Add(name, (IRenderer) renderer);

return renderer;
}
// OBJECTS
public static readonly StreetLightRenderer StreetLightRenderer = Register("street_light", new StreetLightRenderer());

// OVERLAY
public static readonly CrosshairOverlay CrosshairOverlay = Register("crosshair", new CrosshairOverlay());

private static T Register<T>(string name, T renderer) {
RegistryTypes.Renderers.Add(name, (IRenderer) renderer);

return renderer;
}
the static thing never call register why? static things should get automaticly called when i create the class or not? so why does it not call my method
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.