ikkentim / SampSharp

A framework for writing game modes for SA-MP in C#. SA-MP is a free Massively Multiplayer Online game mod for the PC version of Rockstar Games Grand Theft Auto: San Andreas.
https://sampsharp.net
Apache License 2.0
210 stars 42 forks source link

Add overload for AddComponent method that takes an instance value #425

Closed duydang2311 closed 1 year ago

duydang2311 commented 1 year ago

There are two overloads for AddComponent method, and they are not so convenient to use.

T AddComponent<T>(EntityId entity) where T : Component;
T AddComponent<T>(EntityId entity, params object[] args) where T : Component;

Using params object[] args destroys the whole advantages of having language server intelli-sense. There are no suggestions on what we can pass into the arguments. Besides, this using reflection to create instance is also a bit slower.

Instead, I'd suggest adding this overload:

T AddComponent<T>(EntityId entity, T value) where T : Component;

The method becomes much more shorter and nicer to use, considering the example below:

public class MyComponent : Component
{
    public long Id { get; set; }
    public string Name { get; set; }
}

AddComponent(entity, new Component() { Id = 1, Name = "abc" });