Useful snippet: getting the top left of the minimap in screen coordinates

Originally published at: https://cookbook.fivem.net/2019/08/12/useful-snippet-getting-the-top-left-of-the-minimap-in-screen-coordinates/

SetScriptGfxAlign(string.byte(‘L’), string.byte(‘B’)) local minimapTopX, minimapTopY = GetScriptGfxPosition(-0.0045, -(0.002 + 0.188888)) ResetScriptGfxAlign() local w, h = GetActiveScreenResolution() return { w * minimapTopX, h * minimapTopY } This could be useful if you want to align things to NUI or other screen-space draws, and works on pretty much any aspect ratio. The magic numbers here are the…

8 Likes

Hopefully this serves as a more reliable method than

Especially on really weird resolution

2 Likes

So, commenting because I’ve tried out both of these, started off with using the Minimap Anchor Script (which for the most part works perfectly, minus “weird” resolutions); now, I could be wrong or be doing something wrong in my tests, but when using the snippet with a 16:9 aspect ratio screen resolution (such as 1920 by 1080), it works perfectly/flawlessly obtaining the top-left of the minimap in pixels (from the bottom-right-hand corner?), which you need to divide by 10 (or 2) a few times till it is less than or equal to 1.0 to be of any use with the DrawText native, which gets the text spot on. However, for me at least, it seems whenever I change the screen resolution to a 4:3 aspect ratio screen resolution such as 800 by 600, it is no longer properly aligned with the top-left of the minimap (like I said could be a fault in my testing but I don’t think so), and same goes for even “weirder” screen resolutions like wide (2560 by 1080) and ultrawide (3840 by 1080) resolutions, whatever aspect ratios those are.

Reason for this post is to hopefully get more of an understanding of this as well as to potentially help increase the adoption rate of this, the following code obviously isn’t the cleanest/best (made it just for testing/debugging purposes), but anyone who wants to jump on and help create/develop a more “common” and universal alignment method for a bunch of other scripts already out there to potentially begin making use of, add to this discussion or private/direct message me.

Sample Code:

function GetNewMinimapAnchor()
    SetScriptGfxAlign(string.byte('L'), string.byte('B'))
    local minimapTopX, minimapTopY = GetScriptGfxPosition(-0.0045, 0.002 + (-0.188888))
    ResetScriptGfxAlign()
    local w, h = GetActiveScreenResolution()
    return { w * minimapTopX, h * minimapTopY }
end

function DrawTest()
	local minimapTopX, minimapTopY = table.unpack(GetNewMinimapAnchor())
	while minimapTopX > 1.0 or minimapTopY > 1.0 do
		minimapTopX, minimapTopY = minimapTopX/10, minimapTopY/10
	end
	
	
	
	--local ui = GetMinimapAnchor() -- Minimap Anchor Script's Version
	
	--print(minimapTopX..","..ui.x.." | "..minimapTopY..","..ui.y.." | "..ui.width..","..ui.height) -- Not usable without Minimap Anchor Script Reference being included

	print(minimapTopX.." | "..minimapTopY) -- Display the values in client F8 console, not really needed but here for sanity checks
	
	drawTxtRaw(minimapTopX, minimapTopY, 1.0, 1.0, 0.5, "Test", 255, 255, 255, 255) -- The 1.0 values here don't do anything
end

function drawTxtRaw(x,y ,width,height,scale, text, r,g,b,a) -- The width and height variables here don't do anything
    SetTextFont(4)
    SetTextProportional(0)
    SetTextScale(scale, scale)
    SetTextColour(r, g, b, a)
    SetTextDropShadow(0, 0, 0, 0,255)
    SetTextEdge(2, 0, 0, 0, 255)
    SetTextDropShadow()
    SetTextOutline()
	
	--SetTextCentre(true)
	
    SetTextEntry("STRING")
    AddTextComponentString(text)
	
	--ClearDrawOrigin()
	
    DrawText(x, y)
end

Honestly, the easier workaround would probably just be adding or subtracting some pixels for a given screen width using the Minimap Anchor Script (aka if screen width equals this then add this amount of pixels to the value the script provides), but this potentially has the potential to be an even easier method that just works without needing manual definitions to be given for any non-standard screen resolution that pops up, and I’d like to see that happen if not be the one to make that happen for everyone, as it can potentially overcome a lot of the issues that stem from using those screen resolutions or aspect ratios.

If you want to use this for script drawing natives and not NUI, just don’t multiply it. :stuck_out_tongue:

where exactly do we put this

Is this the reeason my minimap resolution is so bad?..

1 Like

Unrelated bump, what is the HUD combo you are using there? Slick.

1 Like