Begins_with() not working as intended "FIXED"

Godot Version

Question

extends Node2D

@onready var line_edit = $LineEdit

var text : String

func _on_line_edit_text_submitted(new_text):
	
	if new_text.begins_with(Global.letter):
		newRound()
	else:
		return
		
func newRound():
		
	Global.currentRound += 1
		
	Global.minTextLength += 1
		
	print(Global.currentRound)
		
	print(Global.minTextLength, " minTextlength")
	
	print("yes")
		
	Global.generateRandomLetter()
		
	line_edit.text = ""
		

	

Global Script:

extends Node

var letter : String
#var upperCaseletter : String

var currentNumberLetter = 0
var characters : String =  "abcdefghijklmnopqrstuvwxyz"
# var upperCasecharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var lineEditText : String

var currentRound = 0

var minTextLength = 1

func _ready():
	generateRandomLetter()

func generateRandomLetter():
	var rng = RandomNumberGenerator.new()
	var randomNumber = rng.randi() % 26  # Generating a random number between 0 and 26

	var letter = characters[randomNumber]
	var upperCaseLetter = letter.to_upper()
	var lowerCaseLetter = letter.to_lower()

	print(letter)

Is it really the falut of begins_with ?
In Global, var letter in the first line and var letter in generateRandomLetter() are different variables because you put var on the latter.

1 Like

if something is not working, do a small test to verify your code. below i accidentally set v_test_string to “Herro” and I get false result.

so to make sure i did not make a mistake i print(v_test_string). then i will notice i set the string to “Herro” instead of “Hello”. alternatively you could v_string.begins_with(“Hello”)

extends Node2D
func _ready():
	var v_string : String = "Hello, world!"
	
	var v_test_string : String = "Herro"
	print(v_string.begins_with(v_test_string)) #false
	
	print(v_test_string)
	print(v_string.begins_with("Hello")) #true


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.