0 votes

Hello, guys, who knows how to implement an in-game console in Your project? Example of consoles: Skyrim, CS, etc.

in Engine by (148 points)

2 Answers

0 votes

Hi,
this is my approach:

if have a container with a LineEdit in it. Output is debugger console but can be easly changed

extends LineEdit
class_name Console

var objects:Dictionary
var open:bool = false

func _ready():
    app.console = self


func register( object ):
    objects[object.name] = object


func _input(event):
    if event.is_action_pressed("ACTION_CONSOLE"):
        if open:
            close()
        else:
            open()


func open():
    open = true
    app.disable_actions = true
    get_parent().visible = true
    get_parent().get_parent().visible = true
    grab_focus()
    Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)


func close():
    open = false
    app.disable_actions = false
    get_parent().visible = false
    get_parent().get_parent().visible = false
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _on_TextEdit_text_entered(new_text:String):
    close()

    print("Console: "+new_text)
    var parts:PoolStringArray = new_text.split(" ", false )

    text = ""

    var cmd
    var objectName
    var attributes

    if parts.size() == 0:
        return
    if parts.size() > 0:
        cmd = parts[0]
        cmd = "cmd_"+cmd.trim_prefix("_")
        parts.remove(0)
    if parts.size() > 0:
        objectName = parts[0]
        parts.remove(0)
    if parts.size() > 0:
        attributes = parts

    if has_method(cmd):
        # first try to call local
        call(cmd,objectName,attributes)
    else:
        #then try to call on object
        call_on_object( cmd, objectName, attributes )


func call_on_object( cmd, objectName, attributes ):
    if not objects.has(objectName):
        print("Console: object unknown")
        return

    var object = objects[objectName]

    if object.has_method( cmd ):
        object.call(cmd, attributes)
    else:
        print("Console: methode unknown")
        return

#------------------- list of commands avail

func cmd_get_names(objectName, attributes):
    for name in objects:
        print( name )

while app is a singleton is use to store objects globaly.

Since i want to address functions in objects ... objects have to register with

app.console.register(self)

i disable all other inputs like WASD movement here. You have to implement this in your _inputs

app.disable_actions = true

all my commands get prefixed with "cmd_" to keep user from accessing other functions
the console first tries to call the command localy when there is no local function it then tries to call it on the object of the given name

by (3,932 points)

Yeah, "I see.".. Thanks...

0 votes

I haven't tried it yet, but there is one in the asset lib: https://godotengine.org/asset-library/asset/103

by (1,094 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 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 webmaster@godotengine.org with your username.