Jump to content

Looping through the vehicles returns only the last vehicle created


Recommended Posts

Hello!

I am doing a police speedcamera script, but there is one problem I don't know how to fix. I have tried several methods, setting suspectVehicle to nil several times, but it's not working as I want it to work and I don't understand what could be the problem. When I'm trying to get the speed of a vehicle with the speedcam, it only works with the vehicle that I created last, it doesn't work with the other existing vehicles, and I tried restarting the resource after creating the vehicle, deleting vehicles, etc, but it doesn't work. Sometimes it works if I go a little farther from the last created vehicle, it detects the other vehicle that is for example next to it, but this is not how I want it to work. Could somebody help me fixing it? Here's my code:

local sx, sy = guiGetScreenSize()

local policeSpeedCameraState = false
local suspectVehicle = nil
local suspectVehicleVelocity = 0

local speedCamDistance = 30

local cameraLineDistance, cameraLineWidth = 200, 10
local cameraLineColor = tocolor(255,255,255,255)

local randomCameraNumber = 0

function isVehicleWithinSpeedCamSight(vehicle)
	if vehicle and isElement(vehicle) and getElementType(vehicle) == "vehicle" then
		local vehiclex,vehicley,vehiclez = getElementPosition(vehicle)
		local screenposx, screenposy = getScreenFromWorldPosition(vehiclex,vehicley,vehiclez)
		if screenposx and screenposy then
			if screenposx >= sx/2-cameraLineDistance and screenposx <= sx/2+cameraLineDistance+cameraLineWidth and screenposy >= sy/2-cameraLineDistance and screenposy <=  sy/2+cameraLineDistance+cameraLineWidth then
				return true
			else
				return false
			end
		end
	end
	return false
end

function activatePoliceSpeedCamera(cmd, state)
	--if getPedOccupiedVehicleSeat(localPlayer) == 1 then
		if policeSpeedCameraState ~= state then
			if state == "true" then
				policeSpeedCameraState = true
				addEventHandler("onClientRender", getRootElement(), renderSpeedCamera)
				setDashboardCameraForPlayer(localPlayer)
				randomCameraNumber = math.random(100,999)
			elseif state == "false" then
				policeSpeedCameraState = false
				removeEventHandler("onClientRender", getRootElement(), renderSpeedCamera)
				setCameraTarget(localPlayer)
				exports["cg_hud"]:toggleHUD(true)
				exports["cg_radar"]:toggleRadar(true)
			end
		end
	--else
	--	outputChatBox("csak anyósból")
	--end
end
addCommandHandler("speedcam", activatePoliceSpeedCamera)

