Trying to remove lag when C++ code runs

Hello
I have created an actor that activates a trigger when Multiple Booleans are set to true, but I’ve noticed when it activates, the programme is hit by extreme lag spikes. The code describes is the if statement commented below:
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (Scrnsht) {
TriggerOccur();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (P_Pressed_MyActor && DoOnce) {
DAQmxClearTask(taskHandle1);
UE_LOG(LogTemp, Log, TEXT(“Hello”));
(DAQmxCreateTask(“”, &taskHandle1));
(DAQmxCreateDOChan(taskHandle1, “Dev1/port0/line0:7”, “”, DAQmx_Val_ChanForAllLines));
(DAQmxStartTask(taskHandle1));
DoOnce = false;
}
/*if (!DoOnce) {
if (X_Correct && Y_Correct && Z_Correct && X_AngleCorrect && Y_AngleCorrect && Z_AngleCorrect && Delay) {
Delay = false;
(DAQmxWriteDigitalLines(taskHandle1, 1, 1, 10.0, DAQmx_Val_GroupByChannel, dataD, NULL, NULL));
(DAQmxWriteDigitalLines(taskHandle1, 1, 1, 10.0, DAQmx_Val_GroupByChannel, dataU, NULL, NULL));
(DAQmxWriteDigitalLines(taskHandle1, 1, 1, 10.0, DAQmx_Val_GroupByChannel, dataD, NULL, NULL));
std::this_thread::sleep_for(std::chrono::milliseconds(Period_milliseconds));
Delay = true;
}

}*/

}
I believe the way I have coded it is the issue but am unsure what I did wrong

You can’t be using sleep on the GameThread, that locks basically everything. In fact, you shouldn’t use sleep at all in unreal code, ever. If you want to do something on a timer, use Gameplay Timers. Also if you can avoid Tick and use Event Dispatchers (Delegates in C++) to notify when the boolean values change, that would be a better implementation

Also, it’s really discouraged using std in Unreal. Unreal has an equivalent for most of the things you might need from std, especially for gameplay code, so you should try and use that instead

1 Like