Skip to content

How do we avoid falling into line by line translation from GDScript to C#? #6622

@paulloz

Description

@paulloz

Hey 🙂

I was reading through the recent updates in the documentation and stumbled on a C# example that IMHO warrants a bit of discussion. I noticed it due to the recent PR on the CharacterBody2D page (#6614), but the part that bugs me was already there in the 3.x documentation.

So, on this page, we find the following snippet of code.

public override void _PhysicsProcess(double delta)
{
    var collision = MoveAndCollide(Velocity * (float)delta);
    if (collision != null)
    {
        Velocity = Velocity.Bounce(collision.GetNormal());
        if (collision.GetCollider().HasMethod("Hit"))
        {
            collision.GetCollider().Call("Hit");
        }
    }
}

It is (as almost always) a direct translation of the GDScript example. More often than not, converting examples this way is fine. However, here, we are taking a very "duck typing-y" approach of what was returned by collision.GetCollider()... And I'd argue presenting this as a good approach in C# could be harmful (I lack a better word here) to the reader. In this particular instance, I guess most people would write this using an interface. But we don't really elaborate on what the object containing that Hit() method would be on this page, so I don't know if there's a point.

Anyway, I'm of course not saying we need to tailor every single C# code block completely differently than their GDScript counterpart. But I was curious of other people's opinion about this, if we should try to "correct" this kind of thing when we see them or if I'm just entirely delusional.

Edit: this was tagged automatically by GH, but it should probably be "discussion" instead of "enhancement"?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions