全局变量数据未通过[Godot.2.15]
Global variable data not passed [Godot.2.15]
我在 Godot 中创建了一个 "Addition mental math calculator",用户在点击 "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")
尝试替换行
var c = a + b
有
c = a + b
var c 正在声明一个在函数范围内工作的新 var。 var 关键字应该在代码的最外层声明。
希望对您有所帮助!
我在 Godot 中创建了一个 "Addition mental math calculator",用户在点击 "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")
尝试替换行
var c = a + b
有
c = a + b
var c 正在声明一个在函数范围内工作的新 var。 var 关键字应该在代码的最外层声明。
希望对您有所帮助!