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.
(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]
----------