Jump to content

Component's Entity is rawptr


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

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();

 

Link to comment
Share on other sites

  • Solution

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.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...