Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Tweens #966

Open
bootzin opened this issue Dec 7, 2024 · 2 comments
Open

Expose Tweens #966

bootzin opened this issue Dec 7, 2024 · 2 comments

Comments

@bootzin
Copy link

bootzin commented Dec 7, 2024

Hi

I was wondering if it would be possible to expose the Tween list used inside the Tweener. Would be useful to know, for instance, when all tweens had finished processing

@kaltinril
Copy link
Contributor

@AristurtleDev and other Devs/Mods,

This request seems pretty straight forward. If the requestor @bootzin just needs to know if the tweens are finished, then I think A or B should solve that without exposing the behavior of the private member _activeTweens. If @bootzin really does still need access to the underline private member, and we want to go that route, Option C could be performed.

It appears a public variable AllocationCount was added in 2018 in this commit:
5e3cbb3#diff-3a4b830fec3aaaec78ec00828bcf2a630ccecf43acc51a27a29621a6a0c685e6

This variable I would assume was intended to be the count of active tweens (unsure?)
It also doesn't appear this variable is ever subtracted from, or used anywhere else in the entire library.

Options off the top of my head:
A) Add a AllocationCount--; on line 70 so that this variable correctly shows how many active objects there are:
https://github.com/craftworkgames/MonoGame.Extended/blob/080f5e1f23ed25e0011c7d6471b3e37fada85c30/source/MonoGame.Extended/Tweening/Tweener.cs#L70C2-L70C47

B) change AllocationCount into an expression-bodied member instead like public long AllocationCount => _activeTweens.Count;

C) Expose the private List of Tween _activeTweens

Thoughts from anyone?

@AristurtleDev
Copy link
Member

It does look like AllocationCount was initially meant to keep track of the number of tweens created. It's only incrementing when a new Tween is created and added to the _activeTweens list. I'm not sure what the original purpose of this property was, since it's only ever incremented and never used anywhere. Maybe a left over idea that Dylan never finished?

As for exposing the private member, this can be debatable. I personally prefer design to show intent over just exposing everything and letting the end user make their own mistakes. For instance, List<T> is not thread safe, and it's also used in for loops during the Cancel* methods, so you wouldn't want anything modifying the list during these scenarios.

Given we're on .NET 8 now, and all the new wonderful things with Span<T> would could expose the list that way by doing something like

pulbic Span<Tween> ActiveTweens => CollectionMarshal.AsSpan(_activeTweens);

This is a zero allocation return of the underlying collection from the list as a Span<T> that can be used to check on the current active tweens and get information about them without risking exposing the internal implementation design.

cc: @craftworkgames/monogame-extended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants