I’m implementing some console commands, using FAutoConsoleCommand. I’ve done some Exec functions before, but, since it’s already deprecated, I started to use the former.
This is the code I’m using to register my command in my UCheatManager, which does work.
The thing is, CSetHeroStaminaExecuted is a static function, but I need to call a non-static Blueprint-implementable function. How can I bind that UFunction to the console command, or retrieve the World to get to my PlayerController’s cheat manager, cast it and call my UFunction? GEngine->GetWorld() seems to be returning nullptr always. I’m aware there’s this FConsoleCommandWithArgsDelegate::BindUFunction(), but I’m not sure how to pass the WorldContext object it needs to do the binding.
GEngine->GetWorld shouldn’t be returning null. That seems off. You could store a static pointer to your Hero (assuming there’s only one) on your game mode and then create a static method to grab it. Basically use a singleton-esque model.
Turns out I had an epiphany and decided to store a static pointer to the world itself in the constructor of my CheatManager*.*
UCSCheatManager::UCSCheatManager()
{
UCSCheatManager::world = GetWorld();
if (UCSCheatManager::world != nullptr)
{
UE_LOG(LogClass, Log, TEXT("Found a valid world"));
}
}
It does work and I’m able to call non-static functions from my manager, but again, is it there a “proper” way to do it? According to this (unless my english is that bad, as it’s not my native language), it shouldn’t be accessed in a constructor.
OK, going with Matt’s suggestion and my previous workaround, what I did was to create a singleton-esque static reference to my cheat manager. Seems to do what I need, but still, it’d be great to have better documentation on FAutoConsoleCommand, specially on binding events. Console Manager section of the manual only gives details about console vars. Hope the Shooter project has something like this.
In the end, my solution was to create a static instance for my cheat manager, then call the non-static function from that static instance when using FAutoConsoleCommand.