When a var mesh_comonent is used for EntityEnteredEvent and EntityExitedEvent, these events stop getting called when the var mesh_component is changed unless the events are looped however, when these are looped, the event gets called too much and logic is broken
Please select what you are reporting on:
Verse
What Type of Bug are you experiencing?
Verse
Steps to Reproduce
Create this setup:
if. var Mesh : mesh_component = E.GetComponent[mesh_component] then:
Mesh.EntityEnteredEvent.Subscribe(Function1)
Mesh.EntityExitedEvent.Subscribe(Function2)
NewEntity := entity{}
MeshComp := NEWMESH:
Entity := NewEntity, Material := AMaterial
set CompArray = array {MeshComp}
ChildEntities : []component = E.GetComponents()
for (Index := 0..ChildEntities.Length -1):
if. Child := ChildEntities[Index]
then. Child.RemoveFromEntity()
E.AddComponents(CompArray)
SpawnedMeshComp := E.FindDescendantComponents(mesh_component)
for (MeshComponent : SpawnedMeshComp):
set SpawnedMesh = MeshComponent
Expected Result
The subscribable EntityEnteredEvent and EntityExitedEvent events continue to fire even after the mesh is changed from its original
Observed Result
The events are not fired after the mesh is changed
Platform(s)
Any
Additional Notes
looping the events does get them to update but this can’t be used as the event ends up being fired too many times
Thank you for bringing this to our attention. After reviewing with the team, we believe this behavior is working as intended. Since a mesh_component is being removed and a new one added, you’ll need to re-subscribe to the Entity Entered event. Please let us know if that doesn’t resolve your issue, we’ll be happy to take another look. Thanks again
My issue is that if I resubscribe it, because its in a loop in the logic I’m actually using, when the event is fired, it fires multiple times and messes with logic, if there is a way to prevent this I would love to know. I would also argue that because it’s using a var, it should automatically resubscribe when the var changes as it just makes sense. I’ll send the whole function I’m using (I’m new to SG so it might not be the best code):
Thanks for sharing the details of your setup. The behavior you’re seeing is expected given how the system works: when the underlying component is replaced, the old instance is destroyed and a brand-new one is instantiated. Because of this, any event subscriptions tied to the previous instance are no longer valid, which is why you need to resubscribe to the event on the new component.