Skip to content

Instantly share code, notes, and snippets.

@d0p3t
Last active October 5, 2018 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0p3t/eb500ffec72cc6c5a5ebb259376b1d7b to your computer and use it in GitHub Desktop.
Save d0p3t/eb500ffec72cc6c5a5ebb259376b1d7b to your computer and use it in GitHub Desktop.
Snippet of PLAYER_SWITCH translated to C# from https://gist.github.com/speedium666/a8b81fd6d9039fb8a191d8d0bb8c7388
public static async Task StartSwitch(Vector4 fromWh, Vector4 toWh, int model)
{
try
{
var from = new Vector3(fromWh.X, fromWh.Y, fromWh.Z);
var to = new Vector3(toWh.X, toWh.Y, toWh.Z);
var playerPed = Game.PlayerPed;
var switchModel = new Model(model);
var switchType = GetIdealPlayerSwitchType(from.X, from.Y, from.Z, to.X, to.Y, to.Z);
var switchFlag = 1024;
if (switchType == 3)
{
switchType = 2;
if (to.DistanceToSquared2D(from) < 40f)
{
return;
}
}
while (!HasModelLoaded((uint)switchModel.Hash))
{
SetEntityLocallyInvisible(playerPed.Handle);
RequestModel((uint)switchModel.Hash);
await BaseScript.Delay(0);
}
var switchToPed = await World.CreatePed(switchModel, to, 0);
switchToPed.IsVisible = false;
switchToPed.IsInvincible = true;
SetEntityAsMissionEntity(switchToPed.Handle, true, false);
switchToPed.Task.ClearAllImmediately();
if (!playerPed.IsInjured)
{
switchToPed.Heading = toWh.W;
}
switchToPed.IsCollisionEnabled = false;
switchToPed.IsVisible = false;
SetModelAsNoLongerNeeded((uint)switchModel.Hash);
StartPlayerSwitch(playerPed.Handle, switchToPed.Handle, switchFlag, switchType);
while (GetPlayerSwitchState() != 8)
{
SetEntityLocallyInvisible(playerPed.Handle);
await BaseScript.Delay(0);
}
SetFocusEntity(switchToPed.Handle);
playerPed.IsPositionFrozen = false;
playerPed.IsCollisionEnabled = true;
playerPed.IsVisible = true;
playerPed.IsInvincible = false;
playerPed.Position = to;
playerPed.Heading = toWh.W;
var handle = switchToPed.Handle;
DeletePed(ref handle);
DeleteEntity(ref handle);
World.DestroyAllCameras();
}
catch (Exception e)
{
Debug.WriteLine($"{e.Message} : Exception happened on SpawnManagement.StartSwitch()");
}
await Task.FromResult(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment