Skip to content

Conversation

Doprez
Copy link
Contributor

@Doprez Doprez commented Aug 21, 2025

PR Details

This PR shows a POC that would reduce the amount of required casts by users. I don't want to merge this but I was curious on what the thoughts would be on something like this as using object causes a ton of casting and required checks to be usable. Ideally I would redo this PR but with the Obsolete attribute. The only variable that gets in the way is CollectionChanged as I dont know what the non-obsolete variable would be called.

I also still need to verify that there wont be weird checks in the GameStudio layers as I assume they may not like generics as much as objects. Works fine.

Related Issue

No related issues. I ran into this today trying to use tracking collections for a modular system I am experimenting with as seen in the code snippet below:

    private void WornItemsChanged(object? sender, TrackingCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            var newItem = e.Item;
            if (e.Item is GameItem item && !item.HasDescriptor<WearableDescriptor>())
            {
                _logger.Error($"Only wearable items can be worn. Item: {item.Name}");

                // Remove the item if it is not wearable
                var key = e.Key as string;
                var oldItem = e.OldItem == null ? null : e.OldItem as GameItem;
                WornItems[key] = oldItem;
            }
        }
    }

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

@Doprez
Copy link
Contributor Author

Doprez commented Aug 21, 2025

No issues with a simple example.

{264313F5-AFCE-4B43-B7F4-328448DA2DE6}
public class TestComponent : StartupScript
{
    public TrackingCollection<int> Numbers { get; set; } = [];
    public TrackingHashSet<int> UniqueNumbers { get; set; } = new TrackingHashSet<int>();
    public TrackingDictionary<int, string> NumberNames { get; set; } = new TrackingDictionary<int, string>();
}

@Doprez
Copy link
Contributor Author

Doprez commented Aug 22, 2025

For context on if I were to make this real I would just change the existing TrackingCollectionChangedEventArgs to inherit from TrackingCollectionChangedEventArgs<T,T> like TrackingCollectionChangedEventArgs<object, object> and set it as obsolete for the people who already use this functionality so they can migrate over gradually.

Copy link
Member

@Kryptos-FR Kryptos-FR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no against it if there are no regression.

I don't think we can do a transition with an duplicated obsolete and non-obsolete property. That would be messy. So it has to be a breaking change IMO.

@Doprez
Copy link
Contributor Author

Doprez commented Aug 22, 2025

Im ok with it being a breaking change. I think it would be fairly easy to tell people how to migrate as they just need to define their types but I just dont want to wait for this to release 😆

I was also thinking of making a seperate event args for Lists since they shouldnt need to define 2 types and would simplify the Collection and HashSet event definitions.

@VaclavElias VaclavElias added the area-Core Issue of the engine unrelated to other defined areas label Aug 23, 2025
@Doprez
Copy link
Contributor Author

Doprez commented Aug 23, 2025

I went ahead with the implementation that separates the definition between collections and keyed collections. This was a lot nicer to work with at a higher level and just looked cleaner overall.

Would this be able to be pushed in to main as a revision breaking change or would it have to wait for the major/minor release?

@Doprez Doprez marked this pull request as ready for review September 23, 2025 17:09
Copy link
Collaborator

@Eideren Eideren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just minor stuff. Have you looked at the samples solution if there is any remnant over there as well ?

Comment on lines -1 to -2
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your IDE ate the header

Comment on lines +17 to +18
private EventHandler<TrackingCollectionChangedEventArgs<T>>? _itemAdded;
private EventHandler<TrackingCollectionChangedEventArgs<T>>? _itemRemoved;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong naming convention

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my mistake, I thought we were going to use the Microsoft standards for new changes if we worked in them. Ill switch them back to the lower camel case structure.

Comment on lines -1 to -3
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to worry that you aren't feeding your IDE correctly

Comment on lines +26 to +27
private EventHandler<TrackingKeyedCollectionChangedEventArgs<TKey, TValue>>? _itemAdded;
private EventHandler<TrackingKeyedCollectionChangedEventArgs<TKey, TValue>>? _itemRemoved;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, same here

Comment on lines +19 to +20
private EventHandler<TrackingCollectionChangedEventArgs<T>>? _itemAdded;
private EventHandler<TrackingCollectionChangedEventArgs<T>>? _itemRemoved;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

@Eideren
Copy link
Collaborator

Eideren commented Sep 27, 2025

I'll change this to draft for organizational purposes, feel free to change it back when ready :)

@Eideren Eideren marked this pull request as draft September 27, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Core Issue of the engine unrelated to other defined areas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants