Topic was automatically imported from the old Question2Answer platform.
Asked By
RetroDan007
Several days ago I want to make a program that can do this.
Is it possible to do with FOR LOOP?
extends Node
var sumador = 0
var value
var value_str
func sum():
value_str = $LineEdit.get_text()
if value_str.is_valid_integer():
value = int(value_str)
for i in range(3):
sumador = sumador + value
return
else:
print("Error")
pass
I call the function from a button.
And in doing so I think it restarts everything
How could I do it? I need help. If someone can help me and send me the resolved project so that I can analyze it, I would appreciate it very much.
Right now your function multiplies one LineEdits entry by four and keeps the result for itself.
First You need to implement a way to get 3 values from LineEdit. For example introduce variable that is array like
var textvalues = []
and when user presses some Input that accepts value in LineEdit :
if $LineEdit.text.is_valid_integer() :
textvalues.append($LineEdit.text)
You can get a sum of every element of array in FOR loop:
func sum():
var sum = 0
for entry in textvalues :
sum += entry
return sum
But You will need to implement methods to keep array within max 3 elements. SO You will have to think of it - if You want to get a sum of first 3 elements that player enters in LineEdit and then reset, or You want to replace texts of array when Player enters more than 3 arguments and so on.