SetVehicleColor: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
 
(24 intermediate revisions by 17 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__
This function will set the color of a vehicle. Each vehicle can have up to 4 colors, for different aspects of the vehicle. Most vehicles only use two of the colors.
{{Server client function}}
This function will set the color of a vehicle using either a RGB format, or the [[Vehicle Colors|standard San Andreas color IDs.]] Vehicles can have up to 3 colors, most of the vehicles have 2 colors only.  


{{New feature|3.0110|1.1|
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setVehicleColor ( vehicle theVehicle, int color1, int color2, int color3, int color4 )             
bool setVehicleColor(vehicle veh, int r1, int g1, int b1, [int r2, int g2, int b2, int r3, int g3, int b3, int r4, int g4, int b4])           
</syntaxhighlight>
 
<syntaxhighlight lang="lua">
bool setVehicleColor(vehicle veh, int p1, int p2, int p3, int p4)             
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[vehicle]]:setColor}}


===Required Arguments===  
===Required Arguments===  
*'''theVehicle:''' The vehicle that you wish to set the color of.
*'''theVehicle:''' The [[vehicle]] that you wish to set the color of.
 
* RGB format:
===Optional Arguments===
** '''r1, g1, b1:''' Three integers indicating the red, green and blue components of the first (main) color for the vehicle
{{OptionalArg}}
* Palette format:
*'''color1:''' An integer indicating the first (main) color for the vehicle
** '''p1, p2, p3, p4:''' Exactly four integers indicating the [[Vehicle Colors|vehicle color IDs]] from the palette. <br> If a vehicle has fewer than four colours, you must still provide four colours, otherwise the RGB format will be used.
*'''color2:''' An integer indicating the second color for the vehicle
===Optional Arguments (RGB format only)===
*'''color3:''' An integer indicating the third color for the vehicle
*'''r2, g2, b2:''' Three integers indicating the red, green and blue components of the second color for the vehicle
*'''color4:''' An integer indicating the fourth color for the vehicle
*'''r3, g3, b3:''' Three integers indicating the red, green and blue components of the third color for the vehicle
 
*'''r4, g4, b4:''' Three integers indicating the red, green and blue components of the fourth color for the vehicle
The table below shows valid color ids:
}}
{{Vehicle_colors}}
 
===Returns===
===Returns===
Returns ''true'' if vehicle's color was set, ''false'' if an invalid vehicle or invalid color ids were specified.
Returns ''true'' if vehicle's color was set, ''false'' if an invalid vehicle or invalid colors were specified.


==Example==  
==Examples==
This example implements a ''set_vehicle_color'' function.
<section name="Example 1 - RGB format" class="server" show="true">
This example implements a serverside ''random_color'' console command.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Register the command handler and attach it to the 'consoleSetVehicleColor' function
addCommandHandler( 'random_color',
addCommandHandler ( "set_vehicle_color", "consoleSetVehicleColor" )
function( uPlayer )
-- Define our function that will handle this command
if isPedInVehicle( uPlayer ) then
function consoleSetVehicleColor ( playerSource, commandName, col1, col2, col3, col4 )
local uVehicle = getPedOccupiedVehicle( uPlayer )
-- If a player triggered this in-game
if uVehicle then
if ( playerSource ) then
local r, g, b = math.random( 255 ), math.random( 255 ), math.random( 255 )
-- Get the player's vehicle
setVehicleColor( uVehicle, r, g, b )
playerVehicle = getPlayerOccupiedVehicle ( playerSource )
end
-- If the player is in a vehicle and we've got at least 1 parameter
end
if ( playerVehicle and col1 ) then
end
-- Get the vehicle's existing color and use it if fewer than 4 arguments were passed
)
exCol1, exCol2, exCol3, exCol4 = getVehicleColor ( playerVehicle )
</syntaxhighlight>
</section>


if not col2 then col2 = exCol2 end
if not col3 then col3 = exCol3 end
if not col4 then col4 = exCol4 end


