From c47690ee06bf54a96fb0270c0a1db27c513f2e1f Mon Sep 17 00:00:00 2001 From: Panouklakos <69419914+Panouklakos@users.noreply.github.com> Date: Thu, 18 Jan 2024 16:46:29 +0200 Subject: [PATCH] Example of the REGISTER_PEDHEADSHOT native (#763) * Example of the REGISTER_PEDHEADSHOT native I recently wanted to use this native, which originally had a forum post linked to it, so I just made an example out of it and I believe having it on the native page is better. --------- Co-authored-by: ammonia-cfx <38232208+4mmonium@users.noreply.github.com> --- PED/RegisterPedheadshot.md | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/PED/RegisterPedheadshot.md b/PED/RegisterPedheadshot.md index a8130c2f8..a811e1136 100644 --- a/PED/RegisterPedheadshot.md +++ b/PED/RegisterPedheadshot.md @@ -1,6 +1,7 @@ --- ns: PED --- + ## REGISTER_PEDHEADSHOT ```c @@ -13,10 +14,10 @@ gtaforums.com/topic/885580-ped-headshotmugshot-txd/ ``` ## Parameters -* **ped**: +* **ped**: The ped you want to take the "headshot" of. ## Return value -The Pedheadshot handle. +It returns a Pedheadshot handle, which can be used by natives such as [GET_PEDHEADSHOT_TXD_STRING](https://docs.fivem.net/natives/?_0xDB4EACD4AD0A5D6B) ## Examples ```lua @@ -40,3 +41,32 @@ CreateThread(function() UnregisterPedheadshot(handle) end) ``` + +```js + // This function gets the ped headshot texture and returns an url which can be used in NUI (written in TypeScript) + + export const GetPedHeadShotUrl = (ped: number): Promise => { + // We return a Promise so we can async await it, because registering the ped headshot takes some time. + + return new Promise(async res => { + const handle = RegisterPedheadshot(ped) + + while (!IsPedheadshotReady(handle) || !IsPedheadshotValid(handle)) { + // This is a custom function, not a native, which just returns a promise that is resolved after the ms specified using the setTimeout function + await Delay(10) + } + + const txdString = GetPedheadshotTxdString(handle) + + // We "unload" the handle as we no longer need it + UnregisterPedheadshot(handle) + + // From forum post: https://forum.cfx.re/t/ped-headshot-to-image/4813157/2 + + const pictureUrl = `https://nui-img/${txdString}/${txdString}?v=${Date.now()}` + + // Finally we resolve the Promise! + res(pictureUrl) + }) +} +```