Skip to content

Instantly share code, notes, and snippets.

@amartinz
Last active November 26, 2023 10:21
Show Gist options
  • Save amartinz/3b68c9538b39efd177aca63727e8078f to your computer and use it in GitHub Desktop.
Save amartinz/3b68c9538b39efd177aca63727e8078f to your computer and use it in GitHub Desktop.
Unreal Engine - Toggle "Lag" Action
/*
* Call to toggle lag settings, eg via binding input actions.
* Useful for testing client side prediction, server reconciliation, server side rewind, ...
*
* 1) If lag and loss settings are set, reset
* 2) If lag setting is set, set loss setting
* 3) Else set lag setting
*/
void ToggleLag()
{
if (GetWorld() == nullptr || GetWorld()->GetNetDriver() == nullptr) return;
auto PacketSimulationSettings = GetWorld()->GetNetDriver()->PacketSimulationSettings;
if (PacketSimulationSettings.PktLag != 0 && PacketSimulationSettings.PktLoss != 0)
{
UE_LOGFMT(LogTemp, Warning, "|Network| Resetting packet simulation settings");
PacketSimulationSettings.ResetSettings();
}
else if (PacketSimulationSettings.PktLag != 0)
{
UE_LOGFMT(LogTemp, Warning, "|Network| Applying packet loss");
PacketSimulationSettings.PktLoss = 10;
}
else
{
UE_LOGFMT(LogTemp, Warning, "|Network| Applying packet lag");
PacketSimulationSettings.PktLag = 500;
}
PacketSimulationSettings.ValidateSettings();
GetWorld()->GetNetDriver()->SetPacketSimulationSettings(PacketSimulationSettings);
UE_LOGFMT(LogTemp, Warning, "|Network| Lag({0}), Loss({1})",
GetWorld()->GetNetDriver()->PacketSimulationSettings.PktLag,
GetWorld()->GetNetDriver()->PacketSimulationSettings.PktLoss);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment