[Solved/OneSync]CreateWeaponObject not sync well when use with attach

Client

Using canary?
No

Windows version:
20H2 19042.804

Server

Operating system:
Windows Server 2012 R2 Datacenter

Artifact version:
Server: 3539

Incident

Summary:
CreateWeaponObject not sync well when use with attach. The attached entity just floating around.

Expected behavior:
API.CreateWeaponObject should sync normally, just like World.CreateProp.

Actual behavior:
image - https://i.imgur.com/qVcCIoF.mp4 (I’ve tested recently and it still behave like this video)

Steps to reproduce:

using CitizenFX.Core;
using CitizenFX.Core.Native;

            // create weapon object
            var weaponHandle = API.CreateWeaponObject(
                (uint) weapon.Hash,
                1,
                Game.PlayerPed.Position.X,
                Game.PlayerPed.Position.Y,
                Game.PlayerPed.Position.Z - 50,
                true,
                1f,
                0);

            var entity = Entity.FromHandle(weaponHandle);

            // set tint
            API.SetWeaponObjectTintIndex(weaponHandle, (int) weapon.Tint);

            // misc
            entity.IsPersistent = true;
            API.SetEntityCollision(weaponHandle, true, false);
            
            // attach
            entity.AttachTo(
                Game.PlayerPed.Bones[info.Bone],
                info.Position,
                info.Rotation);

            // register to network
            API.NetworkRegisterEntityAsNetworked(weaponHandle);
            var weaponNetId = API.NetworkGetNetworkIdFromEntity(weaponHandle);
            API.SetNetworkIdCanMigrate(weaponNetId, true);
            API.SetNetworkIdExistsOnAllMachines(weaponNetId, true);

Server/Client?
Client-Side Script (C#)

Files for repro (if any):
github repo

Any additional info:
It seems to be a long standing issue and I’ve tested recent days and it still behave buggy. Or maybe it’s rather a rarely used API?

2 Likes

What makes you think it should?

Game code explicitly does not create a netobj for these entities, so it’s equivalent to passing false to isNetwork in CreateObject, and Rockstar does not use this in any network scripts (except for locally rendering a weapon in cutscenes played client-side).

You could just call NETWORK_REGISTER_ENTITY_AS_NETWORKED - Cfx.re Docs on the returned entity to make a netobj manually (which it seems you do, but then also you’re calling a few other random natives, and you’re using it only after attaching the weapon), if that doesn’t work that may an unrelated issue but this native is not meant to be used in MP anyway.

Did this even work in non-1s?

2 Likes

No, you’re right, CreateWeaponObject is not meant to be used in network scripts.

I’ve tried adjust order of the code / call NETWORK_REGISTER_ENTITY_AS_NETWORKED only and still behaves the same.

Nope, non-1s has same issue also.

Overall, entity created by this native is not meant to be used in MP, or at least when using with attachTo.
I’ll try figure this out with newly-added StateBag feature.

3 Likes