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

Fixed keybinds created from C++ mods executing multiple times #446

Merged
merged 1 commit into from
Apr 21, 2024

Conversation

UE4SS
Copy link
Collaborator

@UE4SS UE4SS commented Mar 28, 2024

Description

This only happened if the hot-reload feature was used to reload all mods.

This was fixed by introducing CppUserModBase::register_keydown_event.
It's identical to the one in UE4SSProgram, except it keeps track of which C++ mod registered the event so that it can be "unregistered" when the mod is reloaded.

Mods that continue to use the UE4SSProgram version will continue to have the same problem.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

I created a C++ mod that registered a keybind with the new function that output something to the console, and verified that only one message in the console appeared after using the bind after spamming the "Restart All Mods" button.

Checklist

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • I have added the necessary description of this PR to the changelog, and I have followed the same format as other entries.
  • Any dependent changes have been merged and published in downstream modules.

This only happened if the hot-reload feature was used to reload all mods.

This was fixed by introducing CppUserModBase::register_keydown_event.
It's identical to the one in UE4SSProgram, except it keeps track of which C++ mod registered the event so that it can be "unregistered" when the mod is reloaded.

Mods that continue to use the UE4SSProgram version will continue to have the same problem.

Updated changelog
@Buckminsterfullerene02
Copy link
Member

Buckminsterfullerene02 commented Apr 10, 2024

Can you send the code responsible for registering the keybind? I'm trying to replicate but am having troubles with it due to getting "failed to load dll" due to that part of the code being erreonous somehow (removing that code causes the mod.dll to load fine). Admittedly I have never written ue4ss c++ mod code before as xmake branch is the first time I can actually compile ue4ss since c++ api was added.

I figured the following code would work inside of the mod's constructor:

        const Input::EventCallbackCallable callback = [&] {
            Output::send<LogLevel::Verbose>(STR("P key was pressed\n"));
        };

        register_keydown_event(Input::P, callback, 2); // I assume custom_data is 2 because it means the bind came from c++

@UE4SS
Copy link
Collaborator Author

UE4SS commented Apr 10, 2024

Can you send the code responsible for registering the keybind? I'm trying to replicate but am having troubles with it due to getting "failed to load dll" due to that part of the code being erreonous somehow (removing that code causes the mod.dll to load fine). Admittedly I have never written ue4ss c++ mod code before as xmake branch is the first time I can actually compile ue4ss since c++ api was added.

I figured the following code would work inside of the mod's constructor:

        const Input::EventCallbackCallable callback = [&] {
            Output::send<LogLevel::Verbose>(STR("P key was pressed\n"));
        };

        register_keydown_event(Input::P, callback, 2); // I assume custom_data is 2 because it means the bind came from c++

This code is functioning, but wrong in the assumption that the last param should be 2.
The last param isn't needed and actually means nothing in this context.
The "2" that designates the bind as coming from a C++ mod is done automatically in the CppUserModBase wrapper for register_keydown_event that you've called in your code.

As far as your problem is concerned: Are you sure that you moved both UE4SS.dll and your mod dll after building ?
I tried your code and it works fine for me.

EDIT: For the record, this is my event registration code:

register_keydown_event(Input::Key::Q, {Input::ModifierKey::CONTROL}, [&]() {
    Output::send<LogLevel::Warning>(STR("test\n"));
});

@Buckminsterfullerene02
Copy link
Member

Can you send the code responsible for registering the keybind? I'm trying to replicate but am having troubles with it due to getting "failed to load dll" due to that part of the code being erreonous somehow (removing that code causes the mod.dll to load fine). Admittedly I have never written ue4ss c++ mod code before as xmake branch is the first time I can actually compile ue4ss since c++ api was added.
I figured the following code would work inside of the mod's constructor:

        const Input::EventCallbackCallable callback = [&] {
            Output::send<LogLevel::Verbose>(STR("P key was pressed\n"));
        };

        register_keydown_event(Input::P, callback, 2); // I assume custom_data is 2 because it means the bind came from c++

This code is functioning, but wrong in the assumption that the last param should be 2. The last param isn't needed and actually means nothing in this context. The "2" that designates the bind as coming from a C++ mod is done automatically in the CppUserModBase wrapper for register_keydown_event that you've called in your code.

As far as your problem is concerned: Are you sure that you moved both UE4SS.dll and your mod dll after building ? I tried your code and it works fine for me.

Ahhh yeah I was forgetting to replace the UE4SS.dll as well! Apologies! Thanks for the explanation though, I appreciate it. I can replicate your fix, so I think this is fine.

btw, I searched at least 30 ue4ss cpp mods for this function (which already existed under the same name but only through UE4SSProgram::) using this search link but I didn't find a single person using this function. I'd imagine people would actually start using it now that it's in the mod base but I'm sure there are probably a bunch more things not being used, which goes to show how much we need proper c++ api docs generation of some sort. Maybe I'll look into it when I'm less busy irl.

@Buckminsterfullerene02 Buckminsterfullerene02 merged commit 658e17e into main Apr 21, 2024
@Buckminsterfullerene02 Buckminsterfullerene02 deleted the cpp-mod-keybind-dupe-exec-fix branch April 21, 2024 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants