NjConsole’s Command Line lets you run user-defined and built-in commands through text input.
Commands are registered the same way as the Options Menu (via [ConsoleOption]
or programmatically).
See Options Menu for full details.
By default, all Options Menu entries also appear as commands. To separate the two, see Managing Command Catalogs.
Tab
accepts the first suggestion.Shift+↑
/ Shift+↓
to navigate suggestions, Tab
or Enter
to accept.↑
/ ↓
cycles through previous commands.Esc
closes the Command Line.⌨
button to toggle between prompt and normal input.<command> <parameters seperated by space ` ` or comma `,`>
Built-in commands appear under /
.
Example: /help
lists all commands.
String params can be wrapped in quotes ("
) to include spaces or commas. Escape "
with \"
.
Notes:
/
creates grouped folders, like in the Options Menu.[ConsoleOption]
void SayHello() {
Debug.Log("Hello");
}
Command: sayhello
[ConsoleOption("profile/name")]
public string Name;
Get Command: profile/name
Set Command: profile/name "My name here"
[ConsoleOption("demo/introduce")]
void IntroducePerson(string name, int age) { ... }
Command: demo/introduce Ninjadini 30
or demo/introduce "Ninjadini", 30
If a parameter is an object that requires constructor arguments, group them in parentheses:
[ConsoleOption("math / vector multiply")]
static Vector3 MultiplyV(Vector3 a, float b) => a * b;
Command: math/vector multiply (1 2 3) 1
or math/vector multiply (1,2,3),1
Nested constructors:
[ConsoleOption]
void NestedConstructor(PositionAndSize a, string message) => Debug.Log($"Position: {a.Pos}, Size: {a.Size}, Message: {message}");
struct PositionAndSize {
public Vector3 Pos;
public float Size;
public PositionAndSize(Vector3 pos, float size) {
Pos = pos;
Size = size;
}
}
Command: nestedConstructor ((1, 2, 3), 3), "hello there"
[ConsoleOption("profiles/GetUserId")]
string GetUserId() { ... }
Command: profiles/getuserid
Output: > ‘UserId1234’
< the returned string from GetUserId method.
Returned objects are stored in $_
and can be reused:
[ConsoleOption("profiles/GetProfile")]
PlayerProfile GetProfile(string id) { ... }
Command: profiles/getprofile $_
Output: > PlayerProfile; Scope $@ set.
ℹ️ If a command returns a null object, the returned object variable
$_
will not change.
/store <name>
: store the last returned object.
/retrieve <name>
: retrieve a stored object.
/list stored
: list stored variables.
Example:
[ConsoleOption("profiles/loadprofile")]
void LoadProfile(PlayerProfile value) { ... }
Command: /store profile
Command: profiles/loadprofile $profile
Output: `> PlayerProfile; Scope $@ set.`
When a command returns a class object, the Command Line automatically switches scope to that object.
This is shown in the output as: Scope $@ set
.
Scope Variables
$@
— current scope object.$@prev
— previous scope object.$_
— last returned object (not necessarily scoped).Some built-in commands (like /call
) act directly on the current scope object, letting you invoke any field, property, or method via reflection:
/call Name
/call Name "New name here"
/call SetAge 30
/call
uses reflection. Member names are case-sensitive.
/rescope
: return to the previous scope ($@prev
).
/scope <stored name>
: switch to a stored variable’s scope.
If you log an object like this:
NjLogger.Info("Player Profile link: ", playerProfile.AsLogRef());
⌨
to send that object to the Command Line (e.g., /store profile
)./inspect
to open the last returned object in the Inspector.From NjConsole’s hierarchy panel:
⌨
to pass it to the Command Line.To register commands for Command Line only (not Options Menu):
NjConsole.CommandLineOptions.CreateCatalog...(...)
This API is the same as the Options Menu catalog.
NjConsole.Options.CommandLinePath = null;
NjConsole.Options.CommandLinePath = "<folder>"
You can access stored variables programmatically the same way built-in commands do:
var storage = NjConsole.Modules.GetOrCreateModule<ConsoleObjReferenceStorage>();
var lastResult = storage.GetLastResult(); // same as `storage.GetStored("_")`
var scope = storage.GetScope(); // same as `storage.GetStored("@")`
var customVar = storage.GetStored("profile"); // if you called `/store profile`
Returned objects, scopes and other stored $ variables will be strong referenced and will not be garbage collected.
Call/clear stored
to clear everything.
If a command returns an IConsoleCommandlineModule, the Command Line switches into a locked mode, where the next input is routed to that module. This is useful for creating interactive prompts, multi-step wizards, or temporary modes.
[ConsoleOption("food prompt")]
IConsoleCommandlineModule FoodPrompt() => new DemoFoodPromptHandler();
class DemoFoodPromptHandler : IConsoleCommandlineModule {
public bool TryRun(IConsoleCommandlineModule.Context ctx){
if (ctx.Input == "exit") {
ctx.Output.Info("Good bye!");
} else {
ctx.Output.Info($"Your 'name of food': \"{ctx.Input}\"");
ctx.Result = this; // Keep input locked to this module.
}
return true; // Indicates the input was handled.
}
public void FillAutoCompletableHints(IConsoleCommandlineModule.HintContext ctx) {
// Suggest 'exit'
var remaining = StringParser.GetRemainingPartialMatch(ctx.Input, "exit");
if (remaining != null) {
ctx.Add(remaining, "<alpha=#44> Exit the demo prompt");
return;
} else {
// Suggest generic text without tracking whats already typed (-ctx.Input.Length)
ctx.Add("", "A name of food", -ctx.Input.Length);
}
}
}
See GuessNumberCommandLineGame
in DemoNjConsole.cs or call guessTheNumber
in demo scene for a full example.
Similar to custom input prompt handling, you can also create an extension and add it in project settings.
PopulateHelpSuggestions(List<string> helpLines)
to display help text.Project Settings > NjConsole > Extension Modules > Add Extension Module
> add your new classApply changes
[Serializable]
public class DemoFoodPromptHandler : IConsoleCommandlineModule, IConsoleExtension {
...
...
public void PopulateHelpSuggestions(List<string> helpLines) {
helpLines.Add("<Type any food you like>");
}
public bool PersistInEditMode => true; // OPTIONAL: If you need your custom commands to be runnable outside play-mode.
void OnAdded(ConsoleModules modules) {
// OPTIONAL: How to disable Options Menu's command line.
var optionsCmdModule = NjConsole.Modules.GetModule<OptionCommandsModule>(false);
if (optionsCmdModule != null)
{
optionsCmdModule.Enabled = false;
}
}
}
Your custom executor is evaluated before the default Command Line, except that /
commands always take priority.
If TryRun()
returns false
, NjConsole passes the input to the next enabled Command Line Module.