代码不响应属性中的更改



所以我aNum作为补丁自己的属性,在我的代码中我有 ask patch 0 -5 [set aNum (aNum - 1)]但补丁只是将其 aNum 更改为 -1。

我如何确保它采用之前分配的 aNum 并从中减去 1?

谢谢!

它能满足你的期望。 所以问题是,为什么你看不到你所期望的。 找到答案称为"调试"。 执行调试的一种简单方法是使用 print 语句来帮助您检查程序的逻辑。 (另一种方法是使用 error 标记意外的程序状态。 例如

globals [testPatch]
patches-own [aNum]
to decrementANum [#patch]
  ask #patch [set aNum (aNum - 1)]
end
to test
  set testPatch patch 0 -5
  repeat 5 [
    let _before [aNum] of testPatch
    decrementANum testPatch
    print (word "before: " _before "; after: " [aNum] of testPatch)
    ]
end

一般来说,编写这样一个最小的自包含示例会提出更好的问题。 最有可能的是,您会在尝试生成示例时发现错误。

最新更新