-- Set the vehicle's color
<section name="Example 2 - palette format" class="server" show="true">
setVehicleColor ( playerVehicle, col1, col2, col3, col4 )
This example implements a serverside command for the player to spawn a red and white Banshee, using the palette colors.
else
<syntaxhighlight lang="lua">
-- If we didn't get at least 1 parameter or the player doesn't have a vehicle, display some help text
addCommandHandler("banshee", function(player)
outputConsole ( "This function will set your current vehicle's colors. A vehicle can have up to 4 colors.", playerSource )
    local x, y, z = getElementPosition(player)
outputConsole ( "Syntax: set_vehicle_color color1 [ color2 color3 color4 ]", playerSource )
    local rx, ry, rz = getElementRotation(player)
outputConsole ( "You must be in a vehicle to use this function.", playerSource )
    local vehicle = createVehicle(429, x, y, z, 0.0, 0.0, rz, "PALETTE")
end
    setVehicleColor(vehicle, 3, 1, 0, 0)-- A red and white Banshee!
end
    addVehicleUpgrade(vehicle, 1010)
end
    warpPedIntoVehicle(player, vehicle, 0)
    outputChatBox("You have spawned a red and white Banshee! I also added NOS for you!", source, 50, 168, 82)
end)
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Vehicle_functions}}
{{Vehicle_functions}}

Latest revision as of 17:22, 4 October 2022

This function will set the color of a vehicle using either a RGB format, or the standard San Andreas color IDs. Vehicles can have up to 3 colors, most of the vehicles have 2 colors only.

Syntax

bool setVehicleColor(vehicle veh, int r1, int g1, int b1, [int r2, int g2, int b2, int r3, int g3, int b3, int r4, int g4, int b4])            
bool setVehicleColor(vehicle veh, int p1, int p2, int p3, int p4)            


OOP Syntax Help! I don't understand this!

Method: vehicle:setColor(...)


Required Arguments

  • theVehicle: The vehicle that you wish to set the color of.
  • RGB format:
    • r1, g1, b1: Three integers indicating the red, green and blue components of the first (main) color for the vehicle
  • Palette format:
    • p1, p2, p3, p4: Exactly four integers indicating the vehicle color IDs from the palette.
      If a vehicle has fewer than four colours, you must still provide four colours, otherwise the RGB format will be used.

Optional Arguments (RGB format only)

  • r2, g2, b2: Three integers indicating the red, green and blue components of the second color for the vehicle
  • r3, g3, b3: Three integers indicating the red, green and blue components of the third color for the vehicle
  • r4, g4, b4: Three integers indicating the red, green and blue components of the fourth color for the vehicle

Returns

Returns true if vehicle's color was set, false if an invalid vehicle or invalid colors were specified.

Examples

Click to collapse [-]
Example 1 - RGB format

This example implements a serverside random_color console command.

addCommandHandler( 'random_color',
	function( uPlayer )
		if isPedInVehicle( uPlayer ) then
			local uVehicle = getPedOccupiedVehicle( uPlayer )
			if uVehicle then
				local r, g, b = math.random( 255 ), math.random( 255 ), math.random( 255 )
				setVehicleColor( uVehicle, r, g, b )
			end
		end
	end
)


Click to collapse [-]
Example 2 - palette format

This example implements a serverside command for the player to spawn a red and white Banshee, using the palette colors.

addCommandHandler("banshee", function(player)
    local x, y, z = getElementPosition(player)
    local rx, ry, rz = getElementRotation(player)
    local vehicle = createVehicle(429, x, y, z, 0.0, 0.0, rz, "PALETTE")
    setVehicleColor(vehicle, 3, 1, 0, 0)-- A red and white Banshee!
    addVehicleUpgrade(vehicle, 1010)
    warpPedIntoVehicle(player, vehicle, 0)
    outputChatBox("You have spawned a red and white Banshee! I also added NOS for you!", source, 50, 168, 82)
end)

See Also