SpiderPig Posted September 9, 2023 Posted September 9, 2023 Is there a reason the only accessible reference to a components entity is a raw pointer? std::weak_ptr<Entity> entityptr; //<-- not accessable? std::map<WString, std::vector<Connection> > connections; friend CComponent; protected: virtual void ReceiveSignal(shared_ptr<Component> sender, const WString& input, std::vector<std::any> arguments); Entity* entity;//<-- Use this one? virtual void Start(); virtual void Update(); Quote
Solution Josh Posted September 9, 2023 Solution Posted September 9, 2023 The entity "owns" the component". Otherwise you would have to maintain a handle to every component, to prevent them from being deleted. If the component also has a shared pointer to the entity, it would create a circular reference, and neither would ever be deleted. Normally I would use a weak pointer and have a GetEntity() method, but for components this seemed annoying, so I just used a raw pointer. When the component is detached from the entity, this pointer is set to NULL automatically, so I think this is fairly safe. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without.
SpiderPig Posted September 9, 2023 Author Posted September 9, 2023 Can a raw pointer be cast to a shared_ptr? There was just a few things I wanted to create inside component and had to pass the owner entity. I'm sure I'll find another way though so it's no problem. Quote
Josh Posted September 9, 2023 Posted September 9, 2023 auto e = entity->As<Entity>() 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without.
Josh Posted September 16, 2023 Posted September 16, 2023 For the reason you pointed out, the raw pointer is removed and you should use GetEntity() instead. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.