未传递全局变量数据[Godot.2.15]

时间:2019-01-02 11:19:06

标签: godot gdscript

我在Godot中创建了一个“附加心理数学计算器”,向用户显示了一个有关单击“ on_Button_pressed”按钮的新问题,其总和存储在“ global var c”中,然后通过“ Lineedit”访问输入然后存储在“ var d”中。但是,当传递“ var c”与“ Lineedit”中的“ var d”进行比较时,它将传递“ Null”值。如何在两个函数之间传递值。

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")

1 个答案:

答案 0 :(得分:1)

尝试替换行

var c = a + b

使用

c = a + b

变量var声明一个新的变量,该变量在函数的作用域内起作用。 var关键字应位于代码的最外层声明中。

希望有帮助!