Godot:我设置一个变量,但它充当指针
var t = db.query_result
然后我在 for 中使用它与多个其他查询一起更改 t 的值,但我想将其设置为第一个查询的值?如何在 db.query_result 处设置 t 并使其在进行其他查询时不会更改?
func setTuto1():#
db.open_db()
db.query("select ID from currentTutorialMap where LandTextureType = 'Stone0';")
var t = db.query_result
print(t)
print(t.size())
print(t[1]["ID"])
for i in range(0, t.size()):
var x = rng.randi_range(0, 6)
if x==0:
db.query("update currentTutorialMap set LandTextureIndex = 10 where ID == %d;" % t[i]["ID"])
elif (x==1 or x==2):
db.query("update currentTutorialMap set LandTextureIndex = 9 where ID == %d;" % t[i]["ID"])
elif (x==3 or x==4):
db.query("update currentTutorialMap set LandTextureIndex = 11 where ID == %d;" % t[i]["ID"])
elif (x==5 or x==6):
db.query("update currentTutorialMap set LandTextureIndex = 12 where ID == %d;" % t[i]["ID"])
var t = db.query_result
Then I use this in a for with multiple other queries which change the value of t, but I want to set it at the value of my first query? How do I set t at db.query_result and make it not change afterwards when I make other queries?
func setTuto1():#
db.open_db()
db.query("select ID from currentTutorialMap where LandTextureType = 'Stone0';")
var t = db.query_result
print(t)
print(t.size())
print(t[1]["ID"])
for i in range(0, t.size()):
var x = rng.randi_range(0, 6)
if x==0:
db.query("update currentTutorialMap set LandTextureIndex = 10 where ID == %d;" % t[i]["ID"])
elif (x==1 or x==2):
db.query("update currentTutorialMap set LandTextureIndex = 9 where ID == %d;" % t[i]["ID"])
elif (x==3 or x==4):
db.query("update currentTutorialMap set LandTextureIndex = 11 where ID == %d;" % t[i]["ID"])
elif (x==5 or x==6):
db.query("update currentTutorialMap set LandTextureIndex = 12 where ID == %d;" % t[i]["ID"])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变量
t
是您的方法 (func
) 的本地变量。如果您希望它保持其值,请将其设为一个字段(在任何func
之外声明它,指南建议在脚本顶部执行此操作)。如果您只想要第一个结果,而不是覆盖它,您可以使用一个
bool
变量,默认设置为false
。当您写入t
时,您将其设置为true
。这允许您检查是否已经设置t
。像这样:
或者你可以使用 null。尽管仔细检查 db.query_result 是否可以为 null。
像这样的事情:
顺便说一下,使用准备好的语句。
The variable
t
is local to your method (func
). If you want it to keep its value, make it a field (declare it outside anyfunc
, the guidelines suggest to do that at the top of the script).And if you just want the first result, and not overwrite it, you can have a
bool
variable, set tofalse
by default. When you writet
, you set it totrue
. That allows you to check if you have already sett
.Something like this:
Alternatively you could use null. Although double check if
db.query_result
can be null.Something like this:
And, by the way, use prepared statements.