How can I make my sprites disappear when I type a command in a in-game console debugger

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

I at the moment have a code debugger which I would like to add functionality to. Therefore, this means when I type a command like “collect_item banana”, the sprite of the banana will disappear.

Also, I would like to do more things with my debugger like using the command line to allow the user to enter text i.e. command, for example “open_door”, and in the game the door will open.

I’ve been trying to do this for ages! I feel like it is a very difficult function to implement.

Anyone got any ideas. Would be very much appreciated.

Many thanks

:bust_in_silhouette: Reply From: Kazuren

You can probably add a way to submit a command when a user presses enter in the _process function of your code debugger which would emit a signal that any other node could listen to

signal command_submitted(command)

func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("ui_accept"):
        # do any cleanup required here like clearing the console from text
		emit_signal("command_submitted", command)

and then on a parent node you could do

func _ready():
    code_debugger.connect("command_submitted", self, "_on_CodeDebugger_command_submitted")
func _on_CodeDebugger_command_submitted(command):
    # you could split the command here because it's a string into
    # function_name arg1 arg2 arg3 etc
    var args = command.split(" ")
    # first substring is the function name
    # rest are the arguments of the function
    var function_name = args.pop_front()
    callv(function_name, args)

func collect_item(item):
    # collect item here
func open_door():
    # open door here

you can read more about callv here
I haven’t tested the code above but I am sure something similar to this works.

Here when you use the emit_signal (the argument inside the brackets), command, is not declared in the current scope. Also when you use code_debugger are your referring to the name of the actual console where my script is attached to.

With collect_item(item) I don’t see how the banana would disappear, in terms of a queue_free() sort of function that removes the sprite.

This was very helpful in further understanding how I will implement the most important functionality of my gameplay.

Thanks once again

Juan | 2022-02-11 14:17

You were correct to suggest that there is an emit signal, however the identifier is not declared in the current scope?

However, with the command_submitted I think this would have to be a function by itself. For example:

func add_command(name, target, target_name = null):
emit_signal(“command_added”, name, target, target_name)
return self._command_service.create(name, target, target_name)

Also, with picking up an item, it would be as simple as making the banana disappear when a specific command is imputed, and from their a signal to the node to queue_free().

I’m not really sure, trying to make heads and tails out of this.

Juan | 2022-02-11 16:13

I am assuming the codedebugger is a node you have in your scene that acts as a text field. If that’s the case then you should be able to have a way to get its current value when the user presses enter. The command variable is this value it could be named something else in your script.
eg. “collect_item banana”
The collect_item(item) function would get as input the “item” you wrote
so for example “collect_item banana” would give it “banana” as input and then you have multiple ways of getting your banana node to make it disappear.
you could use get_node, find_node
you could add the banana node to a “banana” group either from the editor or in its _ready() function and then get it that way.
and then once you have the banana node you could do
banana.visible = false or banana.queue_free() depending on what you want to happen with it.

Kazuren | 2022-02-11 16:14

The code debugger which my script is attached to, is a canvas layer. But I tried making the command_submitted a function first, like so:

func command_submitted(command):
emit_signal(“command_submitted”, command)

and this took away the errors but I have a feeling there is a lot more to this…

Many thanks once again, and I will try your way, step by step.

By the way, my console is in a completely different scene, but can propagated using Ctrl and the quote key(') anywhere in the game.

Juan | 2022-02-11 16:22

So I’ve tried this out, and the good thing about this experiment is that whenever a command is given that does not contain the name Bannana, which is spelt wrong for more clarity, it will give a message saying, "You have picked up a banana(spelt correctly).

However, when the specific word comes up after the command pig_collect_item Bannana(spelt wrong here), it will queue_free and will display Attempt to call function queue_free() in base ‘null instance’ on a null instance.

And this is the code I used:

func pig_collect_item(item = ‘’):
Console.write_line('You have collected a ’ + item)
get_node(“Banana/Bannana”)
$Banana/Bannana.queue_free()

Juan | 2022-02-11 17:00

I made a small project which describes the way I would do it. You can download it and import it to test it out
GitHub - Kazuren/CodeDebugger: code debugger answer for https://godotengine.org/qa/127545/make-sprites-disappear-type-command-game-console-debugger?show=127649#c127649
click on the text editor and write “collect_item banana” to see the sprite disappear.
Note that I have added an “enter” action in the input map and I am getting the banana using a group.
There’s no error checking so if you write something other than “collect_item banana” it will fail but that should be easy to implement, you would basically check if the function exists first and then you could check if the group exists.

Kazuren | 2022-02-11 17:21

Mate you’re a savior, we need to talk more outside this forum. I’m doing a degree in Computer Science, and I decided to create a game in Godot for my final major project. Obviously, I was never taught game development, but I thought it would be a challenge. Truth is that it has been and uphill struggle, with a lot of positive things taken of this experience.

Anyway, I appreciate the support, and I will keep you up to date with my progress, just for peace of mind as they say.

Thanks again,

Juan

Juan | 2022-02-11 17:31

Glad I could help. You can add me on discord if you have any more questions.
I’d be happy to answer them.
Kazuren#1635

Kazuren | 2022-02-11 17:45

Hey Kazuren,

As per our previous conversation, I used the code you provided me and found it extremely useful, in allowing me to use my debugger for different functions, such as the main one; collecting items by entering text and input for the command. I was wondering if I would be able to use an autocomplete so that the player can be assisted to write the correct command.

It has been hard trying to find code which I can easily embed as a feature of my command console i.e., autocomplete. Could you guide me as to how to implement this feature, would be very much appreciated.

Juan

Juan | 2022-03-19 17:49

You should add me on discord and I can help you more specifically there. I’ve posted my username above. Also I did reply to one of your recent questions suggesting what you should do, you should look into that if you haven’t.

Kazuren | 2022-03-20 00:23