0 votes

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

Godot version 3.4
in Engine by (72 points)

1 Answer

0 votes
Best answer

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.

by (62 points)
selected by

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

With collectitem(item) I don't see how the banana would disappear, in terms of a queuefree() 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

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 addcommand(name, target, targetname = null):
emitsignal("commandadded", name, target, targetname)
return self.
commandservice.create(name, target, targetname)

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.

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.

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 commandsubmitted(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.

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 pigcollectitem Bannana(spelt wrong here), it will queuefree and will display Attempt to call function queuefree() in base 'null instance' on a null instance.

And this is the code I used:

func pigcollectitem(item = ''):
Console.writeline('You have collected a ' + item)
get
node("Banana/Bannana")
$Banana/Bannana.queue_free()

I made a small project which describes the way I would do it. You can download it and import it to test it out
https://github.com/Kazuren/CodeDebugger
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.

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

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

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

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.

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.