CreateVehicle

From GT-MP
Jump to: navigation, search
CreateVehicle
Shared Function
Utilities-terminal.png Server-Side System-users.png Client-Side
Csharp.png C# Javascript.png JavaScript

Creates a vehicle.

Dialog-warning.png

Note: If shared API was used when creating, then the created entities are not deleted on ResourceStop


Syntax

In C#
Server-Side Function

Icon-info.png
This method is asynchronous. There is no guarantee the entity will be known to the client when working with it after creation (e.g. setting properties). Use waitForSynchronization() to make sure it's known.

waitForSynchronization() does not guarantee that the entity is streamed in on the client, it just means that the entity is known (and can be updated serverside). If you need to work with it on clientside you will need to check if the entity is streamed in (e.g. by hooking to the onEntityStreamIn event). Entities that are not streamed in can not be accessed using the clientside API. (Version 0.1.7)

Vehicle API.createVehicle(VehicleHash model, Vector3 pos, Vector3 rot, int color1, int color2, int dimension = 0);
  • model: parameter input should be in VehicleHash type.
  • pos: parameter input should be in Vector3 type. This will decide where the vehicle spawns.
  • rot: parameter input should be in Vector3 type. This decides the vehicle's angle upon spawning
  • color1: parameter input should be in int type. See Vehicle Colors
  • color2: parameter input should be in int type. See Vehicle Colors
  • dimension: parameter input should be in int type, optional, default value: 0

In Javascript
Client-Side Function

LocalHandle API.createVehicle(int model, Vector3 pos, Vector3 heading);
  • model: parameter input should be the actual hash integer.
  • pos: parameter input should be in Vector3 type. This will decide where the vehicle spawns.
  • heading: parameter input should be in Vector3 type. This will decide what direction the vehicle points.
Icon-info.png

Vehicle position can be changed without recreating the vehicle again with setEntityPosition


Icon-info.png

An issue has been detected with rapidly spawning vehicles, and vehicles ending up at position 0, 0, 0. A temporary workaround is to sleep for a few milliseconds inbetween the spawns.


Usage examples

  • Example 1

In C#
Server-Side Function

VehicleHash myVehicle = API.vehicleNameToModel("adder");
API.createVehicle(myVehicle, vehPos, vehRot, 0, 0); //Spawned vehicle is visible in all Dimensions
  • Example 2

In C#
Server-Side Function

API.createVehicle(VehicleHash.Adder, vehPos, vehRot, 0, 0); //Spawned vehicle is visible in all Dimensions
  • Example 3

In C#
Server-Side Function

VehicleHash myVehicle = VehicleHash.Adder;
NetHandle myVeh1 = API.createVehicle(myVehicle, vehPos, vehRot, 0, 0); //Spawned vehicle is visible in all Dimensions
NetHandle myVeh2 = API.createVehicle(myVehicle, vehPos2, vehRot2, 0, 0, 5); //Spawned vehicle is visible only in Dimension 5


References