How to input a command inside a command debugger

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Juan

I have recently created a debugger for my game, using a godot tutorial which I think was a pretty neat way for my player to engage with the world of created for my character. So far I’ve been able to set my command for Max_speed which increases the speed at which the player moves.

Now I want to take it a step further and implement a command that allows the player to move, up down, left right, when the player inputs a command line like, move_state left, or move_state right. I’ll show you my code for the console itself, and the command_handler, where all the instructions for how the commands are going to be executed are stored.

extends Control

onready var input_box = get_node(“input”)
onready var output_box = get_node(“output”)
onready var command_handler = get_node(“command_handler”)

func _ready():
input_box.grab_focus()

func process_command(text):
var words = text.split(" “)
words = Array(words)
for i in range(words.count(”“)):
words.erase(”")
if words.size() == 0:
return

var command_word = words.pop_front()

for c in command_handler.valid_commands:
	if c[0] == command_word:
		if words.size() != c[1].size():
			output_text(str('Failure executing command"', command_word, '", expected', c[1].size(), 'parameters'))
			return
		for i in range(words.size()):
			if not check_type(words[i], c[1][i]):
				output_text(str('Failure executing command "', command_word, '", parameters', (i +1),
							'("', words[i], '") is of the wrong type'))
				return
		output_text(command_handler.callv(command_word, words))
		return
output_text(str('Command"', command_word, '"does not exist.'))

func check_type(string, type):
if type == command_handler.ARG_INT:
return string.is_valid_integer()
if type == command_handler.ARG_FLOAT:
return string.is_valid_float()
if type == command_handler.ARG_STRING:
return true
if type == command_handler.ARG_BOOL:
return (string == “true” or string == “false”)
return false

func output_text(text):
output_box.text = str(output_box.text, “\n”, text)

func _on_input_text_entered(new_text):
input_box.clear()
process_command(new_text)

All of this is absolutely fine, and works.

extends Node2D

onready var player = get_parent().get_parent().get_node(“.”)
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
enum{
MOVE
}
var state = MOVE
var input_vector = Vector2.ZERO

func _physics_process(delta):
match state:
MOVE:
move_state(delta)

enum{
ARG_INT,
ARG_STRING,
ARG_FLOAT,
ARG_BOOL
}

const valid_commands = [
[“set_speed”,
[ARG_FLOAT]
],
[“move_state”,
[ARG_STRING]
],
[“set_friction”,
[ARG_INT]
]
]

func _ready():
pass

func set_speed(MAX_SPEED):
MAX_SPEED = float(MAX_SPEED)
if MAX_SPEED >= 50 and MAX_SPEED <= 400:
player.MAX_SPEED = MAX_SPEED
return str("Succesfully set speed to ", MAX_SPEED)
return “Speed value must be between 50 and 400!”

func move_state(delta):
input_vector = String(input_vector)
if input_vector == “ui_left”:
animationTree.set(“parameters/Walk/blend_position”, input_vector)
input_vector.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
player.input_vector = Input.get_action_strength(“ui_left”)
return str(“Succesfully moved to the left”, “ui_left”)
return “Direction must be up, down, left, right”

func set_friction(FRICTION):
FRICTION = int(FRICTION)
if FRICTION >= 5 and FRICTION <=400:
player.FRICTION = FRICTION
return str(“Succesfully set friction to”, FRICTION)
return “Friction must be between 50 and 400”

I want to run with this theme/idea that the player can control the character through a set of commands, which appear in a debugger console. Can someone suggest a way that I can structure my code so that I can execute this functionality.

Thank you…

To make it easier for you to answer I will split the section that I need help with, which is the movement part.

func move_state(delta):
input_vector = String(input_vector)
if input_vector == “ui_left”:
animationTree.set(“parameters/Walk/blend_position”, input_vector)
input_vector.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
player.input_vector = Input.get_action_strength(“ui_left”)
return str(“Succesfully moved to the left”, “ui_left”)
return “Direction must be up, down, left, right”

When I wrote this code, I was referring to the kinematic body, and therefore tried to emulate the other float functions that set commands. Also, I think it is something to do with the ARG type, and I’m not sure if it takes a string value. But in my rationale, it seems that in order to make a player go right a command must be imputed, and it should take a string parameter, like for example move_state left for moving left.

Juan | 2022-01-31 20:04

:bust_in_silhouette: Reply From: rossunger

I’m not sure I understand what you need help with.

Are you familiar with Node’s “call()” function? it allows you to call a function by passing it the name of the function