I've recently been thinking about creating an ingame console for my projects.
I decided to define the commands in a json file which matches the command and argument count to a 'procedure'.
[
{
"command":"close",
"args": [0],
"procedure": ["close_application"]
},
{
"command":"makebox",
"args": [1,2,3],
"procedure": ["Create_2D_Square","Create_2D_Rect","Create_3D_Box"]
}
]
Then in AppGameKit, I would parse the input string to determine the procedure and get the arg list. Then run the 'procedure', and I know the correct number of args exist.
type tConsoleCommand
Procedure as string
Args as string[]
endtype
parsedInput as tConsoleCommand
// this parses the input string and uses the json to determine the procedure.
parseConsoleInput( consoleInputString, parsedInput )
select ( ParsedInput.Procedure )
case "close_application"
// end the program
endcase
case "Create_2D_Square"
// create square sprite. Width and height values both from ParsedInput.Args[0]
case "Create_2D_Rect"
// create rectangle sprite with the width and height as ParsedInput.Args[0], ParsedInput.Args[1]
endcase
case "Create_3D_Box"
// create object box with the width, height, Depth as ParsedInput.Args[0], ParsedInput.Args[1], ParsedInput.Args[2]
endcase
endselect
I have even toyed with the idea of having sub-commands. That json might look like,
[
{
"command":"console",
"extend":{
"subcommands":[
{
"command":"clear",
"args": [0],
"procedure": ["console_clear"]
},
{
"command":"close",
"args": [0],
"procedure": ["console_close"]
},
{
"command":"resize",
"args": [1,2],
"procedure": ["console_resize_square","console_resize_rect"]
},
{
"command":"save",
"args": [1],
"procedure": ["console_saveoutput"]
}
]
}
},
{
"command":"player",
"extend":{
"subcommands":[
{
"command":"kill",
"args": [0],
"procedure": ["player_kill"]
},
{
"command":"sethealth",
"args": [1],
"procedure": ["player_sethealth"]
}
]
}
},
]
This would define these commands:
console.clear
console.close
console.resize ( size | width, Height )
console.save ( file )
player.kill
player.sethealth ( health )
This might not be exactly what you're after, but its something similar that I have come up with.