Reputation: 1539
I was creating a "Addition mental math calculator" in Godot ,where the user is shown a new question on hitting "on_Button_pressed" button and the sum is stored in "global var c "and then the input is accessed via "Lineedit" and stored in "var d" .However ,when "var c" is passed for comparison with "var d" in "Lineedit",it passes a "Null" value .How could one pass values between two functions.
extends Panel
# global variables
var a = " "
var b = " "
var c = " "
var d = " "
func _ready():
pass
#Generate Random number and store sum in "var c"
func _on_Button_pressed():
randomize()
var a = floor (rand_range(1,100))
var b = floor (rand_range(1,100))
var c = a+b
print (a)
print (b)
print ("c = "+ str(c))
get_node("RichTextLabel").set_text(str(a)+"+"+str(b)+"=")
#User_input
func _on_LineEdit_text_entered( text ):
d = get_node("LineEdit").text
#pass sum "var c" for comparison with user_input "var d"
print(c)<-- NUll value being passed.
#Accesing value via node method
# var e = get_node("Panel").get("c").to_float()<-- Error-"Panel node doesn't exist"
# print(e)
#convert d to float
var f = d.to_float()
print(f)
#Compare sum with user_input
# if f == c:
# get_node("RichTextLabel").set_text("Right Answer")
# else:
# get_node("RichTextLabel").set_text("Wrong Answer")
Upvotes: 0
Views: 500
Reputation: 316
Try replacing the line
var c = a + b
With
c = a + b
The var c is declaring a new var working the scope of the function. The var keyword should be on the outermost declaration of the code.
Hope that helps!
Upvotes: 1