Comments (14)
I like that small utility class, and it toally solves the issue. Probably it also need a variadic emplace() method to create the elements, but yeah, something like totally works.
I was probably overthinking, since its only for creation, I only need the emplace functionality.
from entt.
Yeah, it was just an example to give you a grasp. I don't even know if it compiles fine. 🙂
I'm glad it helped btw. Let me know if we can consider the issue closed. Thanks. 👍
from entt.
Something like the handle
class maybe? It has a method to get the registry though. However, you can use it as an example to implement a custom solution. Does it work for you?
from entt.
I looked into the handle, and see that it had access to the entity and to the registry, so it doesn't really solves my issue. I guess I could inherit from handle
and disable the registry access. Would you accept a contribute for this?
from entt.
It would be a breaking change, so no. I don't plan to make the registry
method private in that class. I'm sorry.
However, you can inherit or take inspiration from it and do whatever you want that fits your design. 👍
from entt.
I wasnt thinking about breaking the current design of the class, that would be terrible.
from entt.
I was thinking on adding a new template parameter to the handle class, and instantiate the entity() and registry() methods depending on that template parameter. But I have checked the code and the basic_handle already has a ...Scope template parameter, which makes it impossible to add a new parameter between the Registry type and the Scope without making a breaking change.
from entt.
I guess Michele suggests doing whatever you want/need in your code base. Adding a new type that is essentially handle
, but not quite, or complicating the handle
itself is not a great plan maintenance-wise.
from entt.
Yeah, I think so.
I still think the functionality will be a nice addition to EnTT, I have found my self several times trying to restrict access to the registry. But if there is not enough traction for adding it, we can just close the issue.
Thanks for the answers!
from entt.
I have found my self several times trying to restrict access to the registry
What you're trying to do isn't restricting access to the registry though.
Instead, you're trying to emulate the EC model of Unity or the like, where the entity is the key actor and you can do everything by means of its API.
ECS models are slightly different. Systems are meant to operate on a whole set of entities and components rather than on a single entity. Whit this in mind, you can already restrict a system's scope by passing storage classes or views to it.
That is why there isn't much traction probably. This is a pretty uncommon request. 🙂
from entt.
The issue is not when operating on the entities, for that I use views. The issue comes when creating entities. In my use case, I am trying to create an entity with the needed components to simulate a garment. I need to add several components (Mesh, Collider...).
Right now I have a function which is in charge of creating the components for the entity, but to do so, I need to pass the whole registry, so inside that function, which should only be allowed to create the components I want, now I can modify all the registry. That is why I was thinking on a way of being able to add components to a single entity, without giving full access to the registry.
If there is a better way to initialize the entities with its components, I am all ears
from entt.
Passing in the "whole" registry
is not an issue in your case.
However I see 2 options for you that can enforce this form of separation.
- Like @skypjack said, instead of passing in the
registry
to the creation function, you can only pass in theentity
and all thestorage
s (component pools) that you require. - You already found the
Scope... template
on thehandle
:), while this still would allow to access theregistry field
of ahandle
, it would allow you to restrict whichcomponent types
(storage
s) can be accessed through it's interface. This feels more like the solution you want. You can also declare an alias for the special restricted handle type.
from entt.
Why do you say its not an issue in my case? I am thinking on a teammate doing something wrong like
void initialize_garment(entt::registry ®, entt::entity garment) {
registry.emplace<Mesh>(garment);
registry.emplace<Collider>(garment);
registry.emplace<Material>(garment);
...
auto meshes = reg.view<Mesh>(); // Wrong, we are also accessing already existing meshes
}
I agree the programmer of that code is doing something really bad, and that code will never pass code review. But restricting what the programmer can use in the first place will solve the issue.
How would you use the component pools in this case? I have never done something like that.
The option of restricting the component types of the handle doesn't totally solve the issue (although it definitely improves it). I can still access entities which have one of the restricted types.
Maybe there is a third solution. Create entities with a "to be built" component and pass a view to those entities.
from entt.
I see your point. However, as you correctly pointed out, the handle class cannot be changed easily in this sense.
That said, if all you want is to restrict access when creating new entities, a small utility with a minimal interface is bettter suited probably. Something like this (completely out of my mind and just to give you a grasp of what I mean):
struct my_utility {
my_utility(entt::registry &ref, entt::entity target)
: reg{&ref}, entity{target}
{}
template<typename Type>
decltype(auto) emplace() { return reg->emplace<Type>(entity); }
private:
entt::registry *reg;
entt::entity entity;
That's all. You can pass it as a function argument and use it in the body as:
Mesh &mesh = util.emplace<Mesh>();
// fill the mesh as needed and proceed with other components
I mean, for a grand total of 9 loc you've a fully encapsulated emplace function, which is what you're looking for if I get it correctly? If this is the case, it sounds like a good compromise to me. 🤷♂️
How would you use the component pools in this case?
It wouldn't fit your requirements anyway. If you pass a pool to a function, your teammate can still remove random elements from it or iterate all the instances in it and do something crazy with them.
Create entities with a "to be built" component and pass a view to those entities.
I personally use something similar actually. Sort of setup this entity as concept C component that is handled by a specific system at the right point in the loop (for some definitions of right ofc 😅).
This doesn't solve your issue though. My system still receives the whole registry to setup things.
from entt.
Related Issues (20)
- Access violation when using registry.clear<Component>() HOT 9
- Is it possible to use entt::nth_argument_t with lambdas that capture? HOT 6
- Question it is posible to use Entt with lightweight wrapper class as a entt::entity HOT 2
- Calling valid() on empty entt::valid leads to a segmentation fault.
- Add ability to compare entt::null and entt::handle
- Components with overriden new operator. HOT 6
- sparse_set_iterator::operator[] causes warnings when used with size_t HOT 6
- Working with multiple storages for the same type and preparing a view for each of the storages HOT 3
- entt build error: unused type alias 'common_type' HOT 9
- assert on destroy (inside of release function) non-orphan entity HOT 8
- Handling large tilemap HOT 7
- bazel version and compatibility number out of sync HOT 5
- storage<entt::entity>("id"_hs) should be asserted against HOT 2
- Get a view of components with a certain trait HOT 2
- Groups not functioning without sigh_mixin HOT 1
- Reliability across a network?
- Can't emplace component derived from std::vector HOT 4
- Iterating over all entities HOT 2
- Documentation references the deprecated registry-based `entt::to_entity` API HOT 2
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 entt.