UE4SS-RE / RE-UE4SS

Injectable LUA scripting system, SDK generator, live property editor and other dumping utilities for UE4/5 games
http://docs.ue4ss.com/
MIT License
1.37k stars 187 forks source link

[Suggestion] Implement 'on_hot_reload' function for mods #602

Open chudders1231 opened 4 months ago

chudders1231 commented 4 months ago

for debugging purposes it would be nice to have an event that we could hook into to execute initialisation for example that would otherwise be called in ServerAcknowledgePossession which is my preferred initialisation event.

UE4SS commented 4 months ago

Anything executed at file scope of main.lua (i.e. anything not in a function) is executed immediately upon the loading or reloading of a mod. You didn't give a lot of information but hopefully this answer was helpful, otherwise provide more information about what you want.

chudders1231 commented 4 months ago

yeah so i guess this is only important in a dev environment.

for example in my init function i will cache a load of info, and say i want to get the local player character but that info is not available on the main menu.

so if i was to call the init outside of ServerAcknowledgePossession, that will throw errors in the main menu right and means that i would need to remember to remove those sorts of functions when publishing. But if there was an event that is fired when hot reloading, the transition from dev to production would be really nice.

it is obviously nothing important but would be a nice QoL feature for me personally.

inkydragon commented 2 months ago

A workaround: maybe you need ModRef:SetSharedVariable/GetSharedVariable

Warning: These variables do not get reset when hot-reloading.

—— https://docs.ue4ss.com/lua-api/classes/mod.html?highlight=ModRef#getsharedvariablestring-variablename

local function AfterInitGameStateHook()
    -- TODO
end

-- The game has been initialised
local ModGameStart = ModRef:GetSharedVariable("ModGameStart")
if ModGameStart and type(ModGameStart) == "boolean" then
    print("Mod hot-reloading.\n")

    AfterInitGameStateHook()
else
    print("First Start Game.\n")

    -- Called after AGameModeBase::InitGameState
    RegisterInitGameStatePostHook(function(GameState)
        if ModRef:GetSharedVariable("ModGameStart") then
            return
        end

        ModRef:SetSharedVariable("ModGameStart", true)
        print("AfterInitGameStateHook\n")
        AfterInitGameStateHook()
    end)
end