GetVehicleNameFromID: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Visual improvement)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Incomplete]]
{{Server client function}}
__NOTOC__
{{Deprecated|getVehicleNameFromModel}}


__NOTOC__
Gets the name of a vehicle by its model id.
Allows you to input a vehicleID and return a vehicle name from that ID


==Syntax==  
==Syntax==  
Line 13: Line 14:


===Returns===
===Returns===
Returns ''true'' if vehicle exists, ''false'' otherwise.
Returns the name of the vehicle if the id was valid, ''false'' otherwise.


==Example==  
==Example==  
This example allows you to use the chatbot to type a command. The command is used to check the vehiclename of the vehicleid the user entered.
This will retrieve the name of a car so its name can be displayed when the player chooses to spawn a car by ID.
 
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onConsole", root, "onConsole" )
function createvehiclecommand ( sourcePlayer, commandName, carid )
function onConsole ( message )  
-- This function is triggered by the text "spawnvehicle" in the console.
local command = gettok(message, 1, 32) --A varible called command stores a message typed in the chatbox by a player
-- The player must specify the parameter 'carid' to specify
if (command == "!namevehicle #") then --If the typed chat is equal to this, the code commences
-- what car they wish to spawn.
outputChatBox ( "The name of # is".. vehiclename, source ) --the chatbox outputs the vehiclename of the vehicleid the player entered
local x, y, z = getElementPosition ( sourcePlayer )
end
-- Get the position of the player to spawn the car near this location
local carname = getVehicleNameFromID ( tonumber(carid) )
-- Get the name of the car the player asked for and store it in the
-- variable 'carname'
if carname == false then
outputChatBox ( "That is not a valid car ID", sourcePlayer )
else
createVehicle ( carid, x + 5, y, z )
-- Spawn the car at x + 5 from the player so it doesn't crush him
outputChatBox ( "A " .. carname .. " was created!" )
end
-- If the entered car ID is invalid, false will be returned.
-- Otherwise a string is returned, we create the car and announce the car name in the chatbox.
end
end
addCommandHandler ( "spawnvehicle", createvehiclecommand )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Vehicle functions}}
{{Vehicle functions}}

Latest revision as of 11:31, 26 June 2014


Emblem-important.png This function is deprecated. This means that its use is discouraged and that it might not exist in future versions.

Please use getVehicleNameFromModel instead.


Gets the name of a vehicle by its model id.

Syntax

string getVehicleNameFromID ( int id )            

Required Arguments

  • id: This is a vehicle id. See vehicle to see what values will return names (true).

Returns

Returns the name of the vehicle if the id was valid, false otherwise.

Example

This will retrieve the name of a car so its name can be displayed when the player chooses to spawn a car by ID.

function createvehiclecommand ( sourcePlayer, commandName, carid )
	-- This function is triggered by the text "spawnvehicle" in the console.
	-- The player must specify the parameter 'carid' to specify
	-- what car they wish to spawn.
	local x, y, z = getElementPosition ( sourcePlayer )
	-- Get the position of the player to spawn the car near this location
	local carname = getVehicleNameFromID ( tonumber(carid) )
	-- Get the name of the car the player asked for and store it in the
	-- variable 'carname'
	if carname == false then
		outputChatBox ( "That is not a valid car ID", sourcePlayer )
	else
		createVehicle ( carid, x + 5, y, z )
		-- Spawn the car at x + 5 from the player so it doesn't crush him
		outputChatBox ( "A " .. carname .. " was created!" )
	end
	-- If the entered car ID is invalid, false will be returned.
	-- Otherwise a string is returned, we create the car and announce the car name in the chatbox.
end
addCommandHandler ( "spawnvehicle", createvehiclecommand )

See Also