I want my in-game console to provide me with necessary functionality

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

I have certain items that I want my character to consume, but only when there is a command given through an in-game console which is where the user types their commands. I want to make it so that the command functions like an action just pressed input event, where the user enters a command like pig.collect_item, and this prints the String "You have collected a banana, and the banana disappears with queuefree().

func _input(event):
if event.is_action_pressed(“quentincaffeino_console_autocomplete”):
queue_free()

I’m trying to emulate games like Minecraft where the user controls the world through lines of code. So I just want to know how I can make my banana disappear when a command is entered into an in-game console.

Thank you

:bust_in_silhouette: Reply From: SF123

You could have a LineEdit node or a TextEdit node that appears when you press a button like t or whatever you want and disappears when you press esc or just whatever you want it to be… here’s an idea of what the code could be:

var console_focused = false

func _input(event):
    if event.is_action_pressed("open_console"):
        if not console_focused:
            $Console.show()
            $Console.grab_focus()
            console_focused = true
    elif event.is_action_pressed("submit_console") and console_focused:
        var command = $Console.text
        $Console.hide()
        console_focused = false
        
        if len(command) > 0:
            #Do whatever you want the command to do
            if command == "func _input(event):\nif event.isactionpressed("quentincaffeinoconsoleautocomplete"):\nqueuefree()":
                do_whatever()

This was useful, however I think you misunderstood my question. I was asking for a command that would allow my user to destroy objects, by writing an instruction like collect_item, and then the item disappearing when this command was typed into a console i.e. at the line edit. Also, if command == func _input(event), shows up as the func being misplaced.

Is there anyway of implementing functionality so that I can destroy objects from the in-game console?

Many thanks once again.

Juan | 2022-02-10 15:40

Oh! I’m so sorry!

I guess I did misunderstand this…
Alright. First of all you said,

Also, if command == func _input(event), shows up as the func being misplaced.

Do you have it in quotes? Correction to my old one:

if command == "func _input(event):\nif event.isactionpressed(\"quentincaffeinoconsoleautocomplete\"):\nqueuefree()":

But from what I understand you’d want it like this:

if command == "pig.collect_item":
    print("You have collected a banana, and the banana disappears with queue_free()")
elif command == "queue_free()":
    #Still not fully sure what you'd want it to do...

Yeah, I apologize but I don’t know how you have the pickup mechanic…is it added to an inventory variable, does it set banana_collected = true, not sure.

If there’s anything else, or anything I missed, let me know and I’ll try my best to figure it out.

SF123 | 2022-02-10 20:33

So yeah, the most simple way of describing what I’m trying to achieve with the gameplay is to type a command like pig_collect_item banana. And when the command is entered into the console debugger, the banana sprite disappears like a queuefree() function. For now, all I want to know is how I can give my console the functionality needed to execute this kind of procedure.

Bear in mind that my console is a canvassLayer, in a different scene, but can be propagated anywhere in my game.

Juan | 2022-02-11 16:40