• How can I spawn vehicles with a colour assigned to them?
    14 replies, posted
Hi there everyone, very simple question with hopefully a simple answer (I'm still pretty much a lua novice despite many years making vehicles for Gmod). How do you spawn a vehicle with a custom colour assigned to it in the spawn script? I've looked through the documentation and found that colours are defined in Lua with the line: [code]Color( 1, 2, 3, 4 ) [/code] but how do I put that into a vehicle spawn script so it will be assigned to the vehicle when the player spawns it from the menu? I've tried [code]local V = { -- Required information Name = "Beta Grand Am V0.8.1",  Class = "prop_vehicle_jeep", Category = Category, -- Optional information Author = "Master Chris", Information = "Bae has a Grand Ass", Model = "models/vehicles/pontiac_grandam/pontiac_20l2.mdl", Color = (Color( 0, 255, 0 )),  KeyValues = { vehiclescript = "scripts/vehicles/GrandAm-v037.txt" } } list.Set( "Vehicles", "Beta_GrandAm", V )[/code] but that had no effect, no error either. I've also tried putting the Color( 0, 255, 0 ) bit into the KeyValues section but again no luck. Frankly I have no idea if I'm close or way off here so any help would be appreciated.
ent:SetColor( Color( 0, 255, 0, 230 ) ) ent:SetRenderMode( RENDERMODE_TRANSALPHA ) Example found here: Entity/SetColor
Ok, but those lines don't look as if they will just slip into the vehicle spawn code though. local V = { -- Required information Name = "Beta Grand Am V0.8.1",  Class = "prop_vehicle_jeep", Category = Category, -- Optional information Author = "Master Chris", Information = "Bae has a Grand Ass", Model = "models/vehicles/pontiac_grandam/pontiac_20l2.mdl", ent:SetColor( Color( 0, 255, 0, 230 ) ) ent:SetRenderMode( RENDERMODE_TRANSALPHA ) KeyValues = { vehiclescript = "scripts/vehicles/GrandAm-v037.txt" } } list.Set( "Vehicles", "Beta_GrandAm", V ) I tested this but it just spat out an error. I'm afraid I'm going to need more context on how I need to actually use the commands. They look like something you would call after the entity was created.
According to this list.Set can't accept colors. So you nee to create hook PlayerSpawnedVehicle (Sandbox and SBox-Derived gamemodes only) or OnEntityCreated (for all) If entity name is Beta Grand Am V0.8.1 then change it color
Ah ok I think I get where this is going. So the code I need to add would look something like this: hook.Add( "PlayerSpawnedVehicle", "MC_GrandAm.ColourChange", function( ent ) if PlayerSpawnedVehicle = "Beta Grand Am V0.8.1" then ent:SetColor( Color( 0, 255, 0, 255)) ent:SetRenderMode( RENDERMODE_TRANSALPHA) end)
I think that one should work but with the other ones it would have to be "V:SetColor" because "local V" is saying... local V = { --is equal to all the stuff in these brackets -- Required information Name = "Beta Grand Am V0.8.1", Class = "prop_vehicle_jeep", Category = Category, -- Optional information Author = "Master Chris", Information = "Bae has a Grand Ass", Model = "models/vehicles/pontiac_grandam/pontiac_20l2.mdl", ent:SetColor( Color( 0, 255, 0, 230 ) ) ent:SetRenderMode( RENDERMODE_TRANSALPHA ) KeyValues = { vehiclescript = "scripts/vehicles/GrandAm-v037.txt" } } list.Set( "Vehicles", "Beta_GrandAm", V ) but as Spar said it might not work but that is the correct function to use because you're trying to apply the value Color to an Entity
I'm afraid that code gives me the error "attempt to index global 'V' (a nil value)" local V = { -- Required information Name = "Beta Grand Am V0.8.1",  Class = "prop_vehicle_jeep", Category = Category, -- Optional information Author = "Master Chris", Information = "Bae has a Grand Ass", Model = "models/vehicles/pontiac_grandam/pontiac_20l2.mdl", V:SetColor( Color( 0, 255, 0, 230 ) ), V:SetRenderMode( RENDERMODE_TRANSALPHA ), KeyValues = { vehiclescript = "scripts/vehicles/GrandAm-v037.txt" } } list.Set( "Vehicles", "Beta_GrandAm", V ) I think I'll go ahead with trying to write a working version of the hook I started in the last post but I'll do that later today, right now I need some rest as it's been a long day.
No it shouln't. V is a table, not a entity. It is used in SpawnMenu List Almost. For understanding you can read this: Hook Library Usage PlayerSpawnedVehicle just an ID of hook, it's not an argument. Read you code and see where function argument is. You need IsVehicle and ent.VehicleName == *name*
I wasn't talking about V I was talking about ':SetColor'
Here is how you can do it: hook.Add("PlayerSpawnedVehicle", "MC_Vehicles.SpawnColorChange", function(ply, vehicle) local realClass = vehicle:GetVehicleClass() if !list.Get("Vehicles")[realClass] then return end local vehicleTable = list.Get("Vehicles")[realClass] local color = vehicleTable.SpawnColor if color then vehicle:SetColor(string.ToColor(color)) vehicle:SetRenderMode(RENDERMODE_TRANSALPHA) end end) and your vehicle code would look like this: local V = { -- Required information Name = "Beta Grand Am V0.8.1", Class = "prop_vehicle_jeep", Category = Category, -- Optional information Author = "Master Chris", Information = "Bae has a Grand Ass", Model = "models/vehicles/pontiac_grandam/pontiac_20l2.mdl", SpawnColor = "0 255 0 255", -- use string, not Color KeyValues = { vehiclescript = "scripts/vehicles/GrandAm-v037.txt" } } list.Set( "Vehicles", "Beta_GrandAm", V ) You just have to set the color as a string instead of the Color structure.
Thanks guys, I'll take a closer look at this when I get back to the computer later.
Thanks Gmod4phun, that code worked perfectly. Though I have to admit I'm a little surprised it took something that complicated just to change the colour of a vehicle :S Anyway, the reason I wanted to add this code is because I recently found out about a method of making colourable metallic paint but for the sake of the end user I wanted something other than white to be the starting colour you get when you spawn the car. Would it be possible to set up the code so that a random colour was chosen every time you spawned the car? Kind of like this: http://steamcommunity.com/sharedfiles/filedetails/?id=1313913818 http://steamcommunity.com/sharedfiles/filedetails/?id=1313913870
Sure. You can either use a totally random color, or pick a random color from a table of colors to make it not as much random. Stuff is explained, so you can modify it to fit your style local SpawnColorTable = { -- add more colors here Color(255,0,0), Color(0,255,0), Color(0,0,255), Color(255,255,0), Color(255,0,255), Color(0,255,255), Color(0,100,180), Color(0,180,220), } hook.Add("PlayerSpawnedVehicle", "MC_Vehicles.SpawnColorChange", function(ply, vehicle) if !IsValid(vehicle) then return end -- just check if stuff exists if !vehicle.GetVehicleClass then return end -- some vehicles dont have this function (I don't know why) despite being vehicles local realClass = vehicle:GetVehicleClass() -- get our real class local vehicleTable = list.Get("Vehicles")[realClass] -- get the parameters table if !vehicleTable then return end -- if we failed, it probably does not exist, dont continue local spawncolor = vehicleTable.SpawnColor -- gets what is in the SpawnColor, either a color or RANDOM or RANDOMTABLE if spawncolor == nil then return end -- if it returns nil (does not exist), dont continue local clr = string.ToColor(spawncolor) -- tries to convert to a Color, defaults to 255 255 255 255 when it fails to properly convert if spawncolor == "RANDOM" then -- if SpawnColor was set to "RANDOM", pick a fully random color clr = Color(math.random(0,255), math.random(0,255), math.random(0,255)) elseif spawncolor == "RANDOMTABLE" then -- if SpawnColor was set to "RANDOMTABLE", pick a random color from the table of predefined colors clr = SpawnColorTable[math.random(1, #SpawnColorTable)] -- picks Color from table on the index of i, where i is a random number between 1 and the length (#) of the table end vehicle:SetColor(clr) -- color that car! vehicle:SetRenderMode(RENDERMODE_TRANSALPHA) -- makes alpha work end)
That is fantastic, thanks very much! On a side note I was going to say that I just found out about a conflict with the old code you wrote and simfphys vehicles but the new code removes that problem, so no need to worry.
Yeah I found out, too. They seem not to have the GetVehicleClass function, so it called a nonexisting function. It should not throw out any errors anymore
Sorry, you need to Log In to post a reply to this thread.