Comments (18)
Once we add a component using a generated method like e.AddHealth(42)
we could add an event handler for OnComponentRemoved
. This handler could check if the component index is the same and push the component to the component pool. This way it would work for both cases e.RemoveAllComponents()
and pool.DestroyEntity()
What do you think?
I also have unity project for performance testing where I could check if the added event handler has a negative effect on the performance.
from entitas.
Also, I think it's time to also add tests for the actual logic of the generated output, not only tests for the expected output of the generator. I'd like to add tests for the four component types (standard, flag, single std, single flag)
from entitas.
First tests revealed it adds 312byte per component even with cached event handlers
from entitas.
Creating new components would be cheaper...
from entitas.
I see... The issue I stumbled upon, and thus discovering this intricacy, was that my object count was increasing a lot, I had a lot of entities which were being destroyed and created, and the GC was turning on once in ~10 secs and that was stuttering my game :/
I am tinkering with the code right now, trying to figure out another way...
from entitas.
from entitas.
Here's my test, if you're interested:
from entitas.
using System.Collections.Generic;
namespace Entitas {
public partial class Entity {
public PositionComponent position { get { return (PositionComponent)GetComponent(ComponentIds.Position); } }
public bool hasPosition { get { return HasComponent(ComponentIds.Position); } }
static readonly Stack<PositionComponent> _positionComponentPool = new Stack<PositionComponent>();
public static void ClearPositionComponentPool() {
_positionComponentPool.Clear();
}
EntityChanged cachedOnComponentRemoved {
get {
if (_cachedOnComponentRemoved == null) {
_cachedOnComponentRemoved = onComponentRemoved;
}
return _cachedOnComponentRemoved;
}
}
EntityChanged _cachedOnComponentRemoved;
public Entity AddPosition(float newX, float newY, float newZ) {
OnComponentRemoved += cachedOnComponentRemoved;
var component = _positionComponentPool.Count > 0 ? _positionComponentPool.Pop() : new PositionComponent();
component.x = newX;
component.y = newY;
component.z = newZ;
return AddComponent(ComponentIds.Position, component);
}
public Entity ReplacePosition(float newX, float newY, float newZ) {
// var previousComponent = hasPosition ? position : null;
var component = _positionComponentPool.Count > 0 ? _positionComponentPool.Pop() : new PositionComponent();
component.x = newX;
component.y = newY;
component.z = newZ;
ReplaceComponent(ComponentIds.Position, component);
// if (previousComponent != null) {
// _positionComponentPool.Push(previousComponent);
// }
return this;
}
public Entity RemovePosition() {
// var component = position;
RemoveComponent(ComponentIds.Position);
// _positionComponentPool.Push(component);
return this;
}
void onComponentRemoved(Entity entity, int index, IComponent component) {
OnComponentRemoved -= cachedOnComponentRemoved;
if (index == ComponentIds.Position) {
_positionComponentPool.Push((PositionComponent)component);
}
}
}
public partial class Matcher {
static IMatcher _matcherPosition;
public static IMatcher Position {
get {
if (_matcherPosition == null) {
var matcher = (Matcher)Matcher.AllOf(ComponentIds.Position);
matcher.componentNames = ComponentIds.componentNames;
_matcherPosition = matcher;
}
return _matcherPosition;
}
}
}
}
from entitas.
added onComponentRemoved
from entitas.
I found this article summarizing some garbage tests with events and delegates: http://jacksondunstan.com/articles/3264#comments and they confirm your findings. 104 bytes for creating a delegate and then another 208 bytes when adding that delegate to an event if it is the second+.
from entitas.
I could try writing a custom event system for Entitas. Might be (or not be) more efficient
from entitas.
I have an idea which works completely without events. An entity could have an array of stacks (similar to the array which holds the components). When removing a component it will get the stack at index and push the component. Done. If no stack exists, it doesn't (which means we're using entitas without the code generator)
I'll give it a try
from entitas.
fyi, I started writing tests for the existing generated component extensions. After that I will implement my idea test driven.
from entitas.
Ok, I found a way which works without events.
See PR #60
from entitas.
@trumpets @SvDvorak
If you like, you can test the latest version. Here are the zip files
fbfc99e
I'd be interested if it solves your issues. Let me know, if it works better now
from entitas.
See #60
from entitas.
A bit late but I checked through your solution, smart moving up the pools and creating a function to retrieve the correct one. Nicely done!
from entitas.
Thanks :)
from entitas.
Related Issues (20)
- Wiki closed for editing
- Unity crashes when clicking on a GameObject with Component of Entitas HOT 8
- Remove DesperateDevs dependencies
- [SOLVED] Unity 2022.3.4f1: Exceptions + broken inspector when looking at any entity HOT 16
- Add/Replace/Remove vs Set/Unset HOT 5
- Boolean in setttings for unique components to actually be unique
- A component with multiple contexts +events, only make event listeners for 1 context
- Could not resolve type with token ... expected class System.Threading.CancellationToken HOT 10
- How to generate codes in a project which has several csproj files? HOT 1
- Update on Entitas 2.0-beta and my situation HOT 3
- [1.14.1]code gen twice and compile error HOT 1
- there is a Error, on click Tools/Jenny/Gennerate, how can I do? HOT 4
- _group.GetEntities().AsParallel().ForAll(Execute);这段语句有GC HOT 2
- Is it feasible to use Entitas in the backend? HOT 7
- Entity Behaviour in Unity inspector not visible HOT 2
- Removal of crucial SearchPaths when executing the wiz command
- How to get all group in some context, without reflection。
- GUI Null ref when trying to view entity in inspector HOT 1
- generator issues HOT 1
- Error running dotnet Jenny/Jenny.Generator.Cli.dll gen -v command - System.IO.FileNotFoundException HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entitas.