Console custom auto-completion?

I am trying to implement a console command like "drop " where I want the user to type a name from a custom list of strings… an Exec function with a string parameter gets me that… but I want the console to aid the user with auto-completion from my custom list of strings.

Is there any way to do this with CheatManager exec-functions or console-variables?

I cannot create an enum… I’d like to supply a function or something that returns a list built at run-time.

It looks like autocompleting arguments isn’t as convenient as you’d hope. You can’t define a command and provide a function to provide autocomplete results. Instead, the list of possible commands (including their autocompleted arugments) is built as a monolithic list. However, this list is built at runtime and you can force the list to be dirty (when new objects are added, etc).

You’ll need to subclass UConsole and override BuildRuntimeAutoCompleteList to include your commands in AutoCompleteList. See how map loading commands are built in the loop iterating over map names in Console.cpp line 184:

AutoCompleteList[NewIdx].Command = FString::Printf(TEXT("open %s"),*TrimmedMapName);

You’ll have to iterate over the objects you want droppable and add commands like this yourself.

To set the auto complete list as dirty, set bIsRuntimeAutoCompleteUpToDate=false at any time. It looks like once that flag is dirty, the autocomplete list will be rebuilt on the next keyboard console input.

I’m not sure how you tell the engine to use your new console class, but I’d guess it’s similar to extending GameEngine. Add the following to GameName/Config/DefaultEngine.ini:

[/Script/Engine.Engine]
Console=/Script/GameName.GameNameConsole

(Assuming you made a UGameNameConsole in your game module.)

(Caveat: This was too complex for me to bother, so I haven’t actually tried this yet. I investigated on Unreal 4.11.2)

Also note that there’s the red herring of UConsoleSettings::ManualAutoCompleteList which appear to just be a list of all commands (not autocompleted arguments). Using UFUNCTION(Exec) seems to automatically add your command to autocomplete so messing with ManualAutoCompleteList is not required. I think it’s only for the old UEngine::HandleObjCommand-style parser commands.

If anyone is still having similar problem, there is a delegate for that now (since 4.15)

UConsole::RegisterConsoleAutoCompleteEntries

It is surprisingly simple to set up custom autocomplete, thanks

Forgive the bump, but it turns out autocompletion in Unreal comes from a lot of places besides UConsole::RegisterConsoleAutoCompleteEntries.

The UConsole hook has a notable drawback: It only works when there is a UGameViewportClient instance to host it, so it requires an active game world. If you want the editor console command executor to include your autocompletion as well, you need to use something else.

FParse::Command has a built-in command gathering mechanism that seems to be used in some places (mainly DumpConsoleCommands), but there’s a much easier way that works without implementing a special interface.

It’s a bit of a hack, but here’s the best solution I’ve found so far:

// Register
IConsoleObject* RegisteredCommandPreview = IConsoleManager::Get().RegisterConsoleCommand(AutoCompleteText, Description, ECVF_Default);

// Unregister
IConsoleManager::Get().UnregisterConsoleObject(RegisteredCommandPreview);

You’re probably used to using IConsoleManager::Get().RegisterConsoleCommand for registering commands, but turns out you can register empty console object entries without a callback, meaning it just adds it to the list of console objects for both UConsole and FConsoleCommandExecutor to auto-gather without intercepting execution of your original console command.

Just note that if you add too many it can slow down generation of the autocomplete tree, and has the downside of adding every autocompletion you register to the list printed by DumpConsoleCommands.

To progressively register those autocomplete entries in a way that prevents flooding the console command list with too many autocomplete options, it’s a little tricky. I couldn’t find any accessible ways to hook into text input changes. I needed to find places to inspect the editor/gameplay consoles and set a timer that checks for input text changes. The UConsole is pretty easy to find, but for the editor console command input you need to search for the relevant text input widgets. After that you can implement your own autocompletion algorithm to register/unregister relevant console commands.

I made a tutorial for this