3
\$\begingroup\$

I have a lot of redundancy here, but I am not sure how could I make it look shorter.

namespace Notan
{
    public class EquipmentSystem : ScriptableObject
    {
        public UnityEvent onUpdate = new UnityEvent();

        [SerializeField] private Weapon weapon1;
        public Weapon Weapon1
        {
            get => weapon1;
            private set
            {
                weapon1 = value;
                onUpdate?.Invoke();
            }
        }

        [SerializeField] private Weapon weapon2;
        public Weapon Weapon2
        {
            get => weapon2;
            private set
            {
                weapon2 = value;
                onUpdate?.Invoke();
            }
        }

        [SerializeField] private Accessory accessory1;
        public Accessory Accessory1
        {
            get => accessory1;
            private set
            {
                accessory1 = value;
                onUpdate?.Invoke();
            }
        }

        [SerializeField] private Accessory accessory2;
        public Accessory Accessory2
        {
            get => accessory2;
            private set
            {
                accessory2 = value;
                onUpdate?.Invoke();
            }
        }

        [SerializeField] private Shield shield;
        public Shield Shield
        {
            get => shield;
            private set
            {
                shield = value;
                onUpdate?.Invoke();
            }
        }

        [SerializeField] private Armor armor;
        public Armor Armor
        {
            get => armor;
            private set
            {
                armor = value;
                onUpdate?.Invoke();
            }
        }

    }
}
```
\$\endgroup\$
3
  • 1
    \$\begingroup\$ use PropertyChangedEventHandler to raise event whenever a property changes. here is a good answer to it : stackoverflow.com/questions/2246777/… \$\endgroup\$
    – iSR5
    Mar 3, 2021 at 11:26
  • 1
    \$\begingroup\$ Without knowing the domain it is hard to give any naming advice but for me PrimaryWeapon and SecondaryWeapon would be more meaningful. \$\endgroup\$ Mar 3, 2021 at 13:18
  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Dec 1, 2021 at 13:21

1 Answer 1

0
\$\begingroup\$

You could make your code less redundant through the use of a custom SourceGenerator. It could work kinda like Fody works for WPF and INotifyPropertyChanged.

Sadly I have no example to provide for the SourceGenerator's implementation because I haven't had time to toy with them yet. Though the idea is to make your SourceGenerator generate the redundant code for you at compile time whenever it detects something that you want to be autogenerated.

You have two options but both are gonna require custom attribute(s) :

  • Make it work like a white-list : don't autogenerate by default, autogenerate only when property is tagged with your attribute. Your property (or field) would then be reduced to

    [MyAutoGenerationAttribute]
    public object MyProperty { get; set; }
    
  • Make it work like a black-list (like Fody does) : always autogenerate by default, don't autogenerate when property is tagged with your attribute.

I'm not really familiar with Unity but my only concern is that this solution may not be compatible with Unity's shenanigans, especially since SourceGenerators is a fairly new feature.

PS : If optimization matters I recommend you edit your setters so that the event gets raised only when the value of the property changes for real (ex: by wrapping the setter with something like if (myBackingField != value))

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.