Jump to content

Check if vehicle is nearby by vehicle's plate?


Recommended Posts

I want to check if the selected vehicle is nearby by the vehicle's plate.

I'm getting the vehicle's plate text from client-side gui (gridlist selection)  then I query that plate from mysql in server-side and want to check if that is nearby or not.

function SetVehicleSellingDB(vehPlate)
local query = dbQuery(con, "SELECT * FROM vehicle WHERE plate=?", vehPlate)
local result = dbPoll(query, -1)
if result then
	local vehicleplate = result[1]["plate"] --- I getting the selected vehicle's plate from client-side gui succesfully
	local vehicle = getNearestVehicle( vehicleplate ) --- this doesn't work
    if vehicle then 
         outputChatBox("Vehicle is nearby")
    end 
end
end
addEvent("SetVehSellingDB",true)
addEventHandler("SetVehSellingDB",getRootElement(), SetVehicleSellingDB)

It's works until I getting the vehicle's plate text but how can I insert the vehicleplate variable into the getNearestVehicle function?

function getNearestVehicle( player )  
    local x, y, z = getElementPosition( player )  
    local prevDistance  
    local nearestVehicle  
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do  
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) )  
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance  
            nearestVehicle = v 
        end  
    end  
    return nearestVehicle or false  
end  

getNearestVehicle function source: 

 

Link to comment
  • Moderators
11 hours ago, thund3rbird23 said:

It's works until I getting the vehicle's plate text but how can I insert the vehicleplate variable into the getNearestVehicle function?

I am not sure why you want to pass the plate into that function. But the function requires a player, not a plate.

 

Basically  this would give you the closest vehicle from the player that executed the event:

local vehicle = getNearestVehicle( client )

 


 

But if you also want to pass the plate as well, sure that is also possible.

Adding a second argument:

local vehicle = getNearestVehicle( client, vehicleplate )

 

And adding second parameter:

function getNearestVehicle( player, vehicleplate )  

 

Note: If you do this, you will have to do something within this function with the plate, else it has no functionality.

Edited by IIYAMA
Link to comment
local vehicleplate = result[1]["plate"]
local x = result[1]["posX"]
local y = result[1]["posY"]
local z = result[1]["posZ"]

local nearby  = 20

if getDistanceBetweenPoints3D(x, y, z, getElementPosition(player)) <= nearby then
  
	outputDebugString("Vehicle is nearby")

end 

Something like this maybe?

I assumed that you store the vehicle position in the dB, I also assumed the keys for that :)

Edited by Zorgman
Link to comment
6 hours ago, IIYAMA said:

I am not sure why you want to pass the plate into that function. But the function requires a player, not a plate.

 

6 hours ago, Patrick said:

Find nearest vehicle by platetext looks unnecessary, if plates are unique.

I'm getting the vehicle plate text from a client-side gui gridlist selection and want to check if the vehicle with the selected plate text is nearby then I can do whatever what I want with the vehicle.

56 minutes ago, Zorgman said:

local vehicleplate = result[1]["plate"]
local x = result[1]["posX"]
local y = result[1]["posY"]
local z = result[1]["posZ"]

local nearby  = 20

if getDistanceBetweenPoints3D(x, y, z, getElementPosition(player)) <= nearby then
  
	outputDebugString("Vehicle is nearby")

end 

Something like this maybe?

I assumed that you store the vehicle position in the dB, I also assumed the keys for that :)

I had to modify the positions variable the positions debug is working well but I always getting "Vehicle is not nearby" doesn't matter if I'm close to the vehicle or not.

And the vehicle position doesn't change while the player is in-game. That will change after the player is disconnected so I don't think so that's the good way of the vehicle's position?!

function SetVehicleSellingDB(vehPlate)
local query = dbQuery(con, "SELECT * FROM vehicle WHERE plate=?", vehPlate)
local result = dbPoll(query, -1)
if result then
	local vehicleplate = result[1]["plate"]
	local pos = fromJSON(result[1]["pos"])
    local x = pos[1]
    local y = pos[2]
    local z = pos[3]
	iprint("x: "..x.. " y: "..y.." z: "..z)

local nearby  = 20

if getDistanceBetweenPoints3D(x, y, z, getElementPosition(source)) <= nearby then
  
	outputDebugString("Vehicle is nearby")
else
    outputDebugString("Vehicle is not nearby")
  end 
 end
end

 

  • Like 1
Link to comment
  • Moderators
44 minutes ago, thund3rbird23 said:

That will change after the player is disconnected so I don't think so that's the good way of the vehicle's position?!

You can loop through all vehicles and check if their plate is the same as the item in the database. But it is easier and more efficient to set an identifier when you create the vehicle.

What is used as your database primary key per vehicle?

Edited by IIYAMA
Link to comment
12 minutes ago, IIYAMA said:

You can loop through all vehicles and check if their plate is the same as the item in the database. But it is easier and more efficient to set an identifier when you create the vehicle.

What is used as your database primary key per vehicle?

The vehicle ID is the primary key in database. (id column)

Link to comment
  • Moderators
19 minutes ago, thund3rbird23 said:

The vehicle ID is the primary key in database. (id column)

Cool

 

When creating your vehicles you can link the ID to the vehicle in a few ways.

 

Either by table:

  • No synchronization server/client
  • Across resources if you use exports.
  • More maintenance (if a vehicle gets deleted)
local linkVehiclesToDataBase = {
	element_to_ID = {},
	ID_to_element = {}
}

function setVehicleByDataBaseID (ID, vehicle)
	linkVehiclesToDataBase.ID_to_element[ID] = vehicle
	return true
end


function getVehicleByDataBaseID (ID)
	return linkVehiclesToDataBase.ID_to_element[ID]
end

function setDataBaseIDByVehicle (vehicle, ID)
	linkVehiclesToDataBase.element_to_ID[vehicle] = ID
	return true
end

function getDataBaseIDByVehicle (vehicle)
	return linkVehiclesToDataBase.element_to_ID[vehicle]
end

---------

local ID = --...
local vehicle = createVehicle(--[[...]])


setDataBaseIDByVehicle (vehicle, ID)
-- enables: getDataBaseIDByVehicle (vehicle)


setVehicleByDataBaseID (ID, vehicle)
-- enables: getVehicleByDataBaseID (ID)

 

Or by ID:

setElementID ( vehicle, "vehicle-database-ID:" .. ID) 

https://wiki.multitheftauto.com/wiki/SetElementID

 

  • Less maintenance
  • Synchronization server/client
  • Across resources
  • Problem that can occur: Double ID's created by other resources or ID overwrite by other resources.

+

https://wiki.multitheftauto.com/wiki/GetElementID

https://wiki.multitheftauto.com/wiki/GetElementByID

 

 

 

Edited by IIYAMA
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...