Author Topic: Spawning a car with lua  (Read 8714 times)

0 Members and 2 Guests are viewing this topic.

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Spawning a car with lua
« on: September 15, 2015, 05:08:34 PM »
Alright so I made this post on facepunch a few days ago...

At the time of this post i'm on my phone so I don't feel like re-creating my post so please visit the FP link... You can reply here or there it does not matter!

Now I got how to spawn a HL2 car but how would I spawn a TDM or something else like that?? Please help!!!

Thanks!

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Spawning a car with lua
« Reply #1 on: September 16, 2015, 05:57:31 AM »
Simple example using an LWCar...


local car1 = ents.Create("prop_vehicle_jeep")
car1:SetModel("models/LoneWolfie/chev_tahoe.mdl")
car1.VehicleTable = list.Get( "Vehicles" )[ "Chevy Tahoe Police"]
car1:SetKeyValue("vehiclescript","scripts/vehicles/lwcars/chev_tahoe.txt") 
car1:SetPos(Vector(273,1875,-122))
car1:SetAngles(Angle(0,90,-0))
car1:SetHealth(defaulthp)
car1.Hurt = false
car1:Spawn()
car1:Activate()
      

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Re: Spawning a car with lua
« Reply #2 on: September 16, 2015, 11:24:41 AM »
Simple example using an LWCar...


local car1 = ents.Create("prop_vehicle_jeep")
car1:SetModel("models/LoneWolfie/chev_tahoe.mdl")
car1.VehicleTable = list.Get( "Vehicles" )[ "Chevy Tahoe Police"]
car1:SetKeyValue("vehiclescript","scripts/vehicles/lwcars/chev_tahoe.txt") 
car1:SetPos(Vector(273,1875,-122))
car1:SetAngles(Angle(0,90,-0))
car1:SetHealth(defaulthp)
car1.Hurt = false
car1:Spawn()
car1:Activate()

The key value how did you get that? Is there a way you can find that by using a chat command? Ex: i made a chat command so when you look at a car you can do !getcar and then it returns the model but is there a way I can get that key value that way? Also the vehicle table is that the cars name like in the spawn list?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Spawning a car with lua
« Reply #3 on: September 16, 2015, 12:08:46 PM »
The KeyValue for the vehicle script is located inside of the Vehicle Table.
I'm not sure why he's setting the VehicleTable on the car object, unless the gamemode he's using requires that or for future quick reference.

Type this in console.
Code: [Select]
lua_run for k, v in pairs( list.Get("Vehicles") ) do print(v.Name .. "\n" .. v.Model .. "\n" .. v.KeyValues.vehiclescript .. "\n\n") end

It will print to your console a list of all the cars installed on your server, their model, and their vehicle script. (Which is all you really need to spawn them with lua)


Example output:
Code: [Select]
Food Cistern Trailer
models/tdmcars/trailers/food_cistern.mdl
scripts/vehicles/TDMCars/trailer_con.txt


Bugatti Veyron
models/tdmcars/bug_veyron.mdl
scripts/vehicles/TDMCars/veyron.txt


Bowler EXR-S
models/tdmcars/bowler_exrs.mdl
scripts/vehicles/TDMCars/bowlexrs.txt


Shelby 1000
models/tdmcars/she_1000.mdl
scripts/vehicles/TDMCars/she1000.txt


Porsche Cayenne Turbo 12
models/tdmcars/por_cayenne12.mdl
scripts/vehicles/TDMCars/cayenne12.txt


Ferrari 250GT
models/tdmcars/ferrari250gt.mdl
scripts/vehicles/TDMCars/ferrari250gt.txt


Porsche 918 Spyder
models/tdmcars/por_918.mdl
scripts/vehicles/TDMCars/918spyd.txt


Holden HSV GTS
models/tdmcars/hsvgts.mdl
scripts/vehicles/TDMCars/hsvgts.txt


Car Seat
models/props_phx/carseat2.mdl
scripts/vehicles/prisoner_pod.txt


Single Axle dolly 2
models/tdmcars/trailers/dolly2.mdl
scripts/vehicles/TDMCars/dolly2.txt


Chair
models/nova/chair_plastic01.mdl
scripts/vehicles/prisoner_pod.txt


Subaru Legacy GT 2010
models/tdmcars/sub_legacygt10.mdl
scripts/vehicles/TDMCars/subleggt10.txt


Audi TT 07
models/tdmcars/auditt.mdl
scripts/vehicles/TDMCars/auditt.txt


Bugatti EB110
models/tdmcars/bug_eb110.mdl
scripts/vehicles/TDMCars/eb110.txt


Porsche 911 GT3-RSR
models/tdmcars/por_gt3rsr.mdl
scripts/vehicles/TDMCars/porgt3rsr.txt


Vauxhall Agila
models/tdmcars/vaux_agila.mdl
scripts/vehicles/TDMCars/vaux_agila.txt


