Skip to content

Commit

Permalink
Add eval command, add/remove variables for eval command
Browse files Browse the repository at this point in the history
  • Loading branch information
limbonaut committed Sep 11, 2024
1 parent 4dd1fc3 commit 932cba7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions builtin_commands.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static func register_commands() -> void:
LimboConsole.register_command(LimboConsole.clear_console, "clear", "clear console screen")
LimboConsole.register_command(cmd_commands, "commands", "list all commands")
LimboConsole.register_command(LimboConsole.info, "echo", "display a line of text")
LimboConsole.register_command(cmd_eval, "eval", "evaluate an expression")
LimboConsole.register_command(cmd_fps_max, "fps_max", "limit framerate")
LimboConsole.register_command(cmd_fullscreen, "fullscreen", "toggle fullscreen mode")
LimboConsole.register_command(cmd_help, "help", "show command info")
Expand Down Expand Up @@ -42,6 +43,22 @@ static func cmd_commands() -> void:
LimboConsole.info(name if desc.is_empty() else "%s -- %s" % [name, desc])


static func cmd_eval(p_expression: String) -> Error:
var exp := Expression.new()
var err: int = exp.parse(p_expression, LimboConsole.get_eval_input_names())
if err != OK:
LimboConsole.error(exp.get_error_text())
return err
var result = exp.execute(LimboConsole.get_eval_inputs())
if not exp.has_execute_failed():
if result != null:
LimboConsole.info(str(result))
return OK
else:
LimboConsole.error(exp.get_error_text())
return ERR_SCRIPT_FAILED


static func cmd_fps_max(p_limit: int = -1) -> void:
if p_limit < 0:
if Engine.max_fps == 0:
Expand Down
21 changes: 21 additions & 0 deletions limbo_console.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _argument_autocomplete_sources: Dictionary # [command_name, arg_idx] => Call
var _history: PackedStringArray
var _hist_idx: int = -1
var _autocomplete_matches: PackedStringArray
var _eval_inputs: Dictionary
var _silent: bool = false


Expand Down Expand Up @@ -375,6 +376,26 @@ func usage(p_command_name: String) -> Error:
return OK


## Define an input variable for "eval" command.
func add_eval_input(p_name: String, p_value) -> void:
_eval_inputs[p_name] = p_value


## Remove specified input variable from "eval" command.
func remove_eval_input(p_name) -> void:
_eval_inputs.erase(p_name)


## List the defined input variables used in "eval" command.
func get_eval_input_names() -> PackedStringArray:
return _eval_inputs.keys()


## Get input variable values used in "eval" command, listed in the same order as names.
func get_eval_inputs() -> Array:
return _eval_inputs.values()


# *** PRIVATE

# *** INITIALIZATION
Expand Down

0 comments on commit 932cba7

Please sign in to comment.