Godot Version
4
Question
Does anyone know or have code on how to make commands when typing in a word, like a text adventure, I'm making a fighting game where you have to type your attack
4
Does anyone know or have code on how to make commands when typing in a word, like a text adventure, I'm making a fighting game where you have to type your attack
You can use a LineEdit node and compare the text the user inputs to the command words.
You can also get keyboard input directly, if (say) you’re making a “Typing of the Dead” style game.
yeah, but I hoped there was a better way than getting every input even if I did, I don’t know how to make typing at all
I’ll check this out, but do you know how I could turn the keyboard into being able to type, I don’t even know how to name character using text and such
the lineedit already takes keyboard input when the player clicks on it
ok cool
what code compares the words?
Seriously?
if this_string == another_string
So you would have something like:
const COMMANDS: Array = [
"shoot",
"run",
"hide",
"eat",
"cry",
]
# and somewhere else in relevant function
var active_command: String = ""
for command_word: String in COMMANDS:
if word_user_input == command_word:
active_command = command_word
break
# then do whatever you want with the active_command
Something like that anyway. Hope that helps.
thanks dude this helps a bunch
how do you get the words or string from the line edit into the code, what signal do I use
and is there a way to get those words without pressing enter because as soon as a command has been typed I want it to reset the text box to having no words and I want the backspace to delete all the current words as well, so is there a way to always be checking if a command word has been typed
Use a LineEdit node:
Then use the signal in the inspector:
func _on_line_edit_text_changed(new_text):
# do something with the new text
What you can then do is every time the text changes, you can run it through your commands to see if it is recognised. If it is you could then implement the command and set the text to blank again.
I hope that helps.
Here is an example for you. Create a node2d and attach a script. Add a LineEditNode as a child. Connect the text changed signal in the LineEdit node with the script. In the script add this:
extends Node2D
@onready var COMMAND_INPUT: LineEdit = $LineEdit
const COMMANDS: Array = [
"shoot",
"run",
"hide",
"eat",
"cry",
]
func _on_line_edit_text_changed(new_text: String) -> void:
var is_command_recognised: bool = check_command_text(new_text)
if is_command_recognised:
COMMAND_INPUT.clear()
implement_user_command(new_text)
func check_command_text(user_input: String) -> bool:
for command_word: String in COMMANDS:
if user_input == command_word:
return true
return false
func implement_user_command(command: String) -> void:
# Do whatever with the command
print("User command: ", command)
Now when you type ‘cry’ the input clears and the command is printed.
Again, hope that helps.
PS You can do tons of exciting things with this, like suggesting commands as they type things, perhaps even fuzzy searching so ‘ry’ would suggest ‘cry’, making an error sound and clearing the input if there is no chance of it matching a command (like if you misspelled something), adding the commands to a list of most recent commands, creating a ‘most used’ shortcut bar to show the commands most used for one click activation, firing particles when clearing the text to show the command was implemented. Good luck with this, your project sounds interesting.
thanks, dude this helps a bunch. do you know if you can see a ghost of the rest of the word that it looks like you’re typing. “cr(y)”
Use rich text in the label and you can change the color/alpha of part of the displayed string.
That is called “type ahead”.
There is no built in method for that.
You can achieve in code but it may be a challenge.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.