Entity pointers in components

I’ve noticed that, the pointer returned by GetEntity() of a component will point to a decent entity during the Init() function of a component. But in Activate() the memory space pointed to is faulty.

Also you are unable to retrieve the Transform from this Entity to use the position of the Entity the component is attached to.

Are there reasons behind of this or is this a bug?

How are you supposed to get information about the Entity a component is attached to, or can components live without a Entity they are attached to? But in the sandbox the component is clearly attached to an Entity (that has its own Transform component).

Or is GetEntity() already deprecated code from Cry and you are not supposed to use it?

2542-nope.jpg

Questions over questions, stand by for answers or contribute some if you have some!

Transform newTransform = AZ::Transform::CreateZero();
EBUS_EVENT_ID_RESULT(newTransform, GetEntityId(), AZ::TransformBus, GetLocalTM);

This returns ‘something’ but getting the Position from that with GetPosition() does not bring me any further.

Edit: Neither with CreateWorldTM

2541-nopenopenpoe.jpg

And this does work for you?

EBUS_EVENT_RESULT(_pEntity, AZ::ComponentApplicationBus, FindEntity, parentEntityID);

Unfortunatly it does not bring me anywhere.

I have also noticed that GetEntity() does not return the same ID that is shown on the (only) Entity in the Inspecter the component is added to.

But the Parent Entity field is unrelated to that problem. Its in another component and is related to the Transformation. I am not even that far yet.

But if GetEntity() only returns the proper Entity when in game mode, its hard/impossible to program any gameplay code. I still think. that the functions are deprecated and related to the CryEngine.

There must be a way with the eventbus system to get that to work properly.

No worries, I get what you want to say :wink:

So as far as I understand this,
a component retrieves the ID it is attached to over

GetEntityId()

You can then get the specific entity over


AZ::Entity* _parent = nullptr;
AZ::EntityId currentEntityID = GetEntityId();
EBUS_EVENT_RESULT(_parent, AZ::ComponentApplicationBus, FindEntity, currentEntityID);<br>

or straight up simply call

GetEntity()

which does the same as the EBUS call (if you are calling the GetEntity() from the component).

There is no other way to get the EntityId over the EBUS-System as far as I can tell, at least its not inside any of the busses in dev/Code/Framework/AZCore/AZCore/Component

The entity id retrieved from GetEntityID() is "valid" (e.g. starts with 15… and has the same size) but it is different from the entity ID shown inside the editor.

Thats the state so far, more coming after lunch.

Hi Herpaderp. I work with this too. And how i understand component->GetEntity() work only in game mode, not in Editor. Because component->Init() running only after you start game mode. I fix that problem with add field AZ::Uuid in my component and after i drag and drop add my component to Entity i add to this fiels parent Entity ID. And After that my component use
EBUS_EVENT_RESULT(pEntity, AZ::ComponentApplicationBus, FindEntity, parentEntityID); To get Pointer to Entity.

MyCopmonet.h


...
AZ::EntityId parentEntityID;
...

MyComponent.cpp


...
// from Reflect
->DataElement("CheckBox", &StaticStructureComponent::update, "Update Component", "")
->Attribute("ChangeNotify", &StaticStructureComponent::OnUpdate)
... // ~From reflect
// from OnUpdate function
AZ::Entity* _pEntity = nullptr;
EBUS_EVENT_RESULT(_pEntity, AZ::ComponentApplicationBus, FindEntity, parentEntityID);
if (_pEntity)
{
Component* _newMeshComponent = nullptr;
AZ::Uuid _meshComponentID = "{C4C69E93-4C1F-446D-AFAB-F8835AD8EFB0}";
EBUS_EVENT_ID_RESULT(_newMeshComponent, _meshComponentID, AZ::ComponentDescriptorBus, CreateComponent);
_pEntity->Deactivate();
_pEntity->AddComponent(_newMeshComponent);
_pEntity->Activate();
}
// ~from OnUpdate function
...

in MyComponent.cpp, code from function binding to “ChangeNotify” and if change filed (CheckBox) Component Add MeshComponent to myParent Entity. I hope this help you.

Yep, work perfect. Only not nice, i must hands add Entity to field ParentField in Editor. But this not critical for me.

GetEntity() worked only in Game mode :frowning:

Here my first problem with GetEntity() How add AZ::Component to AZ::Entity from Custom AZ:Component in c++ ?

Today i finish some work and for me will need transformation field too. If i will found how work with that i reply it to you :slight_smile:

For me need too. I will try found how work with that, and if will found i reply it to you. Sorry for my English, this language not my native language.

Hmm “but it is different from the entity ID shown inside the editor.” Maybe we have two component one for Editor one for Game

I am not using anything from lmbrcentral by now.

Btw:

Came up with the idea to put everything into the OnTick Function from the TickBus,
because I thought that the correct EntityID gets propageted after Activate() is finished,
but that didn’t bring any success either. :frowning:

The next thing I’ve noticed is, that the entityID in the editor stays the same, where as the entityID of the entity object in C++ is changing with every game loop (Alt + G, or AI/Physics debug mode enabled).

Seems like @Anatoliy is right. There must be some kind of EditorEntity and GameEntity that are different. I have this now


Entity* _entity = nullptr.
if (_entity == nullptr)
{
_entity = GetEntity();
//AZ::EntityId currentEntityID = GetEntityId(); //EBUS_EVENT_RESULT(_entity, AZ::ComponentApplicationBus, FindEntity, currentEntityID);
Transform newTransform = AZ::Transform::CreateZero();
if (_entity != nullptr)
{
EBUS_EVENT_ID_RESULT(newTransform, _entity->GetId(), AZ::TransformBus, GetWorldTM);
_position = newTransform.GetPosition();
}
}

And it delivers me the expected result.

So this is as far as it gets:

GetEntity() returns the entity that the component is attached to.
This is NOT the ‘same’ entity as the entity in the editor. Thats why GetEntityId() won’t return the same ID that you can see in the editor.

However you can reference the properties of the entity over that Id aswell.

Hi Herpaderp :slight_smile: I hope this answer can help you. Load *.cfg to AZ::MeshComponent, how?