I’m trying to port my code that adds a new InputEventKey to a specified InputAction to C#
func add_inputeventkey(action:String, keycode:int)->void:
for event in InputMap.action_get_events(action):
if event is InputEventKey and event.get_physical_keycode_with_modifiers() == keycode:
print("Key assignment redundancy: tried to attach physical keycode "+str(keycode)+" to "+action+", to which it was already assigned")
return
var newinput = InputEventKey.new()
InputMap.action_add_event(action,newinput)
newinput.set_physical_keycode(keycode)
public static void add_inputeventkey(string action, int keycode)
{
Godot.Collections.Array<InputEvent> actioninputs = InputMap.ActionGetEvents(action);
foreach (InputEvent input in actioninputs)
{
if (input is InputEventKey inputKey){if (inputKey.GetPhysicalKeycodeWithModifiers() == (Key)keycode)
{
GD.Print("Key assignment redundancy: tried to attach physical keycode "+GD.VarToStr(keycode)+" to "+GD.VarToStr(action)+", to which it was already assigned");
return;
}}
}
InputEventKey newinput;
InputMap.ActionAddEvent(action, newinput);
newinput.SetPhysicalKeycode((Key)keycode);
return;
}
}
Thanks but uh now I have a new problem?? The function only runs the first time it’s called, what’s up with that
(for context it’s part of an autoload that gets called a lot)
You are probably only ever calling it once. There’s nothing about the function itself that you shared that should make it behave differently on subsequent calls.
One of the nice things about C# is the really good debugging. I like to use Jetbrains Rider for this personally, but Visual Studio Code is also a popular option.
If you…
Set up your IDE like Rider or VS Code for your Godot project
Place a breakpoint at place(s) before the function gets called (In Rider you can do this by clicking on the line number, in VSC probably the same though I haven’t used it to be sure)
Run the program in debug mode through your IDE
…Then the program will pause there at any breakpoint that you set. It will let you inspect the current state of variables and all to see what there isn’t as you expect, and let you step through things one line at a time to find where things go wrong. (Or, if it doesn’t pause there, then this tells you that the code is never run at all, and your problem is in whatever other code you thought was going to make that code run in the first place.)
But you shouldn’t need a Godot specific tutorial - any help online about opening your C# project and using the debugger in whichever IDE you choose should be applicable.
public static void add_inputeventkey(string action, int keycode)
{
Godot.Collections.Array<InputEvent> actioninputs = InputMap.ActionGetEvents(action);
foreach (InputEvent input in actioninputs)
{
if (input is InputEventKey inputKey
&& inputKey.GetPhysicalKeycodeWithModifiers() == (Key)keycode)
{
GD.Print("Key assignment redundancy: tried to attach physical keycode "+GD.VarToStr(keycode)+" to "+GD.VarToStr(action)+", to which it was already assigned");
return;
}
}
InputEventKey newinput = new InputEventKey();
InputMap.ActionAddEvent(action, newinput);
newinput.SetPhysicalKeycode((Key)keycode);
}
(no the broken version isn’t here, even my original example got the {}s right)