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

Can Blueprint Modloading and Lua Api work together? #455

Closed
Cui-xf opened this issue Apr 6, 2024 · 2 comments
Closed

Can Blueprint Modloading and Lua Api work together? #455

Cui-xf opened this issue Apr 6, 2024 · 2 comments

Comments

@Cui-xf
Copy link

Cui-xf commented Apr 6, 2024

Hello, I am a newbie.
Now I want to make a BP module for a game. I load some resources through BPModLoaderMod. then, I want to hook some game functions in the BP module to add functionality to the loaded resources. Is it possible to call Lua APIs or similar methods in the BP module

thanks

@UE4SS
Copy link
Collaborator

UE4SS commented Apr 6, 2024

There's no way to directly hook any functions from a BP mod right now but there's an open PR (#421) so in the future you'll hopefully be able to do that out of the box.
For now, you'll have to use the RegisterCustomEvent Lua function to bypass the restrictions of BP.

For example:

-- Assuming a custom event named 'FetchNumberFromLua' exists in your BP mod,
-- with an Out param as the only param.
RegisterCustomEvent("FetchNumberFromLua", function(ContextParam, OutParam)
    -- The Out param for the BP function will be set to the number 4.
    OutParam:set(4)
end)

This can be used to register hooks through Lua as well, just call RegisterHook or any other hook registration function in the Lua callback.

If you have a bunch of data that must be passed between Lua and BPs, then you'll need to create another custom event that transfers that data to the BP like shown above.

If you don't have any data that you want to pass, but would rather handle it all in BP, you could create a new BP function in your BP mod and register the hook in Lua and inside the hook call your BP function.

@UE4SS
Copy link
Collaborator

UE4SS commented Apr 6, 2024

Here's some untested example code for letting your BP mod handle the hook callback, there might be errors:

-- Reference to the BP mod actor.
local ModActor = nil

-- Registering to be notified whenever an instance of your ModActor is created.
-- The alternative to this is to iterate every single game object until you find your mod actor.
-- That is very bad for performance and should generally be avoided.
-- Note that the full name of your mod actor will be different.
-- It won't be Z_ExampleMod_P unless that's what you actually named your mod.
NotifyOnNewObject("/Game/Mods/Z_ExampleMod_P/ModActor.ModActor_C", function(Context)
    ModActor = Context
end)

-- Here's where we hook a BP function and redirect the callback to your own BP mod function.
-- Make sure this function actually exists when your Lua mod is started.
-- If it doesn't, you'll have to use NotifyOnNewObject on the object that the function belongs to.
-- You'll also need to keep track of whether the function is already hooked, and unhook if it is.
-- Otherwise you can get multiple callbacks because you have multiple hooks registered.
-- In this case, I know for sure that 'Character:CanJumpInternal' is available immediately on startup.
-- So I don't bother doing any of what I just explained.
RegisterHook("/Script/Engine.Character:CanJumpInternal", function()
    -- Making sure that we have a ModActor to call the function on
    if not ModActor or not ModActor:IsValid() then return end

    -- Assuming your ModActor has an 'OnJump' function with no params.
    ModActor:OnJump()
end)

@UE4SS UE4SS closed this as completed Apr 11, 2024
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

No branches or pull requests

2 participants