1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

[BNW] Efficient use of SaveUtilis?

Discussion in 'Civ5 - Creation & Customization' started by Serp, Mar 26, 2021.

  1. Serp

    Serp King

    Joined:
    Apr 1, 2015
    Messages:
    646
    Spoiler Context: :

    DLL - Various Mod Components

    @whoward69
    Thank you very much for the fast response (I created a new thread, because it would be too much offtopic in your thread)
    Yes, I already tried to add a GameEvent before all the occurrences of "gDLL->SaveGame(...)" and so on, but for whatever reason it is not triggered (only the autosave when the map was generated works).
    I have no experience regarding serialise/deserialise, so I guess this is no option for me.

    Regarding SaveUtilis: Do you have a link to the most efficient version of it you know?

    My current project:
    I have a mod that shares visbility of one team with another team (eg. when they are friends). I achieved this by adding an event to the DLLs plot function "changeVisibilityCount". This way I get in lua notified about every visibility change and then I can "copy" this over to the friend-team.
    But when friendship ends, I need to revert the visibilty to default somehow. And the only solution that came to my mind is, that I need to save all the plots with viscounts that are shared. (only alternative may be that I hide the whole map and then call a function that builds of visibilty from scratch based on current units and so on. But I don't think there is such a function).
    So I save an array like this:
    g_visible_made_plots[iTeam][iCivTeam][iPlot] = g_visible_made_plots[iTeam][iCivTeam][iPlot] + viscount
    Depending on the number of teams, the units and the map size, this gets a really huge array. And this array is updated on every single call of "changeVisibilityCount", which is very often eg. when a unit moves, while we need to seperately save the array every single time of them. I already only save the relevant info for the specific team with:
    save(pLeader,"g_visible_made_plots",g_visible_made_plots[iCivTeam])
    but it is still by far too much, even on medium maps after ~50 turns it starts heavily lagging.
     
    Last edited: Mar 26, 2021
  2. Serp

    Serp King

    Joined:
    Apr 1, 2015
    Messages:
    646
    I searched for the Fantasy mod whoward mentioned and found the follwing (currently reading the part on where to put an dll event to catch every save):
    https://forums.civfanatics.com/thre...lua-table-data-through-game-save-load.442249/

    edit:
    Problem solved thanks to that article :) I added an game event to the DLL when the game is saved. This way I only need to save with SaveUtili (or TableSave) when it is really needed -> the most efficient way.

    I really wonder why no known DLL, like whowards or Community patch, implemented this GameSave Event in their DLLs, it is a really important one.
     
    Last edited: Mar 26, 2021
  3. Serp

    Serp King

    Joined:
    Apr 1, 2015
    Messages:
    646
    @whoward69 Do you ever experienced a "freeze" because of "gDLL->GetScriptSystem()" ? Like described from Pazyryk here: https://forums.civfanatics.com/thre...lua-table-data-through-game-save-load.442249/
    I added his code to the DLL, but it seems not only the first autosave is causing this freeze, but randomly also other ones. I added CUSTOMLOG prior and after gDLL->GetScriptSystem() and indeed, this is the reason for the freeze between turns (when autosave is done). (it also happens when I use your GAMEEVENTINVOKE_HOOK(...) instead )

    I also had this kind of freeze randomly happening half a year ago and never found out why. But maybe it was just another Event that froze during gDLL->GetScriptSystem().
    So my final question is:
    Is there a way to safely call gDLL->GetScriptSystem() and abort this after ~5 seconds, so the game can at least continue, instead of freezing?
     
  4. Serp

    Serp King

    Joined:
    Apr 1, 2015
    Messages:
    646
    my assumption, that ""gDLL->GetScriptSystem()" is the reason for the freeze, were wrong. I put more "prints" in the dlls code and found this:

    Code:
    bool LuaSupport::CallHook(ICvEngineScriptSystem1* pkScriptSystem, const char* szName, ICvEngineScriptSystemArgs1* args, bool& value)
    {
       // Must release our lock so that if the main thread has the Lua lock and is waiting for the Game Core lock, we don't freeze
        CUSTOMLOG("CustomMods CallHook %s 1", szName);
        bool bHadLock = gDLL->HasGameCoreLock();
        CUSTOMLOG("CustomMods CallHook %s 2", szName);
        if(bHadLock)
        {
            CUSTOMLOG("CustomMods CallHook %s 3", szName);
            gDLL->ReleaseGameCoreLock();
        }
        CUSTOMLOG("CustomMods CallHook %s 4", szName);
        bool bResult = pkScriptSystem->CallHook(szName, args, value);
        CUSTOMLOG("CustomMods CallHook %s 5", szName);
        if(bHadLock)
            gDLL->GetGameCoreLock();
        CUSTOMLOG("CustomMods CallHook %s 6", szName);
        return bResult;
    }
    
    resulted in the following logs prior to the freeze:

    Lua\CvLuaSupport.cpp[75]: LuaSupport::CallHook - CustomMods CallHook PreSaveEvent 1
    Lua\CvLuaSupport.cpp[77]: LuaSupport::CallHook - CustomMods CallHook PreSaveEvent 2
    Lua\CvLuaSupport.cpp[80]: LuaSupport::CallHook - CustomMods CallHook PreSaveEvent 3
    Lua\CvLuaSupport.cpp[83]: LuaSupport::CallHook - CustomMods CallHook PreSaveEvent 4

    So I guess the Lock system is the reason for the freeze. Since the SaveGame/PreSaveEvent is placed into the CvGame.cpp file, and CvDllGame.cpp includes code dealing with the Lock, maybe one should not mess with the lock within CvGame file? No clue...
     

Share This Page