0 votes

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, movestate left, or movestate 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 inputbox = getnode("input")
onready var outputbox = getnode("output")
onready var commandhandler = getnode("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 checktype(string, type):
if type == command
handler.ARGINT:
return string.is
validinteger()
if type == command
handler.ARGFLOAT:
return string.is
validfloat()
if type == command
handler.ARGSTRING:
return true
if type == command
handler.ARG_BOOL:
return (string == "true" or string == "false")
return false

func outputtext(text):
output
box.text = str(output_box.text, "\n", text)

func oninputtextentered(newtext):
input
box.clear()
processcommand(newtext)

All of this is absolutely fine, and works.

extends Node2D

onready var player = getparent().getparent().getnode(".")
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
enum{
MOVE
}
var state = MOVE
var input
vector = Vector2.ZERO

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

enum{
ARGINT,
ARG
STRING,
ARGFLOAT,
ARG
BOOL
}

const validcommands = [
["set
speed",
[ARGFLOAT]
],
["move
state",
[ARGSTRING]
],
["set
friction",
[ARG_INT]
]
]

func _ready():
pass

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

func movestate(delta):
input
vector = String(inputvector)
if input
vector == "uileft":
animationTree.set("parameters/Walk/blend
position", inputvector)
input
vector.x = Input.getactionstrength("uiright") - Input.getactionstrength("uileft")
player.inputvector = Input.getactionstrength("uileft")
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...

Godot version 3.4
in Engine by (72 points)

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

func movestate(delta):
input
vector = String(inputvector)
if input
vector == "uileft":
animationTree.set("parameters/Walk/blend
position", inputvector)
input
vector.x = Input.getactionstrength("uiright") - Input.getactionstrength("uileft")
player.inputvector = Input.getactionstrength("uileft")
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.

1 Answer

0 votes

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

by (1,342 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.