[SOLVED] Drawing UI and Inverting the for loop

Hey,
I’m posting this because I have no idea why this isn’t working…
I’m trying to make a gear display for my FiveM server that would show players what their current gear is.

image

For this I’ve used a for loop to cycle through the vehicle’s gears and draw from right to left (so they will never go outside the bounds of the minimap)

Here’s my code:

local gear = GetVehicleCurrentGear(MyPedVeh)
local highGear = GetVehicleHighGear(MyPedVeh)

if gear ~= nil then
	local xOffset = 0.645
	local tic = 1.310
	local toc = 1.330
	local YPos = UI.y + toc
					
	for i = highGear, 0, -1 do
		if YPos == UI.y + tic then
			YPos = UI.y + toc
		else
			YPos = UI.y + tic
		end
		if gear == i and not VehStopped then
			if i == 0 then
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, "R", 255, 255, 255, 255)
			else
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, tostring(i), 255, 255, 255, 255)
			end
		else
			if i == 0 then
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, "R", 255, 255, 255, 150)
			else
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, tostring(i), 255, 255, 255, 150)
			end
		end
	end
end

What I don’t understand is the for loop.
I originally went with:
for i = 0, highGear, 1 do

As far as I’m concerned that should go from 0 (the reverse gear) to the highest gear available. I realized that when drawing the gears from right ot left this drew them in the wrong direction (from highest to lowest) and wanted the opposite.

So I inverted the loop:
for i = highGear, 0, -1 do

This, as far as I can grasp should get the gears from the highest to the lowest, drawing them from right to left resulting in the highest gear being on the edge and the lowest being inside the minimap. It should (as far as I understand) draw them in the exact opposite order as they draw in my screenshot (above)…

But they don’t… They still draw the wrong way around. Inverting my for loop seems to have changed nothing and I cannot grasp why.

Any help would be greatly appreciated.
It’s not the hugest issue ever, and I’m sure I could live with the gears being drawn from highest to lowest, but it’s a shame in my opinion.

(I realize that the rest of my code is not hugely efficient, but I’m trying to get a lot done as soon as possible, so I’m botching stuff at the moment)

Thank you in advance for any help!

When you changed the loop all you did was change the order of the draw calls. You are deriving x and the label from i which means it doesn’t matter which direction the loop goes. If you want to reverse the label you can do this instead.

local inv = highGear - i
if inv == 0 then
	drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, "R", 255, 255, 255, 255)
else
	drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, tostring(inv), 255, 255, 255, 255)
end
2 Likes

Oh thank you.
I was thinking about it all wrong. I was off-setting more after every call, I was off-setting by a multiple of what gear I was on.

I knew it had to be something silly :joy:

Thanks for your help!

EDIT:
Having tested it I can confirm it works!

image

My code now (for anybody interested):

if gear ~= nil then
	local xOffset = 0.645
	local tic = 1.310
	local toc = 1.330
	local YPos = UI.y + toc
					
	for i = highGear, 0, -1 do
		local inverse = highGear - i
		if YPos == UI.y + tic then
			YPos = UI.y + toc
		else
			YPos = UI.y + tic
		end
		if gear == inverse and not VehStopped then
			if inverse == 0 then
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, "R", 255, 255, 255, 255)
			else
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, tostring(inverse), 255, 255, 255, 255)				end
		else
			if inverse == 0 then
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, "R", 255, 255, 255, 150)
			else
				drawTxt((UI.x + xOffset) - i/200, YPos, 1.0,1.0,0.50, tostring(inverse), 255, 255, 255, 150)				end
		end
	end
end