Godot:我设置一个变量,但它充当指针

发布于 2025-01-11 23:15:17 字数 883 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2025-01-18 23:15:17

变量 t 是您的方法 (func) 的本地变量。如果您希望它保持其值,请将其设为一个字段(在任何 func 之外声明它,指南建议在脚本顶部执行此操作)。

如果您只想要第一个结果,而不是覆盖它,您可以使用一个 bool 变量,默认设置为 false。当您写入t时,您将其设置为true。这允许您检查是否已经设置t

像这样:

var _is_t_set := false
var _t

func setTuto1():
    if _is_t_set:
        # we have been here before
        # do whatever you do in this case
        pass
    else:
        # first time we are here
        db.open_db()
        db.query("blah blah blah")
        _t = db.query_result
        _is_t_set = true # so we do not enter here again

或者你可以使用 null。尽管仔细检查 db.query_result 是否可以为 null。

像这样的事情:

var _t = null

func setTuto1():
    if _t != null:
        # we have been here before
        # do whatever you do in this case
        pass
    else:
        # first time we are here
        db.open_db()
        db.query("blah blah blah")
        _t = db.query_result

顺便说一下,使用准备好的语句

The variable t is local to your method (func). If you want it to keep its value, make it a field (declare it outside any func, 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 to false by default. When you write t, you set it to true. That allows you to check if you have already set t.

Something like this:

var _is_t_set := false
var _t

func setTuto1():
    if _is_t_set:
        # we have been here before
        # do whatever you do in this case
        pass
    else:
        # first time we are here
        db.open_db()
        db.query("blah blah blah")
        _t = db.query_result
        _is_t_set = true # so we do not enter here again

Alternatively you could use null. Although double check if db.query_result can be null.

Something like this:

var _t = null

func setTuto1():
    if _t != null:
        # we have been here before
        # do whatever you do in this case
        pass
    else:
        # first time we are here
        db.open_db()
        db.query("blah blah blah")
        _t = db.query_result

And, by the way, use prepared statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文