Jump to content

Automatically creating trailer


kerts93

Recommended Posts

Hey again,

I've got one small question.

Is it possible, that server automatically creates trailer 591 and attaches it to vehicle 514 if it's created (by map file, by script or by command (that spawns a vehicle). Basically spawns+attaches the trailer every time a specific id vehicle is created?

Link to comment

hmm, by map - not possible with pure event, but possible with something like:

  
-- server: 
function getPositionRelatedToVehicle(theVeh, x, y) 
    local xx, yy, zz = getElementPosition(theVeh) 
    local rx, ry, rot = getElementRotation(theVeh) 
    local lx = xx + math.sin (math.rad(-rot)) * x 
    local ly = yy + math.cos (math.rad(-rot)) * y 
    return lx,ly 
end 
  
addEvent("onVehicleSpawn", false) 
addEventHandler("onVehicleSpawn", getRootElement() function() 
  -- source = spawned vehicle 
  if (getElementModel(source)== 514) then 
    local x,y,z = getElementPosition(source) 
    local rx, ry, rz = getElementRotation(source) 
    local lx, ly = getPositionRelatedToVehicle(source, -3, -3) 
    local trailer = createVehicle(591, lx, ly, z) 
    setElementRotation(trailer, 0, 0, rz) 
    attachTrailerToVehicle(trailer, source) 
  end 
end) 
  
setTimer(function() 
  local vehs = getElementsByType('vehicle') 
  for key,val in ipairs(vehs) do 
    local ed = getElementData(val, "spawnedEventTriggered") 
    if (ed ~= true) then 
      setElementData(val, "spawnedEventTriggered") 
      triggerEvent("onVehicleSpawn", val) 
    end 
  end 
end, 500, 0) 
  

not tested. there is no other way afaik but remember - this can be cpu consuming when there is a lot of cars around the map (but i think it will be fine anyway)

Link to comment

thanks, I'll try it out

This section:

addEventHandler("onVehicleSpawn", getRootElement() function() 
  -- source = spawned vehicle 
  if (getElementModel(source)== 514) then 
    local x,y,z = getElementPosition(source) 
    local rx, ry, rz = getElementRotation(source) 
    local lx, ly = getPositionRelatedToVehicle(source, -3, -3) 
    local trailer = createVehicle(591, lx, ly, z) 
    setElementRotation(trailer, 0, 0, rz) 
    attachTrailerToVehicle(trailer, source) 
  end 
end 

Gives me this error:

')' expected near 'function' line 33 which currently is line 1 here

' expected near 'end' line 43 which is last line here

Link to comment
addEventHandler("onVehicleSpawn", getRootElement(), 
function() 
  -- source = spawned vehicle 
  if (getElementModel(source)== 514) then 
    local x,y,z = getElementPosition(source) 
    local rx, ry, rz = getElementRotation(source) 
    local lx, ly = getPositionRelatedToVehicle(source, -3, -3) 
    local trailer = createVehicle(591, lx, ly, z) 
    setElementRotation(trailer, 0, 0, rz) 
    attachTrailerToVehicle(trailer, source) 
  end 
end) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...