Skip to content

Instantly share code, notes, and snippets.

@Stuyk
Created April 16, 2019 16:46
Show Gist options
  • Save Stuyk/e5b1850e52eacbdccf2980625388e119 to your computer and use it in GitHub Desktop.
Save Stuyk/e5b1850e52eacbdccf2980625388e119 to your computer and use it in GitHub Desktop.
SyncHelper.cs (Clientside) <-> ObjectSync.cs (Serverside)
using System;
using System.Collections.Generic;
using System.Text;
using GTANetworkAPI;
namespace WhateverYouWant.ServerClientEvents
{
public class ObjectSync : Script
{
[RemoteEvent("UpdateObjects")]
public void Event_ObjectStreamUpdate(Client client, object[] args)
{
client.SetSharedData("ObjectStream", args[0].ToString());
foreach(Client c in NAPI.Pools.GetAllPlayers())
{
c.TriggerEvent("ObjectStreamUpdate", client.Handle.Value, args[0].ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using RAGE;
using RAGE.Elements;
namespace WhateverYouWant
{
public class SyncHelper : Events.Script
{
public SyncHelper()
{
Events.OnEntityStreamIn += EntityStreamIn;
Events.OnEntityStreamOut += EntityStreamOut;
Events.OnPlayerQuit += OnPlayerQuit;
Events.Add("ObjectStreamUpdate", ObjectStreamUpdate);
}
private void OnPlayerQuit(Player player)
{
EzSync data = player.GetData<EzSync>("AttachedObjects");
if (data == null)
return;
for (int i = 0; i < data.Objects.Count; i++)
data.Objects[i].Delete();
}
private void ObjectStreamUpdate(object[] args)
{
ushort remoteID = Convert.ToUInt16(args[0]);
Player player = Entities.Players.GetAtRemote(remoteID);
EzSync data = player.GetData<EzSync>("AttachedObjects");
string newData = (string)args[1];
var result = JsonConvert.DeserializeObject<EzSync>(newData);
if (data == null)
{
EzSync sync = new EzSync(player);
sync.Objects = result.Objects;
RAGE.Chat.Output("Creating Objects");
foreach (var obj in sync.Objects)
obj.StreamIn();
return;
}
foreach (var obj in data.Objects)
obj.Delete();
data.Objects = result.Objects;
foreach (var obj in data.Objects)
obj.StreamIn();
}
private void EntityStreamOut(Entity entity)
{
if (entity.Type != RAGE.Elements.Type.Player)
return;
EzSync data = entity.GetData<EzSync>("AttachedObjects");
if (data == null)
return;
for(int i = 0; i < data.Objects.Count; i++)
data.Objects[i].Delete();
}
private void EntityStreamIn(Entity entity)
{
if (entity.Type != RAGE.Elements.Type.Player)
return;
var jsonData = entity.GetSharedData("ObjectStream");
if (jsonData == null)
return;
var result = JsonConvert.DeserializeObject<EzSync>(jsonData.ToString());
if (result.Objects.Count <= 0)
return;
var entityData = entity.GetData<EzSync>("AttachedObjects");
if (entityData != null)
{
foreach (var obj in entityData.Objects)
obj.Delete();
entityData.Objects = result.Objects;
} else
{
EzSync ezSync = new EzSync(entity as Player);
ezSync.Objects = result.Objects;
}
foreach (var obj in result.Objects)
obj.StreamIn();
}
private void BriefCaseExample()
{
EzSync ez = new EzSync(Player.LocalPlayer);
EzSyncObject obj = new EzSyncObject(Player.LocalPlayer, "hei_p_attache_case_shut_s", new Vector3(0.15f, 0, 0), new Vector3(0, 0, -90), 0xdead);
ez.AttachObject(obj);
}
}
public class EzSync
{
public List<EzSyncObject> Objects = new List<EzSyncObject>();
public EzSync(Player player)
{
Player.LocalPlayer.SetData("AttachedObjects", this);
}
public void AttachObject(EzSyncObject ezSyncObject)
{
Objects.Add(ezSyncObject);
UpdateData();
}
public void DetachObject(EzSyncObject ezSyncObject)
{
if (!Objects.Contains(ezSyncObject))
return;
Objects.Remove(ezSyncObject);
ezSyncObject.Delete();
UpdateData();
}
public void UpdateData()
{
Events.CallRemote("UpdateObjects", JsonConvert.SerializeObject(this));
}
}
public class EzSyncObject
{
public string UniqueName { get; set; }
public int AttachedTo { get; set; }
public int RemoteID { get; set; }
public uint HashKey { get; set; }
public float OffsetX = 0;
public float OffsetY = 0;
public float OffsetZ = 0;
public float RotX = 0;
public float RotY = 0;
public float RotZ = 0;
public int BoneIndex { get; set; }
public int CreatedObject { get; set; }
public bool IsPed = true;
public bool FixedRotation = true;
public int ObjectID { get; set; }
public EzSyncObject() { }
public EzSyncObject(Player player, string objectName, Vector3 offset, Vector3 rotation, int boneID, bool isPed = true, bool fixedRotation = true, string uniqueName = "")
{
AttachedTo = player.Handle;
RemoteID = player.RemoteId;
HashKey = RAGE.Game.Misc.GetHashKey(objectName);
OffsetX = offset.X;
OffsetY = offset.Y;
OffsetZ = offset.Z;
RotX = rotation.X;
RotY = rotation.Y;
RotZ = rotation.Z;
BoneIndex = RAGE.Game.Ped.GetPedBoneIndex(player.Handle, boneID);
IsPed = isPed;
FixedRotation = fixedRotation;
UniqueName = uniqueName;
}
public void StreamIn()
{
Player target = Entities.Players.All.Find(x => x.RemoteId == RemoteID);
if (target == null)
return;
ObjectID = RAGE.Game.Object.CreateObject(HashKey, target.Position.X, target.Position.Y, target.Position.Z, true, false, false);
RAGE.Game.Entity.AttachEntityToEntity(ObjectID, target.Handle, BoneIndex, OffsetX, OffsetY, OffsetZ, RotX, RotY, RotZ, true, false, false, IsPed, 0, FixedRotation);
}
public void Delete()
{
int reference = ObjectID;
if(!RAGE.Game.Entity.DoesEntityExist(reference))
return;
RAGE.Chat.Output("Deletd obj");
RAGE.Game.Entity.DeleteEntity(ref reference);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment