[Useful] Various Utility Functions

I thought maybe it’d be a good idea to create a thread where we can collect various useful functions, hopefully this might reduce the amount of duplicate threads asking for how to do this or that. Feel free to contribute to this thread and I will try and keep the OP updated as much as possible.

Preferably code that is commented so people can figure out how it works without using this as some sort of support thread. I’ve made some functions which I’ve used for my own needs, so I’ll start by populating this thread with those.

If you have something you want to share, either reply to this thread or send me a message.

Please do note that my intention with this thread is to try and create a collection of useful functions which the community may benefit from. It is not intended for people to try and prove themselves as being better than others. I don’t wanna see no complaining about someone writing their code differently.

Edit: That feel when you can’t use preformatted text inside a spoiler. :rolling_eyes:


(Client) FlashEntityAlpha
This flashes the entity’s alpha, currently the alpha values are hardcoded but it’d be an easy change to make them controlled via parameters.

[details=FlashEntityAlpha(entityToFlash, timesToFlash, flashInterval)]```
–[[
FlashEntityAlpha(entityToFlash, timesToFlash, flashInterval)
Flashes the alpha of an entity.

entityToFlash: Entity that will have it's alpha changed.
	default: nil, must be specified or it returns false
timesToFlash: Number of times it should flash. IE, 10 means it will be transparent 5 times
	default: 10
flashInterval: How long it will remain at reduced alpha. Specify in milliseconds.
	default: 150ms

]]

function FlashEntityAlpha(entityToFlash, timesToFlash, flashInterval)
– First and foremost, make sure we were given an entity
if(IsAnEntity(entityToFlash)) then
– Make sure we got some valid input, otherwise use defaults
timesToFlash = (type(timesToFlash) == “number” and timesToFlash or 10)
flashInterval = (type(flashInterval) == “number” and flashInterval or 150)
– Prepare our counter variable
local flashCount = 0
– Let’s use an asynchronous thread
Citizen.CreateThread(function()
while (true) do
Citizen.Wait(flashInterval)
– Make sure the entity is still valid before we change the alpha
if(IsAnEntity(entityToFlash)) then
– Check if flashCount is even or an odd number. (if it equals 0, it’s even)
if(flashCount % 2 == 0) then
SetEntityAlpha(entityToFlash, 150)
flashCount = flashCount+1
else
SetEntityAlpha(entityToFlash, 255)
flashCount = flashCount+1
end
if(flashCount == timesToFlash) then
– When we’ve flashed the specified amount of times, stop the function. Reset alpha just in case.
SetEntityAlpha(entityToFlash, 255)
return
end
end
end
end)
else
– We were not given an entity, so return false
return false
end
end

----------
**(Client) Miscellaneous Interface-Related Functions**
Contains various functions to help with interface-related interaction. Such as showing the cursor, getting the relative cursor position, setting UI focus and checking if the cursor is within a certain area.
[details=**Miscellaneous Interface-Related Functions**]```
--[[
		Functions for User Interface Mouse Control
]]

local isCursorShowing = false
local uiHasFocus = false

-- Changes the isCursorShowing variable which controls whether the mouse is shown or not
function ShowCursor(bEnabled, bUIFocus)
	if(bEnabled) then
		isCursorShowing = true
	else
		isCursorShowing = false
	end
	-- Optional argument, so we can basically do SetUIFocused with this function
	if(bUIFocus == true) then
		uiHasFocus = true
	end
end

-- Returns whether the cursor is currently showing or not
function IsCursorShowing()
	return isCursorShowing
end

-- Sets the UI as focused, disables mouse input such as looking around
function SetUIFocused(bEnabled)
	if(bEnabled) then
		uiHasFocus = true
	else
		uiHasFocus = false
	end
end

-- Returns whether the UI has focus or not
function IsUIFocused()
	return uiHasFocus
end

-- Gets the cursor position, relative values. 0.0, 0.0 = top left corner. 1.0, 1.0 = bottom right corner.
function GetCursorPosition()
	if(isCursorShowing) then
		local cursorX, cursorY = GetControlNormal(0, 239), GetControlNormal(0, 240)
		return cursorX, cursorY
	end
end

-- Checks if the cursor is within specified area. Useful for simulating button mouse hovering
function IsCursorWithinArea(posX, posY, sizeX, sizeY)
	if(isCursorShowing) then
		local cursorX, cursorY = GetCursorPosition()
		if(cursorX >= posX and cursorX <= posX+sizeX) and (cursorY >= posY and cursorY <= posY+sizeY) then
			return true
		else
			return false
		end
	else
		return false
	end
end

-- Async thread that does all the magic
Citizen.CreateThread(function()
	while (true) do
		Citizen.Wait(0)
		-- Displays the Cursor
		if(isCursorShowing) then
			ShowCursorThisFrame()
		end
		-- UI Focus
		if(uiHasFocus) then
			DisableControlAction(0, 1, uiHasFocus)		-- Mouse Look, Left/Right
			DisableControlAction(0, 2, uiHasFocus)		-- Mouse Look, Up/Down
			DisableControlAction(0, 142, uiHasFocus)	-- Right Click
		else
			EnableControlAction(0, 1, not uiHasFocus)	-- Mouse Look, Left/Right
			EnableControlAction(0, 2, not uiHasFocus)	-- Mouse Look, Up/Down
			EnableControlAction(0, 142, not uiHasFocus)	-- Right Click
		end
	end
end)
```[/details]
----------
10 Likes

Nice release thanks cant wait to install it on my server

Uhm, it’s not a resource. Just a list of various functions that some may or my not find useful. But nonetheless, you’re welcome. :slight_smile:

Do you think there is a way to make the mouse not be under the HTML renderings

1 Like

Provably a dumb question, but what the hell is an entities alpha?

Transparancy :slight_smile:

@KRISFR +1
Currently, I’m displaying the cursor via JavaScript, I haven’t found a way of displaying the native cursor above the HTML UI. I’ve read on another GTA MP project, they have the same issue and the dev team is working on it, so if the browser part is similar to FiveM’s, it may be possible, but I think it would require a modification of the FiveM client. It’s only suppositions, someone correct me if I’m wrong !

function isNearArea(x,y,z,radius) -- True if local player is near area, else false
	local x2, y2, z2 = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
	local dist = GetDistanceBetweenCoords(x, y, z, x2, y2, z2, false)
	if dist > radius then
		return false
	else
		return true
	end
end
function CreatePedWithHashAtCoordsAndReturn(model, x, y, z) -- create Ped at coords with model and return it
    RequestModel(model)
    hash = GetHashKey( model)
    local i = 0
    while not HasModelLoaded(hash) and i <= 5 do -- On attend qu'il finisse de charger le modèle
        Wait(200)
        i = i+1
    end

    if i == 5 then
        return "Error : Hash not loaded"
    else
        Ped = CreatePed(5, hash, x, y, z, 0.0 ,true)
    end
    return Ped
end

The following isn’t working, if someone can do something with that, let me know :

function IsSpecificPedHashNearPed(player, model, radius)
	--Citizen.Trace(tostring(model))
	local playerCoords = GetEntityCoords(GetPlayerPed(-1), true)
 	Count , nearPed = GetClosestPed(playerCoords.x, playerCoords.y, playerCoords.z, 99.99,1,1,5)
	--Citizen.Trace("COUNT :  "..tostring(Count)) -- false
	--Citizen.Trace("ENTITY1:  "..tostring(entity1)) -- 0
	if nearPed == nil then
		return false
	end
	hash = GetHashKey(model)
	local isSpecificPed = IsPedInModel(nearPed, hash)
	--Citizen.Trace("ISSPECIFICPED :  "..tostring(isSpecificPed))
	if isSpecificPed then
		return true
	else
		return false
	end
end

Maybe you should use the wiki and add them aswell ? :wink: