Skip to content
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

Variables with wrong types become 0 instead of being cast correctly #907

Closed
kb173 opened this issue Nov 2, 2022 · 3 comments · Fixed by godotengine/godot#75758
Closed
Labels
bug This has been identified as a bug confirmed topic:gdextension This relates to the new Godot 4 extension implementation
Milestone

Comments

@kb173
Copy link

kb173 commented Nov 2, 2022

Say I have the following function in a GDExtension project:

void Example::print_variables(double double_val, int int_val) {
	UtilityFunctions::print(double_val, " and ", int_val);
}

As expected, calling example.print_variables(123.0, 321) from GDScript prints "123 and 321" to the console.

However, when the types do not match (e.g. mixing up float and int), they are not properly cast automatically: the debugger does print a warning like Narrowing conversion (float is converted to int and loses precision), but the output is not still "123 and 321" as I would expect. Rather, the variable with the false type is simply 0.

Example: calling example.print_variables(123, 321) (first argument is now int rather than float) produces "0 and 321" in the console.

This type of bug is really hard to track down in a reasonably complex project, so I would expect either a full error if the types don't match, or a better automatic conversion.

Here's a minimal reproduction (just a quick modification of the example project from this repo): type-cast-repro.zip running it in Godot shows that, as described above, parameters with wrong types simply become 0. This is the case even if Godot's optional static typing is not used (= rather than :=).

Tested in Godot 4.0 Beta 3

@Skwint
Copy link

Skwint commented Nov 13, 2022

I just ran into the exact same thing. Passing const radius = 10 to a C++ set_radius(real_t p_radius); and it gets a value of 0 with no warnings or errors. It should definitely cast it. GDscript does allow you to strongly type a variable but I don't think anyone expects to have to!

@Calinou Calinou added bug This has been identified as a bug confirmed topic:gdextension This relates to the new Godot 4 extension implementation labels Nov 13, 2022
@DmitriySalnikov
Copy link
Contributor

In fact, values are not always converted to 0.
If you convert floating point numbers that are less than 0, then you get nan.
But if the native function accept, for example, Vector2i, and GDScript pass Vector2(-50, 50) as an argument, then the native function will get Vector2i(-1035468800, 1112014848).

@dsnopek
Copy link
Collaborator

dsnopek commented May 5, 2023

This sounds like it may be a duplicate of godotengine/godot#75757

Can you test if PR godotengine/godot#75758 fixes it for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This has been identified as a bug confirmed topic:gdextension This relates to the new Godot 4 extension implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants