0

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")
srt111
  • 1,079
  • 11
  • 20
  • Welcome to Stackoverflow! Where and to which other function is your variable `c` passed? In `_on_LineEdit_text_entered` `c` is not used. – Maximilian Peters Jan 02 '19 at 14:40
  • @Maximilian Peters -var c (sum)is passed from _on_Button_pressed() to func_on_LineEdit_text_entered() for comparision with user_input var f(var d converted to float) under #Compare sum with user_input ,The comparison is commented out () as on running the script it will result in an error and not display the values of the variables involved. – srt111 Jan 03 '19 at 15:11

1 Answers1

1

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!

eltee
  • 196
  • 1
  • 6