-
Notifications
You must be signed in to change notification settings - Fork 212
Possible to have methods with default values? #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Rust itself does not have default method parameters, but I think it's possible to define those with NativeScript 1.1, I am not completely sure about this though. These bindings do not yet use NativeScript 1.1 so adding this would probably take some effort. |
NativeScript 1.1 did add If you're fine with writing explicit You may also have your method take a Maybe it'd be possible to implement a derive macro for
#[derive(ToVariant)]
struct MyMethodsArgs {
foo: Foo,
bar: Option<Bar>,
baz: Option<Baz>,
}
#[methods]
impl MyClass {
#[export]
fn my_method(&self, args: MyMethodArgs) -> Result<i64, ()> {
let foo = args.foo;
let bar = args.bar.unwrap_or_else(|| Bar::default());
let baz = args.bar.unwrap_or_else(|| Baz::default());
// ...
Ok(42)
}
} ...and in GDScript: var answer = my_instance.my_method({
"foo": foo,
"baz": baz,
})
# externally tagged dictionaries should work well with GDScript's match
match answer:
{"Ok": var answer}:
print(answer)
{"Err"}:
print("oops")
_:
print("the call itself failed") @karroffel What do you think about this? Are there any pitfalls I might not be aware of? I think one problem with I just realized that fn from_variant(v: &Variant) -> Option<Option<T>> { Some(T::from_variant(v)) } |
I know that Rust itself doesn't support default parameters. I just
wondered if I might expose a method with default parameters to GDScript.
Right now I have:
```
#[export]
fn speak(&mut self, _owner: gdnative::Node, message: GodotString) {
...
```
Something like this would be nice:
```
#[export]
fn speak(&mut self, _owner: gdnative::Node, message: GodotString,
interrupt: Option<bool>) {
```
Or some `enum` that would let me specify a type with a default value,
like `GodotStringWithDefault("foo")`. Disclaimer: haven't had coffee
yet, so please don't take that syntax as definitive. :)
Thanks.
|
Now having had said coffee and rereading this, it looks like my only
option is to pass `null` for my optional/default arguments in GDScript,
then accept an `Option<Variant>` in my method. Is that accurate?
I'm fairly new to GDScript, but what I essentially want is an optional
argument. I don't care about the implementation details overly much
(I.e. whether what I want is a default value, or something like `Option`
that is either `Some(_)` or `None` and which I need to unwrap.) Not sure
if that makes a difference. I can pass nulls in for optional parameters
for now, but if that can go away at some point, that'd be nice.
Thanks again.
|
For now you need to pass |
Got it, thanks, I'll do that for now. I'd like to leave this issue open
and/or update the title so we can support optional/default values if
GDNative does indeed allow it. Maybe `Option<_>` parameters on exported
methods might be wrapped by whatever mechanism
`|godot_nativescript_set_method_argument_information|` uses to mark a
parameter as optional, if indeed it supports optional parameters and not
default values.
Anyhow, thanks again for the help!
|
Hmm, here's what I'm doing now and it isn't working:
```
#[export]
fn speak(&mut self, _owner: gdnative::Node, message: GodotString,
interrupt: Variant) {
let message = message.to_string();
println!("{:?}", interrupt);
let interrupt = interrupt.try_to_bool().unwrap_or(true);
println!("{}: {}", message, interrupt);
...
```
Here `interrupt` is always `true` even if I explicitly call it like so:
```
if node.hint_tooltip:
tts.speak(node.hint_tooltip, false)
```
The `println!` for the variant always returns `Nil(Null)` so clearly I'm
getting something wrong. For now I'm giving up on default values and
just requiring the parameter, since an explicit parameter is better than
an explicit null IMO.
Thanks again.
|
Reading |
Is it possible to either assign a method parameter a default value, or to check whether it is nil/not provided and assign it one from within my Rust function? I've got this working for methods with specified parameters, but now I'd like to make others optional and assign sensible defaults.
Thanks.
The text was updated successfully, but these errors were encountered: