C
C#8mo ago
2spooky2play

✅ Pass in arguments to a generic constructor

public T AddComponent<T>() where T: Component, new() {
Component component = new T(this);
return (T)AddComponent(component);
}
public T AddComponent<T>() where T: Component, new() {
Component component = new T(this);
return (T)AddComponent(component);
}
i want to be able to create new instances of T in the method, but i get the error 'T': cannot provide arguments when creating an instance of a variable type. how can i make this work? one precondition is that all child of Component will only have one argument in their constructor
6 Replies
lycian
lycian8mo ago
the short answer is you can't. The long answer is you either need to use reflection or an initialize pattern like
c#
interface IInitializable
{
void Initialize(object o); // or whatever type param
}

public T AddComponent<T>() where T: Component, IInitializable, new()
{
var component = new T();
component.Initialize(this);
return (T)AddComponent(component);
}
c#
interface IInitializable
{
void Initialize(object o); // or whatever type param
}

public T AddComponent<T>() where T: Component, IInitializable, new()
{
var component = new T();
component.Initialize(this);
return (T)AddComponent(component);
}
2spooky2play
2spooky2playOP8mo ago
public T AddComponent<T>() where T: Component, new() {
Component component = (Component)Activator.CreateInstance(typeof(T), this)!;
return (T)AddComponent(component);
}
public T AddComponent<T>() where T: Component, new() {
Component component = (Component)Activator.CreateInstance(typeof(T), this)!;
return (T)AddComponent(component);
}
i found this solution too
lycian
lycian8mo ago
yup, that's reflection it's not free, so it depends on your needs
2spooky2play
2spooky2playOP8mo ago
damn that really sucks i want to make a game object system like unity does
lycian
lycian8mo ago
So you're wanting something like AddComponent where it sets the gameObject property? you would be able to do that if you did something like
class Component
{
public required GameObject GameObject { get; init; }
}

class GameObject
{
T AddComponent<T>() where T: Component, new()
{
var component = new T()
{
GameObject = this
};
return (T)AddComponent(component);
}

object AddComponent(Object o) => throw new NotImplementedException();
}
class Component
{
public required GameObject GameObject { get; init; }
}

class GameObject
{
T AddComponent<T>() where T: Component, new()
{
var component = new T()
{
GameObject = this
};
return (T)AddComponent(component);
}

object AddComponent(Object o) => throw new NotImplementedException();
}
2spooky2play
2spooky2playOP8mo ago
class TransformComponent : Component {}

var obj1 = new GameObject("obj1");
var transform = obj1.AddComponent<TransformComponent>();
class TransformComponent : Component {}

var obj1 = new GameObject("obj1");
var transform = obj1.AddComponent<TransformComponent>();
i get the error 'TransformComponent' cannot satisfy the 'new()' constraint on parameter 'T' in the generic type or or method 'GameObject.AddComponent<T>()' because 'TransformComponent' has required members. its because of the required keyword i fixed it by removing the keyword and making the Host nullable (which to me is not the best solution)
Want results from more Discord servers?
Add your server