Godot 4.0: Callable
参考記事
公式
Callable を受け渡しして実行する
code:gd
func _ready() -> void:
print("_ready!")
exec_callable(hello)
func hello() -> void:
print("hello")
func exec_callable(f: Callable) -> void:
f.call()
hello 関数をCallable オブジェクトとして引数に渡し、f.call()で実行する。
ラムダ式にして渡すこともできる
code:gd
func _ready() -> void:
print("_ready!")
exec_callable(func():print("hello"))
func exec_callable(f: Callable) -> void:
f.call()
call と call_deferred について
bind の使い方
get_bound_arguments() , get_bound_arguments_count()
bind した引数の数と、実際の引数を取得できる
以下のコードを実行すると
code:gd
func exec_callable(f: Callable) -> void:
print(f.bind("test").get_bound_arguments_count())
print(f.bind("test").get_bound_arguments())
出力は
code:txt
1
get_method()
function名を取得する
lambda 式にやったらどうなる?
空文字になった
code:gd
func _ready() -> void:
exec_callable(func(x:String):print("hello: " + x))
func exec_callable(f: Callable) -> void:
print(f.get_method())
print(exec_callable.get_method())
出力結果は、
code:txt
exec_callable
(1行目はlabmda式の f.get_method() の出力結果で、空白)
is_custom(), is_null(), is_standard(), is_valid()
bool is_custom ( ) const
Returns true if this Callable is a custom callable. Custom callables are created from bind or unbind. In GDScript, lambda functions are also custom callables.
custom かどうかを返す。 custom は、bind or unbind で作られたか、lambda func で作られたもの
bool is_standard ( ) const
Returns true if this Callable is a standard callable. This method is the opposite of is_custom. Returns false if this callable is a lambda function.
standard は、custom の逆
bool is_null ( ) const
null かどうか
bool is_valid ( ) const
Returns true if the callable's object exists and has a valid method name assigned, or is a custom callable.
valid かどうかだが、null とどう違うのかがよく分からない。
存在しない関数名を string で指定して Callable を生成した場合は、null ではないが不正な状態なので valid = false で検査できると分かった。
色々試してみる
名前ありの関数と、labmda 関数を渡した時の違い。is_custom と is_standard の結果が反転しており、両方とも valid = true
code:gd
func _ready() -> void:
exec_callable(func(x:String):print("hello: " + x))
func exec_callable(f: Callable) -> void:
callable_state_check(f)
callable_state_check(_print_value)
func _print_value(value: String) -> void:
print(value)
func callable_state_check(f: Callable) -> void:
var method_name := f.get_method()
if method_name.is_empty():
method_name = "lambda"
if f.is_custom():
print("%s is custom" % method_name)
if f.is_null():
print("%s is null" % method_name)
if f.is_standard():
print("%s is standard" % method_name)
if f.is_valid():
print("%s is valid" % method_name)
出力結果
code:txt
lambda is custom
lambda is valid
_print_value is standard
_print_value is valid
引数無しで生成したCallableは、null となった。valid = false
code:gd
var null_callable = Callable()
callable_state_check(null_callable)
出力結果
code:txt
lambda is null
存在しない関数名を string で渡した生成した Callable は、null ではなく standard = true だが、valid = false となってくれた。valid の存在理由が分かった。
code:gd
var not_exist_callable = Callable(self, "not_exist_method")
callable_state_check(not_exist_callable)
出力結果
code:txt
not_exist_method is standard
rpc
void rpc ( ... ) vararg const
Perform an RPC (Remote Procedure Call). This is used for multiplayer and is normally not available, unless the function being called has been marked as RPC. Calling this method on unsupported functions will result in an error.
自分は今の所使う予定ないのでスルー
https://www.youtube.com/watch?v=6wGI4c5p4JI
Godot3.5までは文字列指定していたので typo による実行時エラーや未発火が起こり得たが、それが改善される