Getting started with Basic Vehicle Sync Resource

From GTA Network Wiki
Jump to navigation Jump to search

Getting started with Basic Vehicle Sync Resource

Getting Started

In this page you will learn everything you need to know about how to use the Basic Vehicle Sync Resource found at https://github.com/GTANetworkDev/ExampleResources under the "basicvehiclesync" folder.

Notes

This resource disables engine auto start/stop. You are required to start/stop it by script using the SetEngineState function found below.

What can I do with this resource?

This resource syncs the engine, lock, door, window, and tyre states of a vehicle to all other players. You can plug-and-play with this resource but you can also use the following C# API functions to operate these states for any vehicle:

public static void SetVehicleWindowState(Vehicle veh, WindowID window, WindowState state)
public static WindowState GetVehicleWindowState(Vehicle veh, WindowID window)
public static void SetVehicleWheelState(Vehicle veh, WheelID wheel, WheelState state)
public static WheelState GetVehicleWheelState(Vehicle veh, WheelID wheel)
public static void SetVehicleDirt(Vehicle veh, float dirt)
public static float GetVehicleDirt(Vehicle veh)
public static void SetDoorState(Vehicle veh, DoorID door, DoorState state)
public static DoorState GetDoorState(Vehicle veh, DoorID door)
public static void SetEngineState(Vehicle veh, bool status)
public static bool GetEngineState(Vehicle veh)
public static void SetLockStatus(Vehicle veh, bool status)
public static bool GetLockState(Vehicle veh)

State Definitions

For engine and lock states they are self explanatory. True = engine on/lock, False = engine off/unlocked. However for wheels, windows, and doors they have several states. I defined the states as 0, 1, and 2. However since it can get messy I created enums to use.

public enum WindowID
{
	WindowFrontRight,
	WindowFrontLeft,
	WindowRearRight,
	WindowRearLeft
}

public enum WindowState
{
	WindowFixed,
	WindowDown,
	WindowBroken
}

public enum DoorID
{
	DoorFrontLeft,
	DoorFrontRight,
	DoorRearLeft,
	DoorRearRight,
	DoorHood,
	DoorTrunk
}

public enum DoorState
{
	DoorClosed,
	DoorOpen,
	DoorBroken,
}

public enum WheelID
{
	Wheel0,
	Wheel1,
	Wheel2,
	Wheel3,
	Wheel4,
	Wheel5,
	Wheel6,
	Wheel7,
	Wheel8, //These two wheels are used for the middle trailer wheels if it's a 6-wheeler trailer.
	Wheel9  //
}

public enum WheelState
{
	WheelFixed,
	WheelBurst,
	WheelOnRim,
}

Code Examples

Example of an engine command

[Command("engine")]
public void EngineCommand(Client player)
{
    VehicleStreaming.SetEngineState(player.Vehicle, !VehicleStreaming.GetEngineState(player.Vehicle));
    player.SendChatMessage("Engine toggled.");
}

Example of a lock command

[Command("lock")]
public void LockCommand(Client player)
{
	VehicleStreaming.SetLockStatus(player.Vehicle, !VehicleStreaming.GetLockState(player.Vehicle));
}

Example of a door command

[Command("door")]
public void DoorCommand(Client player, int door)
{
    DoorState currentState = VehicleStreaming.GetDoorState(player.Vehicle, (DoorID)door);
    if (currentState == DoorState.DoorClosed)
        VehicleStreaming.SetDoorState(player.Vehicle, (DoorID)door, DoorState.DoorOpen);
    else if (currentState == DoorState.DoorOpen)
        VehicleStreaming.SetDoorState(player.Vehicle, (DoorID)door, DoorState.DoorClosed);
    else
        player.SendChatMessage("~y~The door is broken!");
    player.SendChatMessage("Door toggled.");
}

Example of a door command to toggle all doors

[Command("doorall")]
public void DoorAllCommand(Client player)
{
    for(int i = 0; i < 8; i++)
    {
        DoorState currentState = VehicleStreaming.GetDoorState(veh, (DoorID)i);
        if (currentState == DoorState.DoorClosed)
            VehicleSync.VehicleStreaming.SetDoorState(veh, (DoorID)i, DoorState.DoorOpen);
        else if (currentState == DoorState.DoorOpen)
            VehicleSync.VehicleStreaming.SetDoorState(veh, (DoorID)i, DoorState.DoorClosed);
    }
    player.SendChatMessage("All doors toggled.");
}

Example of a window command to toggle windows

[Command("window")]
public void WindowCommand(Client player, int window)
{
    WindowState windowState = VehicleStreaming.GetVehicleWindowState(veh, (WindowID)window);
    if (windowState == WindowState.WindowDown)
        VehicleStreaming.SetVehicleWindowState(veh, (WindowID)window, WindowState.WindowFixed);
    else if (windowState == WindowState.WindowFixed)
        VehicleStreaming.SetVehicleWindowState(veh, (WindowID)window, WindowState.WindowDown);
    else
        player.SendChatMessage("~y~The window is broken!");
    player.SendChatMessage("Window toggled.");
}