0 votes

I got into Godot engine yesterday and I'm now playing around with movement.

The problem is that as you insert more action keys via Inputmap the current way I've seen to handle this is:

if Input.is_action_pressed("ui_down")
...
if Input.is_action_pressed("ui_up")
...
if Input.is_action_pressed("ui_right")
...
if Input.is_action_pressed("ui_left")
...
if Input.is_action_pressed("ui_accept")
...

I've tried to use an alternative to this, first with dictionaries but so far hasn't found any good solution due to is_action_pressedreturning a bool and requiring a string.

in Engine by (12 points)
edited by

1 Answer

0 votes

Use an override function

Heres an example in C#

This function was to be incredibly fast using the type caster. Your input right now is meta however to detect many keyboard presses would possibly require the use of an override instead of isactionpressed. It would be incredibly messy to have an If statement check every single one. Instead we can just have a List to check each one.

public override void _UnhandledKeyInput(InputEventKey @event)
{
    if(@event is InputEventKey KeyValue)
    {
        _EscapeKey(@event);
    }
}
private void _EscapeKey(InputEventKey KeyValue)
{
    if(KeyValue.Scancode == (int)KeyList.Escape)
    {
        GetTree().Quit();
    }
}
by (214 points)

I see, but from what I gather you can't do override functions in gdscript like you can in C#.

Right now, your code is meta (most efficient tactic available with only GDScript). The problem is with GDScript you can't use an override, you are indeed right. However, as the Document says (if you actually read them) C# is 3x faster than GDScript.

GDScript (Release)  13820
C#/Mono 48680
GDNative (Nim)  52780
GDNative (D)    57360
GDNative (C++)  58120

The above is a benchmark on the amount of Sprites loaded and called using a function by spawning in objects and setting their texture to the files recursively. C# is 2nd to best, While GDScript is insufficient. Please be aware than using both Languages (as many professional programmers use more than 1) is not a downside. In a Computer Science class it fails the basic algorithm design in the designing process because we're striving to have most efficient and quickest algorithm available. So take the time to learn both of these languages it will be beneficial in the future!
Don't be a one-trick.

Thanks I'm well aware of C# being much faster than GDScript, technically C++ is the fastest for Godot :).

The question then becomes how do I use both C# and C++ in that case?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories