Fix the component toggler (#37309)

* Make the ComponentToggle remember what entity it gave components to

* fix the null problem by just ignoring the null problem

* Add documentation to the new datafield + removing the "= null" that is not necessary

* small fixes and cleaning the code

* whitespace my beloved

* wait, I dont need those lines, why did I add them?
This commit is contained in:
Samuka-C
2025-05-09 15:00:58 -03:00
committed by GitHub
parent b769ab0f7e
commit fbe05977d5
2 changed files with 24 additions and 5 deletions

View File

@@ -16,13 +16,26 @@ public sealed class ComponentTogglerSystem : EntitySystem
private void OnToggled(Entity<ComponentTogglerComponent> ent, ref ItemToggledEvent args)
{
var target = ent.Comp.Parent ? Transform(ent).ParentUid : ent.Owner;
if (TerminatingOrDeleted(target))
return;
if (args.Activated)
{
var target = ent.Comp.Parent ? Transform(ent).ParentUid : ent.Owner;
if (TerminatingOrDeleted(target))
return;
ent.Comp.Target = target;
EntityManager.AddComponents(target, ent.Comp.Components);
}
else
EntityManager.RemoveComponents(target, ent.Comp.RemoveComponents ?? ent.Comp.Components);
{
if (ent.Comp.Target == null)
return;
if (TerminatingOrDeleted(ent.Comp.Target.Value))
return;
EntityManager.RemoveComponents(ent.Comp.Target.Value, ent.Comp.RemoveComponents ?? ent.Comp.Components);
}
}
}

View File

@@ -29,4 +29,10 @@ public sealed partial class ComponentTogglerComponent : Component
/// </summary>
[DataField]
public bool Parent;
// <summary>
// It holds the entity that the component gave the component to, so it can remove from it even if it changes parent.
// </summary>
[DataField]
public EntityUid? Target;
}