local barY = 0
function renderSpeedCamera()
	local occupiedVehicle = getPedOccupiedVehicle(localPlayer)
	if occupiedVehicle then
		if policeSpeedCameraState then
			for k, v in pairs(getElementsByType("vehicle")) do
				if v ~= occupiedVehicle then
					if isElementOnScreen(v) then
						if isVehicleWithinSpeedCamSight(v) then
							--if getVehicleController(v) then
								local x,y,z = getElementPosition(occupiedVehicle)
								local tx,ty,tz = getElementPosition(v)
								if getDistanceBetweenPoints3D(x, y, z, tx,ty,tz) <= speedCamDistance then
									local clear = isLineOfSightClear (x,y,z,tx,ty,tz,true,false,false,false,false,false,false)
									if clear then
										local vehicleVelocity = exports["cg_vehiclescripting"]:getActualVelocity(v, "mph")
										if vehicleVelocity then
											suspectVehicle = v
											suspectVehicleVelocity = vehicleVelocity
										end
									end
								end
							--end
						else
							suspectVehicle = nil
							suspectVehicleVelocity = "N/A"
						end
					end
				end
			end
		end
	end

	exports["cg_hud"]:toggleHUD(false)
	exports["cg_radar"]:toggleRadar(false)
	
	
	dxDrawRectangle(0,0,sx,sy,tocolor(20,140,245,50))
	local barNumbers = math.ceil(sy/10)
	for i=1, barNumbers do
		barY = barY + 0.01
		if barY >= barNumbers then
			barY = 15
		end
		dxDrawRectangle(0,((i-1)*30) + barY - barNumbers - 15,sx,15,tocolor(255,255,255,25*math.abs(getTickCount() % 1000 - 2000) / 1000))
	end
	
	-- kamera körvonal
	dxDrawLine(sx/2-cameraLineDistance, sy/2-cameraLineDistance-cameraLineWidth/2, sx/2-cameraLineDistance, sy/2, cameraLineColor, cameraLineWidth)
	dxDrawLine(sx/2-cameraLineDistance-cameraLineWidth/2, sy/2-cameraLineDistance, sx/2, sy/2-cameraLineDistance, cameraLineColor, cameraLineWidth)
	dxDrawLine(sx/2+cameraLineDistance, sy/2+cameraLineDistance+cameraLineWidth/2, sx/2+cameraLineDistance, sy/2, cameraLineColor, cameraLineWidth)
	dxDrawLine(sx/2+cameraLineDistance+cameraLineWidth/2, sy/2+cameraLineDistance, sx/2, sy/2+cameraLineDistance, cameraLineColor, cameraLineWidth)
	--outputChatBox(tostring(barY))
	
	if suspectVehicle and isElement(suspectVehicle) then
		local vehicleX,vehicleY,vehicleZ = getElementPosition(getPedOccupiedVehicle(localPlayer))
		local targetX,targetY,targetZ = getElementPosition(suspectVehicle)
		--[[local _, _, rotZ = getElementRotation(suspectVehicle)

		local angle = math.rad(10 + rotZ)

		local cornerX, cornerY = targetX, targetY
		local pointX, pointY = targetX - 2, targetY - 2

		local rotatedX = math.cos(angle) * (pointX - cornerX) - math.sin(angle) * (pointY- cornerY) + cornerX
		local rotatedY = math.sin(angle) * (pointX - cornerX) + math.cos(angle) * (pointY - cornerY) + cornerY]]
		
		--if gettingSuspectSpeed then
			local screenX, screenY = getScreenFromWorldPosition(targetX,targetY,targetZ,25,true)
			if screenX and screenY then
				--dxDrawText("Mérés",  screenX, screenY-50, 0, 0, tocolor(255, 0, 0, 255), 1.5, "default-bold")
				local crosshairSize = 25
				dxDrawLine(screenX-crosshairSize, screenY, screenX+crosshairSize, screenY, tocolor(255,255,255,255), cameraLineWidth/2)
				dxDrawLine(screenX, screenY-crosshairSize, screenX, screenY+crosshairSize, tocolor(255,255,255,255), cameraLineWidth/2)
			end
		--end
		
		-- adatok
		if getDistanceBetweenPoints3D(vehicleX,vehicleY,vehicleZ, targetX,targetY, targetZ) <= speedCamDistance then
			if tonumber(suspectVehicleVelocity) then
				suspectVehicleVelocity = tostring(math.floor(suspectVehicleVelocity)) .. " MPH"
			end
		else
			suspectVehicleVelocity = "N/A"
		end	
	end
	dxDrawText(tostring(suspectVehicleVelocity), sx/2+cameraLineWidth, sy/2+cameraLineDistance+cameraLineWidth, 0, 0, tocolor(255,255,255,255), 2, "default")
	
	dxDrawText("LAPD", sx/2+cameraLineDistance/2, sy/2-cameraLineDistance-cameraLineWidth*6, 0, 0, tocolor(255,255,255,255), 2, "default")
	dxDrawText("S", sx/2+cameraLineDistance/2, sy/2-cameraLineDistance-cameraLineWidth*4, 0, 0, tocolor(255,255,255,255), 2, "default")
	
	dxDrawText("00" .. randomCameraNumber, sx/2-cameraLineDistance, sy/2-cameraLineDistance-cameraLineWidth*6, 0, 0, tocolor(255,255,255,255), 2, "default")
	
	local timehour, timeminute = getTime()
	if timehour < 10 then
		timehour = "0" .. timehour
	end
	
	if timeminute < 10 then
		timeminute = "0" .. timeminute
	end
	dxDrawText(timehour .. ":" .. timeminute, sx/2-cameraLineWidth/2, sy/2-cameraLineDistance-cameraLineWidth*4, sx/2-cameraLineWidth/2, sy/2-cameraLineDistance-cameraLineWidth*4, tocolor(255,255,255,255), 2, "default", "center")
end

function setDashboardCameraForPlayer(player)
	local myVehicle = getPedOccupiedVehicle(player)
	if myVehicle then
		local camera = getCamera()
		setElementPosition(camera, 0,0,0)
		setElementDimension(camera, getElementDimension(myVehicle))
		setElementInterior(camera, getElementInterior(myVehicle))
		attachElements(camera, myVehicle, 0.25,0.75,0.6, 0,0,0)
	else
		return
	end
end

function disableSpeedCamOnVehicleExit(player, seat)
	if player == localPlayer and seat == 1 and policeSpeedCameraState then
		policeSpeedCameraState = false
		removeEventHandler("onClientRender", getRootElement(), renderSpeedCamera)
		setCameraTarget(localPlayer)
		exports["cg_hud"]:toggleHUD(true)
		exports["cg_radar"]:toggleRadar(true)
	end
end
addEventHandler("onClientVehicleStartExit", getRootElement(), disableSpeedCamOnVehicleExit)

 

Edited by Dzsozi
Link to comment

Lines 74 through to 76 are causing your problem. Set the variables before going into the for-loop instead, and in the loop only set when you find the vehicle you want.

What happens in your code is, for example, there are 3 cars, and the 2nd one is in sight of the camera and everything. The script goes through from 1 to 3 as follows:

Spoiler

Vehicle 1 cannot be the target, so make the target variable a nil value
Vehicle 2 is our target, so make the target variable this vehicle
Vehicle 3 is not our target, so set the target variable to nil.

At the end of this, as you can see, the 3rd vehicle changed the variable of our correct target vehicle.

Instead, this should be as follows:

Spoiler

Set target variable to nil
Vehicle 1 cannot be our target, so ignore it (leaving the variable as nil)
Vehicle 2 is our target, so set the target variable to this vehicle
Vehicle 3 isn't our target, so don't change the target variable (leaving it as Vehicle 2)

 

Edited by MrTasty
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...