Entity that is invisible when set a parentSolved

Hello, I would like to do spawn a smart alarm on a storage box, and when I do spawn the smart alarm disappears.


 

void OnEntitySpawned(BaseEntity entity)
        {
            BasePlayer player = BasePlayer.FindByID(entity.OwnerID);
            if (player == null)
                return;

            if (entity.ShortPrefabName != "woodbox_deployed")
                return;

            ItemContainer container = new ItemContainer();
            container.ServerInitialize(null, 6);
            container.GiveUID();

            StorageContainer storage = entity as StorageContainer;
            storage.inventory = container;
            storage.inventory.entityOwner = entity;
            storage.isLootable = true;

            storage.SendNetworkUpdateImmediate();

            Create(entity, player);
        }

        void Create(BaseEntity parent, BasePlayer player)
        {
            SmartAlarm alarm = CreateSmartAlarm(parent);

            alarm.SetFlag(BaseEntity.Flags.On, true);
            alarm.SetFlag(BaseEntity.Flags.Reserved8, true);
            alarm.SetFlag(IOEntity.Flag_HasPower, true);
        }

        SmartAlarm CreateSmartAlarm(BaseEntity parent)
        {
            SmartAlarm smartAlarm = (SmartAlarm)GameManager.server.CreateEntity("assets/prefabs/deployable/playerioents/app/smartalarm/smartalarm.prefab");
            smartAlarm.Spawn();
            smartAlarm.SetParent(parent);

            Vector3 pos = parent.transform.localPosition;
            Quaternion rot = parent.transform.localRotation;

            pos.y = pos.y + 0.55f;

            smartAlarm.transform.position = pos;
            smartAlarm.transform.rotation = rot;

            UnityEngine.Object.DestroyImmediate(smartAlarm.GetComponent<DestroyOnGroundMissing>());
            UnityEngine.Object.DestroyImmediate(smartAlarm.GetComponent<GroundWatch>());
            UnityEngine.Object.DestroyImmediate(smartAlarm.GetComponent<BoxCollider>());
            UnityEngine.Object.DestroyImmediate(smartAlarm.GetComponent<InstrumentKeyController>());

            smartAlarm.EnableSaving(true);
            smartAlarm.SendNetworkUpdateImmediate();
            smartAlarm.SendNetworkUpdate();

            return smartAlarm;
        }​

 

I never tried attaching a SmartAlarm.  But sometimes the client doesn't render entities attached to a parent.  It decides not to render things based on their localPosition, as if it's the real position.  Go to map coordinates near 0,0,0 and try attaching it there.  If the SmartAlarm is rendered, that shows it's affected.

There's a plugin that tries to solve it.  But there may be cases where it still doesn't work.

https://umod.org/plugins/parented-entity-render-fix

I mostly noticed it when parenting entities to a moving object.  MiniCopters, etc.

Did you attach it after spawning the box if not try that. Spawn Both then parent

VNVrul4cIFYO1Zv.png Razor

Did you attach it after spawning the box if not try that. Spawn Both then parent

This will work partially.  Will seem normal for a player who is in the network group as the entity spawns.  But for other clients who network to the object later, it can disappear, since they will only receive the localPosition relative to the parent entity.

I have solve the problem, by defining the position of the parent on the SmartAlarm and making it spawn then changed its positions with transform.localPosition.

But another problem occurred when I restart the server it becomes invisible again

Zugzwang

I never tried attaching a SmartAlarm.  But sometimes the client doesn't render entities attached to a parent.  It decides not to render things based on their localPosition, as if it's the real position.  Go to map coordinates near 0,0,0 and try attaching it there.  If the SmartAlarm is rendered, that shows it's affected.

There's a plugin that tries to solve it.  But there may be cases where it still doesn't work.

https://umod.org/plugins/parented-entity-render-fix

I mostly noticed it when parenting entities to a moving object.  MiniCopters, etc.

The plugin is already installed on the server

Would be nice if Facepunch could just fix the client.  It's probably a single line of code. 

Alistair said it's expected behavior, not a bug, but that it's probably something they could improve on in the future.

When using Parented Entity Render Fix, make sure to put the smart alarm short prefab name in the config (not the item short name).

Thanks, I was able to put the smartalarm.prefab in the config file and it spawn correctly

NKXTQs24ExGTuL8.jpg WhiteThunder

Alistair said it's expected behavior, not a bug, but that it's probably something they could improve on in the future.

When using Parented Entity Render Fix, make sure to put the smart alarm short prefab name in the config (not the item short name).

If they know about this and it is 'on purpose', what is the justification for it?  I mean, the client still knows exactly where the entity is. 

Zugzwang

If they know about this and it is 'on purpose', what is the justification for it?  I mean, the client still knows exactly where the entity is. 

Expected doesn't necessarily mean intentional. Most likely, the implementation was simplified for performance reasons (no unnecessary checks or math). LOD scripts are probably invoked very often, perhaps even every frame. Accessing a GameObject's transform property is not free, and accessing position also has a cost. Most likely, the scripts are caching the position in order to do only vector math.

However, that really only explains why they cache, not the reason behind which value they cache. Caching the world position rather than the local position would probably have negligible cost since it would only apply at create, and it could be done somewhat selectively to reduce the performance consequences.

Even when the entity's model isn't rendered, the client can still interact with it, RPC, see lighting effects, etc.  Would seem like the true position is already calculated.  You can attach a TeslaCoil and still see its zapping effect while it's not rendering the model.  And SimpleLights still cast light while invisible.

I'm having a hard time imagining any performance gain.  The client knows exactly where the entity is the whole time.

Locked automatically