Question;
Extremely new to coding. I made a calculator for a different game I am playing. I have the math behind the UI working, but I cannot get LineEdit to give me an int.
I have tried .to_int() only to get
Invalid call. Nonexistent function ‘to_int’ in base ‘LineEdit’
I have tried get_node(“path”).text only to get
Invalid access to property or key “text” on base object of type “null instance”
I have tried
var aaanum = int(aaa) which may work, but I cannot “hook” it to the LineEdit when get_node isn’t working for me.
I basically need the user to enter numbers into the field–and have those numbers put into the calculator variables to spit out the totals at the bottom.
I feel foolish because there must be something something I am missing hard.
i think so, the print never spits out letters so valtex by default is an int so a letter never causes an issue.
but now I have this new issue
invalid access to property or key “text_changed” on a base object of type null instance
I cannot believe the difficulty I am having… it is to take a number and use it in a formula. Its so basic a concept that the tutorials are far and few between. Yet I clearly cannot grasp how…
Three different sets of numbers, math in the backend. It’s maddening.
“invalid access to _____ on base object of type null instance” basically means something isn’t hooked up properly. The instance should be pointing at a thing, and instead it’s pointing at nothing.
func crash():
var foo: LineEdit # uninitialized...
print(foo.text) # foo isn't pointing at anything, so foo.text isn't valid
If you’re having problems with null instance stuff it implies the thing you’re checking isn’t pointing at what you think it is.
You can probably simplify what you’re doing a bit; it looks like what you’re building is a pen & paper RPG calculator program? If so, it’s pretty much event based; it only needs to do anything when the player takes an action.
You’ve got an _on_LineEdit_text_entered() function. I’d suggest changing the name of that to something more descriptive, like _str_text_entered() or whatever; you’ll presumably want one of those per thing you’re reading.
Wheras you can .connect() your function, I’d suggest instead hooking it up via the editor. On the right side of the editor, there’s a “Node” tab, and in that are a list of signals. If you select your node in the scene panel (left side of the window), right-click on one of the signals in the node panel (right side of the window), it will let you hook a function to the signal for that node.
I have been trying for hours and I cannot get text in TextEdit fields to become integers. It just throws so many errors about how its a string and I can’t get the value.
Sidenote, one issue I was having was a for loop freeze which has been an issue since 2019 it seems? So I made a 500 space array instead of looping through the numbers.
I’m still trying to get inputs to index my arrays to multiply by percentages and the power multipliers, scrolls, themselves.
Its crazy too because print has proven my math is right and displays it soooo easy. Yet labels and lineedit are proving me in
extends Control
@onready var str_input = $StrInput
@onready var dex_input = $DexInput
@onready var result_label = $ResultLabel
# Called when the node enters the scene tree
func _ready():
# You can connect signals via code if you prefer
str_input.text_entered.connect(_on_str_text_entered)
dex_input.text_entered.connect(_on_dex_text_entered)
# Initial calculation with default values
calculate_result()
# Handle STR input
func _on_str_text_entered(new_text):
if new_text.is_valid_int():
calculate_result()
else:
str_input.text = "0" # Reset to default
print("Please enter a valid number for STR")
# Handle DEX input
func _on_dex_text_entered(new_text):
if new_text.is_valid_int():
calculate_result()
else:
dex_input.text = "0" # Reset to default
print("Please enter a valid number for DEX")
# Calculate and display the result
func calculate_result():
var str_value = 0
var dex_value = 0
# Safe conversion
if str_input.text.is_valid_int():
str_value = int(str_input.text)
if dex_input.text.is_valid_int():
dex_value = int(dex_input.text)
# Your formula here
var result = str_value * 2 + dex_value * 1.5
# Update the result label
result_label.text = str(result)
LineEdit does not have a signal called text_entered, it’s called text_changed (or text_submitted when you are only interested when the change is submitted).