在我的 2D 游戏中,玩家能够摧毁箱子、具有两种碰撞形状的物体。当被摧毁时,板条箱会产生也具有碰撞形状的物品。但是当调用以下函数时,Godot控制台中会显示许多类似的错误
代码:
func _on_Crate_item_dropped(collectible, pos):
collectible.init(pos, Vector2(rand_range(30, 100), rand_range(-10, 10)))
$CollectibleContainer.add_child(collectible) # error occurs here
Run Code Online (Sandbox Code Playgroud)
错误:
ERROR: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.
Run Code Online (Sandbox Code Playgroud) 以下示例是我在应用程序中尝试执行的操作的简化版本。
fun main() {
val test: MutableMap<Int, String> = mutableMapOf(
1 to "Apple",
)
test[2] = test[1] // test[1] has incorrect type.
}
Run Code Online (Sandbox Code Playgroud)
该代码无法编译。IntelliJ 给出了以下提示:
Type mismatch.
Required: TypeVariable(V)
Found: String?
Run Code Online (Sandbox Code Playgroud)
我不明白 TypeVariable 是什么。但是当我提供默认值时错误消失
test[2] = test[1] ?: "Grape"
Run Code Online (Sandbox Code Playgroud)
为什么所需的类型是TypeVariable(V),String而不是 ,它到底是什么?如果我的应用程序没有默认值该怎么办?
我想知道如何创建一个脚本,其中包含我想在多个脚本中使用的方法。我不认为我想为它创建一个全局单例,因为我没有存储任何将在多个场景中保存的全局数据。我收集了一些有用的功能,别无他物。
我的目标是能够将布尔表达式表示为字符串,例如"True or False is True". 为了使它成为可能,我首先做了一些布尔谓词:
and' :: Bool -> Bool -> Bool
and' p q = p && q
or' :: Bool -> Bool -> Bool
or' p q = p || q
-- ... same for nor, nand, xor, imply and equivalent
equ' :: Bool -> Bool -> Bool
equ' p q = p == q
Run Code Online (Sandbox Code Playgroud)
之后,我决定制作一个将函数映射到字符串的函数。我依赖于 Haskell 的模式匹配功能,但我的技巧没有奏效。
-- representation function, a.k. "show" for functions
repr :: (Bool -> Bool -> Bool) -> [Char]
repr …Run Code Online (Sandbox Code Playgroud) 在 Java、C 或 Python 等许多命令式编程语言中,我们可以轻松添加打印函数,该函数可以为我们提供有关程序中间状态的信息。
我的目标是找到一种在 Haskell 中做类似事情的方法。我想要一个不仅计算值而且打印一些东西的函数。下面的函数是我想要做的简化版本。我的实际功能太复杂和不全面,没有上下文,无法在这里展示。
我的想法是有一个“纯”的 Haskell 函数,它有一个辅助函数,里面有[Int] -> IO () -> Int类型签名。IO 参数在where子句中初始化为一个do块。但不幸的do是,当我在 GHCI 中运行该函数时,该块没有被执行。虽然该函数编译成功
module Tests where
-- Function returns the sum of the list and tries to print some info
-- but no IO actually happens
pureFuncWithIO :: [Int] -> Int
pureFuncWithIO [] = 0
pureFuncWithIO nums = auxIOfunc nums (return ())
where
auxIOfunc [] _ = 0
auxIOfunc (n : ns) _ = …Run Code Online (Sandbox Code Playgroud)