Skip to content

feat: add GetAllEntities API#92

Open
2vg wants to merge 9 commits intoKxnrl:masterfrom
2vg:feat/get-all-entities
Open

feat: add GetAllEntities API#92
2vg wants to merge 9 commits intoKxnrl:masterfrom
2vg:feat/get-all-entities

Conversation

@2vg
Copy link
Copy Markdown
Contributor

@2vg 2vg commented Apr 4, 2026

close: #57

  • Add GetAllEntities/GetAllEntitiesByClassname methods to IEntityManager for retrieving all currently existing entities
  • Maintain an internal entity cache in EntityManager using an index-based array and a uint[] bitset, populated via existing OnEntityCreated/OnEntityDeleted callbacks
  • Entity references are stored in a fixed IBaseEntity?[16384] array indexed by EntityIndex
  • A uint[512] bitset tracks which indices are active, enabling fast enumeration via BitOperations.TrailingZeroCount without scanning empty slots
    • BitOperation should make it run faster than a naive implementation, Because empty buckets can be skipped, it can be iterated in O(active) time in a sparse state
  • As a bonus, fixed some LogError typos.

Added

  • GetAllEntities(): Returns all active entities
  • GetAllEntities<T>(): Returns all active entities matching a specific type
  • GetAllEntitiesByClassname(string classname): Returns all active entities matching a classname (useful for entity types without a dedicated interface, e.g. CPropYadaYada)
  • GetAllEntitiesByClassname<T>(string classname): Returns all active entities matching both a classname and a specific type

@2vg
Copy link
Copy Markdown
Contributor Author

2vg commented Apr 4, 2026

I asked nuko-sama, and they told me that Created/Destroy is the best option.
Please let me know if there's a better way. 🙏

Comment thread Sharp.Shared/Managers/EntityManager.cs Outdated
Copy link
Copy Markdown
Collaborator

@Nukoooo Nukoooo left a comment

Choose a reason for hiding this comment

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

just some nitpicks, it looks good to me

Comment thread Sharp.Core/Managers/EntityManager.cs Outdated
Comment thread Sharp.Core/Managers/EntityManager.cs Outdated
Comment thread Sharp.Shared/Managers/EntityManager.cs Outdated
@2vg 2vg requested a review from Nukoooo April 4, 2026 11:48
Comment thread Sharp.Core/Managers/EntityManager.cs Outdated
@2vg 2vg requested a review from Kxnrl April 5, 2026 01:53
@Kxnrl
Copy link
Copy Markdown
Owner

Kxnrl commented Apr 6, 2026

an entity will fire an OnDeleted event when it is marked as KILL_ME, but at this time the entity is not destroyed and its data/proterties can be safely accessed. I think you might need to consider this special case, as it is quite common with weapon and item entities. OnDeleted even getting fired before OnSpawned.

@2vg
Copy link
Copy Markdown
Contributor Author

2vg commented Apr 7, 2026

@Kxnrl I've moved it to DeletedPost for now.
Even if retrieved from the cache, I think the Entity should be checked by the Developer using IsValid, etc., but I'm not sure what the best solution is.

@Kxnrl Kxnrl added the work in progress Work in progress label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

work in progress Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add getting a list of entities

3 participants