GMC Vandura
models/tdmcars/gmcvan.mdl
scripts/vehicles/tdmcars/gmcvan.txt


Volvo XC70 Turbo
models/tdmcars/vol_xc70.mdl
scripts/vehicles/tdmcars/volxc70.txt


Porsche Cayenne Turbo S
models/tdmcars/cayenne.mdl
scripts/vehicles/TDMCars/cayenne.txt


Honda S2000
models/tdmcars/hon_s2000.mdl
scripts/vehicles/TDMCars/s2000.txt


Vauxhall Insignia VXR
models/tdmcars/vaux_insignia.mdl
scripts/vehicles/TDMCars/vaux_insignia.txt


BMW M5 E60
models/tdmcars/bmwm5e60.mdl
scripts/vehicles/TDMCars/bmwm5e60.txt


Planks Trailer
models/tdmcars/trailers/logtrailer_planks.mdl
scripts/vehicles/TDMCars/trailer_con.txt

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Re: Spawning a car with lua
« Reply #4 on: September 16, 2015, 01:31:38 PM »
this is my current code:

Code: [Select]
local car = ents.Create(class)
car:SetModel(model)
car.VehicleTable = list.Get( "Vehicles" )[ name ]
car:SetKeyValue(script) 
car:SetPos(ply:GetPos())
car:SetAngles(ply:GetAngles())
car:Spawn()
car:Activate()

this is the table that has the class, model, name, script ect:

Code: [Select]
LMMCDConfigAddCar.C2 = { "prop_vehicle_jeep", "h1tdm", "Hummer H1", 5000, "Pretty sweet hummer! A true mans car!", "models/tdmcars/hummerh1.mdl", "scripts/vehicles/TDMCars/h1.txt" }
when i do print(class.."\n"..model.."\n"..name.."\n"..script.."\n\n") it returns:

Code: [Select]
prop_vehicle_jeep
models/tdmcars/hummerh1.mdl
Hummer H1
scripts/vehicles/TDMCars/h1.txt

now the error i get is:

Code: [Select]
bad argument #2 'SetKeyValue' (string expected, got no value)

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Spawning a car with lua
« Reply #5 on: September 16, 2015, 02:45:27 PM »
Code: [Select]
car:SetKeyValue(script)
Can you show us all your code, because I don't see script defined anywhere.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Spawning a car with lua
« Reply #6 on: September 16, 2015, 04:09:46 PM »
It's

car:SetKeyValue("vehiclescript", "blah/vehiclescript.txt")

The KEY is "vehiclescript" and the value is the text file for the vehicle script.

Offline XxLMM13xX

  • Sr. Member
  • ****
  • Posts: 265
  • Karma: -51
  • New to lua development
    • Twitch
Re: Spawning a car with lua
« Reply #7 on: September 16, 2015, 05:07:10 PM »
Alright first that worked! Thanks!!!

Now in it ent.init function of the car dealer entity i have self.LMMCDID = 0 (lmmcd is lmm's car dealer) and the 0 is just for now... THEN in the function where i spawn the car dealer i have self.LMMCDID = v below is the whole function (with the config section)

Code: [Select]
hook.Add("InitPostEntity", "SpawnLMMCarDealer", function()
timer.Simple(2, function()
for k,v in pairs(LMMCDConfig.CarDealerPos) do
local lmmcd = ents.Create("car_dealer")
lmmcd:SetPos(v[2])
lmmcd:SetAngles(v[3])
lmmcd.LMMCDID = v[1]
lmmcd:Spawn()
end
end)
end)

LMMCDConfig.CarDealerPos = -- Pos for the car dealer's
{
{1, Vector(-136.871857, -1408.653809, -131.968750), Angle(2.090849, -0.501209, 0.000000), Vector(56.624390, -1315.536987, -139.968750), Angle(0.990848, -90.481255, 0.000000)},
}

the first entry in the table is the index... Now the first vector and angle in the table is where the car dealer will spawn and the second is where the car should spawn... now here's the problem... i'm pretty new to tables so how would i call the indexes...

Code: [Select]
car:SetPos(LMMCDConfig.CarDealerPos)
car:SetAngles(LMMCDConfig.CarDealerPos[self.LMMCDID][5])

that's what i have so far... NOTE: Yes i know the setpos is empty just look at the setangles...

Extra Note: the config is in a different file and YES is running

Thanks for the help!

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Spawning a car with lua
« Reply #8 on: September 18, 2015, 12:01:04 PM »
The KeyValue for the vehicle script is located inside of the Vehicle Table.
I'm not sure why he's setting the VehicleTable on the car object, unless the gamemode he's using requires that or for future quick reference.


Yeah, probably not the best example.  I do some stuff with this after-the-fact with the Proton lighting system and I needed handy reference to its vehicle table.  Not an elegant approach by me.