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

feat: FlexibleProcessing as alternative to EntityComponentProcessor for lightweight, abstract and typesafe processing in idiomatic c# #2331

Merged
merged 4 commits into from
Jun 19, 2024

Conversation

Eideren
Copy link
Collaborator

@Eideren Eideren commented Jun 15, 2024

PR Details

This PR enables building of processors from interfaces, enabling unrelated types to be processed by the same system.

The current processor implementation force users to derive from the same root type to build their logic, this does not work well with the fact that we have different 'Script' variant which have all their specific use. It becomes a composition issue when more abstract concepts, like event functions, are introduced.

A good example of that is when components should manipulate physics object, it is preferable to do so in lockstep with the physics engine's tick. But those component may also require an async or update loop to process inputs. We would be forced to either create another new 'script' type which users have to derive from which would split their logic between two different types, or tie it to one of the other 'script', both solutions would introduce additional overhead while not being practical.

Example usage:

public class MyComponent : StartupScript, IPhysicsTick
{
    public void Tick(float deltaTime){ }
}

public interface IPhysicsTick : IComponent<IPhysicsTick.PhysicsTickProc, IPhysicsTick>
{
    void Tick(float deltaTime);
    public class PhysicsTickProc : IProcessor, IUpdateProcessor
    {
        public int Order => 5;
        public List<IPhysicsTick> Components = new();
        public void SystemAdded(IServiceRegistry registryParam) { }
        public void SystemRemoved() { }
        public void OnComponentAdded(IPhysicsTick item) => Components.Add(item);
        public void OnComponentRemoved(IPhysicsTick item) => Components.Remove(item);
        public void Update(GameTime gameTime)
        {
            foreach (var comp in Components)
                comp.Tick((float)gameTime.Elapsed.TotalSeconds);
        }
    }
}

Implementer may include additional processing during the OnComponentAdded step to reduce or elide virtual calls

Related Issue

Introduced for usage in #2131

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.

…or lightweight, abstract and typesafe processing in idiomatic c#
Copy link
Member

@manio143 manio143 left a comment

Choose a reason for hiding this comment

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

Nice work!

@manio143 manio143 added the area-Core Issue of the engine unrelated to other defined areas label Jun 17, 2024
@Eideren Eideren merged commit a6c42cb into stride3d:master Jun 19, 2024
13 checks passed
@Eideren
Copy link
Collaborator Author

Eideren commented Jun 19, 2024

Thanks a bunch for reviewing this @manio143

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.

2 participants