SetVehicleColor: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Corrected wrong OOP syntax)
No edit summary
Line 37: Line 37:
)
)
</syntaxhighlight>  
</syntaxhighlight>  
</section>
{{Warning|Informations below apply to MTA:SA v1.0.5 and older!|true}}
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.
==Syntax==
<syntaxhighlight lang="lua">
bool setVehicleColor ( vehicle theVehicle, int color1, int color2, int color3, int color4 )           
</syntaxhighlight>
===Required Arguments===
*'''theVehicle:''' The [[vehicle]] that you wish to set the color of.
*'''color1:''' An integer indicating the first (main) color for the vehicle
*'''color2:''' An integer indicating the second color for the vehicle
*'''color3:''' An integer indicating the third color for the vehicle
*'''color4:''' An integer indicating the fourth color for the vehicle
The table below shows valid color ids:
{{Vehicle_colors}}
<br>
<section name="Example 1" class="server" show="false">
This example implements a serverside ''set_vehicle_color'' console command.
<syntaxhighlight lang="lua">
-- Define our function that will handle the command
function consoleSetVehicleColor ( playerSource, commandName, col1, col2, col3, col4 )
-- If a player triggered this in-game
if ( playerSource ) then
-- Get the player's vehicle
playerVehicle = getPlayerOccupiedVehicle ( playerSource )
-- If the player is in a vehicle and we've got at least 1 parameter
if ( playerVehicle and col1 ) then
-- Get the vehicle's existing color and use it if fewer than 4 arguments were passed
exCol1, exCol2, exCol3, exCol4 = getVehicleColor ( playerVehicle )
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
setVehicleColor ( playerVehicle, col1, col2, col3, col4 )
else
-- If we didn't get at least 1 parameter or the player doesn't have a vehicle, display some help text
outputConsole ( "This function will set your current vehicle's colors. A vehicle can have up to 4 colors.", playerSource )
outputConsole ( "Syntax: set_vehicle_color color1 [ color2 color3 color4 ]", playerSource )
outputConsole ( "You must be in a vehicle to use this function.", playerSource )
end
end
end
-- Register the command handler and attach it to the consoleSetVehicleColor function
addCommandHandler ( "set_vehicle_color", consoleSetVehicleColor )
</syntaxhighlight>
</section>
<section name="Example 2" class="server" show="false">
This example changes all vehicles color to random every 0.5 sec.
<syntaxhighlight lang="lua">
function randomVehColors()
for i, car in ipairs( getElementsByType( "vehicle" ) ) do
local color = {}
color[1] = math.random(0,126) -- random from 0 to 126, because colors is from 0 to 126
color[2] = math.random(0,126)
color[3] = math.random(0,126)
color[4] = math.random(0,126) -- we take 4 random numbers because setVehicleColor have parameters with 4 colors
setVehicleColor ( car, color[1], color[2], color[3], color[4] ) -- setting color to vehicle
end
end
setTimer( randomVehColors, 500, 0 ) -- timer changes all vehicles colors to random every 0.5 sec.
</syntaxhighlight>
</section>
</section>



Revision as of 23:28, 19 September 2015

This function will set the color of a vehicle. Colors are in RGB format, vehicles can have up to 4 colors. Most vehicles have 2 colors only.

Syntax

bool setVehicleColor ( vehicle theVehicle, int r1, int g1, int b1, [int r2, int g2, int b2, int r3, int g3, int b3, int r4, int g4, int b4] )            


OOP Syntax Help! I don't understand this!

Method: vehicle:setColor(...)


Required Arguments

  • theVehicle: The vehicle that you wish to set the color of.
  • r1, g1, b1: Three integers indicating the red, green and blue components of the first (main) color for the vehicle

Optional Arguments

  • 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.

Example

Click to collapse [-]
Example 1

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
)


See Also