I’m trying to run a console command in c++ from one of my actors. I’m using this code :
bool ran = GetWorld()->Exec(GetWorld(), TEXT("stat fps"));
GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::Yellow, FString("ran : ") + ( ran? FString("YES") : FString("NO")) );
but it doesn’t run - it returns false and the command doesn’t execute. I’ve tried running it in BeginPlay and also in the Tick() function.
Can anyone tell me why this doesn’t work?
Azarus
(Azarus)
May 2, 2015, 12:10am
2
You need the player controller.
APlayerController* PController= UGameplayStatics::GetPlayerController(GetWorld(), 0);
if( PController )
{
PController->ConsoleCommand(TEXT("stat fps"), true);
}
stat fps is not related to the UWorld object so it won’t have any effect. This is why its not working, wrong context.
That worked great, thanks .
I had based my code on this forum response:
I guess things have changed a bit since then.
I swear it was working earlier… now it’s not working at all. Cut and pasted again and doesn’t do anything. I’m running it in multiplayer if that makes any difference.
Can anyone verify the above code works?
I checked that the code was being called via a debug message, and it was.
For some reason I needed to have PrimaryActorTick.bCanEverTick = true, otherwise even though the code was called the command wouldn’t execute.
Thank you ,
this helped me a lot.
If anyone wants to limit the framerate on runtime, this is the way to go:
APlayerController* PController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
if (PController){
FString command = TEXT("t.maxfps %i"), _newFramerate;
PController->ConsoleCommand(command, true);
}
Moss
(Moss)
December 23, 2015, 6:57pm
7
As @ said it’s all about where to call the command on, the STAT command is actually located in UEngine so the best way to call it would be like:
GEngine->Exec("stat fps");
@ is completely correct too ^^