Skip to content

Commit

Permalink
feat(client): Add Entity.getSyncInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Jan 7, 2024
1 parent 56056ab commit a6f8fb5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
39 changes: 39 additions & 0 deletions client/src/classes/Entity.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
#include "Class.h"
#include "cpp-sdk/ICore.h"

static void GetSyncInfo(js::FunctionContext& ctx)
{
if(!ctx.CheckThis()) return;
if(!ctx.CheckArgCount(1)) return;
alt::IEntity* entity = ctx.GetThisObject<alt::IEntity>();

const auto syncInfo = entity->GetSyncInfo();

js::Object obj;
obj.Set("active", syncInfo.active);
obj.Set("receivedTick", syncInfo.receivedTick);
obj.Set("fullyReceivedTick", syncInfo.fullyReceivedTick);
obj.Set("sendTick", syncInfo.sendTick);
obj.Set("ackedSendTick", syncInfo.ackedSendTick);
obj.Set("propertyCount", syncInfo.propertyCount);
obj.Set("componentCount", syncInfo.componentCount);

js::Array componentPropertyIndex(syncInfo.componentCount);
uint32_t lastPropertyIdx = 0;
for(uint32_t i = 0; i < syncInfo.componentCount; i++)
{
const uint32_t endIdx = i == syncInfo.componentCount - 1 ? syncInfo.propertyCount : syncInfo.componentPropertyIndex[i];

js::Array propertiesUpdateTick(static_cast<int>(endIdx - lastPropertyIdx));
for (uint32_t j = lastPropertyIdx; j < endIdx; j++)
{
propertiesUpdateTick.Set(j - lastPropertyIdx, syncInfo.propertiesUpdateTick[j]);
}

componentPropertyIndex.Set(i, propertiesUpdateTick);
lastPropertyIdx = endIdx;
}

obj.Set("propertyUpdateTicks", componentPropertyIndex);
ctx.Return(obj);
}

static void GetByScriptID(js::FunctionContext& ctx)
{
if(!ctx.CheckArgCount(1)) return;
Expand All @@ -20,5 +57,7 @@ extern js::Class entityClass("Entity", &sharedEntityClass, nullptr, [](js::Class
{
tpl.Property<&alt::IEntity::GetScriptID>("scriptID");

tpl.Method("getSyncInfo", GetSyncInfo);

tpl.StaticFunction("getByScriptID", &GetByScriptID);
});
22 changes: 19 additions & 3 deletions types/client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,15 @@ declare module "@altv/client" {
get model(): number;
readonly netOwner?: Player;
readonly visible: boolean;
frozen: boolean;
readonly isStreamedIn: boolean;
readonly streamSyncedMeta: Readonly<altShared.EntityStreamSyncedMeta>;

frozen: boolean;
rot: altShared.Vector3;

readonly streamSyncedMeta: Readonly<altShared.EntityStreamSyncedMeta>;
getSyncInfo(): Data.ISyncInfo;

static readonly all: ReadonlyArray<Entity>;

static getByScriptID(scriptId: number): Entity | null;
}

Expand Down Expand Up @@ -1412,6 +1412,22 @@ declare module "@altv/client" {
static create(opts: altShared.ColShapePolygonCreateOptions): ColShapePolygon;
}

export namespace Data {
export interface ISyncInfo {
readonly active: boolean;
readonly receivedTick: number;
readonly fullyReceivedTick: number;
readonly sendTick: number;
readonly ackedSendTick: number;
readonly propertyCount: number;
readonly componentCount: number;
/**
* 2D array of property update ticks grouped by component
*/
readonly propertyUpdateTicks: number[][];
}
}

export namespace Drawing {
export function drawText2dThisFrame(text: string, pos2d?: altShared.IVector2, font?: Number, scale?: Number, color?: altShared.IRGBA, outline?: boolean, dropShadow?: boolean, textAlign?: number): void;
export function drawText2d(text: string, pos2d?: altShared.IVector2, font?: Number, scale?: Number, color?: altShared.IRGBA, outline?: boolean, dropShadow?: boolean, textAlign?: number): altShared.Timers.EveryTick;
Expand Down
2 changes: 1 addition & 1 deletion types/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altv/client",
"version": "0.0.19",
"version": "0.0.20",
"description": "This package contains the type definitions for the alt:V JS module v2 client types",
"types": "index.d.ts",
"files": [
Expand Down

0 comments on commit a6f8fb5

Please sign in to comment.