Skip to content

Add C# integer casts where needed in Your first 2D game #10854

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

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Apr 10, 2025

PS: Is a cast to float needed here?

var multiplier = GD.RandRange(.8, 1.2);

@Calinou Calinou added enhancement topic:dotnet area:getting started Issues and PRs related to the Getting Started section of the documentation cherrypick:4.3 cherrypick:4.4 labels Apr 10, 2025
@raulsntos
Copy link
Member

These shouldn't be needed. The MinSpeed and MaxSpeed properties are defined as int in the tutorial from what I can see, so the int GD.RandRange(int, int) overload would be used and that already returns int.

As for var multiplier = GD.RandRange(.8, 1.2); I don't think it needs a cast either, since the multiplier variable is only used to add it to an untyped Godot Array.

@tetrapod00
Copy link
Contributor

Agree, these don't look needed. In fact if you add the cast, your editor might suggest removing them (I get an IDE0004 suggestion in VSCode).

The original docs comment says:

GD.RandRange() requires an explicit cast so it would be great to add [these casts]

But I think they may be extrapolating from the earlier cast from C# double to float, which is required because RotateY() takes a float argument:

RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

@MetalWhine
Copy link

MetalWhine commented Apr 11, 2025

Cheers mate, seems like I used the wrong override by making my MinSpeed & MaxSpeed properties as floats and extrapolated from the mentioned previous section of tutorial. My mistake!

@skyace65
Copy link
Contributor

Closing this per tetrapod and raulsntos' comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
archived area:getting started Issues and PRs related to the Getting Started section of the documentation enhancement topic:dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants