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

SaveUtils.lua -- Itemized Data Storage Across Mods

Discussion in 'Civ5 - SDK / LUA' started by Whys, Oct 20, 2010.

  1. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    I am easily annoyed and prone to speak in frustration. It's unproductive, and I apologize for that. You have become the unfortunate focus of my growing sense that this community's stagnation has become a spectator's sport. We were given poor tools to work with and everyday it seems fewer of us have the time or energy to get dirty building something for everyone, while it is far easier to fire off a few comments in the forums. I realize I can be a difficult personality, so let my code speak for me when I say I support this community and wish to work together. Let me speak for myself when I say I am not meant to be this community's public voice. I am merely an advocate for a better future and a grunt with a shovel.

    Shovel with me and I shall not mind. :)
     
  2. SamBC

    SamBC Emperor

    Joined:
    Feb 2, 2010
    Messages:
    1,052
    Location:
    Lancaster
    I'd be the first to say that you've contributed better than myself! I've been frustrated by both little time and by frustration (so to speak), but that's no excuse, in a sense because there's nothing to excuse. I don't think there's enough evidence to say you're a better designer or coder than me, but you're certainly good, and you appear to have a good handle on Lua.

    I'd like to think the tools will get better. I certainly get the feeling that there are people at Firaxis who would like to give us better tools, Sseckman certainly seems to want to from his comments at the 2K forums and wiki, but I can only imagine what life must have been like at Firaxis, particularly the Civ team, in recent months - and I don't particularly want to try, it seems scary.

    In any case, I think I've begun to shovel now - and no hard feelings here.
     
  3. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    at 167
    i use save() save a value and load() it any time ,it will get right value.
    but then i savegame and loadgame, the load() will return the first value instand of last value.
    why?
     
  4. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    167? That's in your code. You'll need to post it so I know what it means. I also don't know what you mean by "first value instead of last value". Can you give an example?
     
  5. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    the 167 means game ver 1.167

    ex
    at turn0, i save 1 to Players[0] by a string
    and load it print it, it is 1
    at turn1, i save 6 to Players[0] by the same string to override it
    and load it print it, it is 6

    but now i savegame and exit to mainmenu
    load savefile and load the Players[0] by the string
    it is 1,but i want it is 6
    and it is 6 before i update the game to 1.167
     
  6. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Try...

    setCacheState( 0 ); --no cache.

    ...and let me know if that fixes it. I don't wish for that to be the solution because it can hurt performance, but if that's clearly the problem then I know where to look for a better solution.
     
  7. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    i test three times.
    it seems work fine after setting no cache.
     
  8. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Okay, now try...

    setCacheState( 1 ); --default.

    ...broken again? Are you running Tuner and can you tell me if there are any output statements in the lua console?
     
  9. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    yes it broken again after enable cache.
    Tuner has no output statements from SaveUtils
    (i set WARN_NOT_SHARED = false, if i do not set this
    it will output "Warning: cache not shared.")
     
  10. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Instead of Players[0], try...

    Players[Game.GetActivePlayer()];

    ...if that doesn't fix it, zip your code and send it to me and I will look at it. I've given SaveUtils a close look and I'm not seeing any obvious bugs.
     
  11. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    i store a lot of table at different Players
    so the Players[0] just for a ex
    what is your mail? i do not know it ?

    or you can find the v11 of realnameerath at mod hub
     
  12. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    I've sent you my email address, but I have not received anything yet. In the mean time, I was unable to reproduce your error with the following example. In this example, test.lua is an InGameUIAddin. This test has been performed both with and without ShareData.lua (also an InGameUIAddin). As expected, without ShareData, there is a 'not shared' warning, but the test results are otherwise identical and both without error.

    test.lua (InGameUIAddin)
    Code:
    include( "SaveUtils" ); MY_MOD_NAME = "WhysMod";
    
    function test()
      local pPlayer = Players[Game.GetActivePlayer()];
      local data = load( pPlayer, "data" ) or 0;
      data = data +1;
      print( "data: "..tostring( data ) );
      save( pPlayer, "data", data );
    end
    Events.ActivePlayerTurnStart.Add( test );
    
    This code simply outputs an increasing integer to the lua console. When saved and reloaded, I always get the expected result.
    _
     
  13. Seraphim8400

    Seraphim8400 Warlord

    Joined:
    Nov 7, 2010
    Messages:
    112
    that is because i forgot my ac adapter at office...
    so i can not open the notebook which has civ5....
    so if you want you can get release ver at mod hub
    its change only that set no cache.
    or you can wait for some days ,i will send you a Detailed report as soon as i back home
    i am very sorry for that
     
  14. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Not a problem. When you have a chance, try running my example code above and let me know if it is giving you the proper results.
     
  15. jerseymike25

    jerseymike25 Chieftain

    Joined:
    Dec 24, 2005
    Messages:
    50
    I don't see a better place to ask this, so I'll try here. In my attempt to understand what this mod is doing, it seems like you're sharing chunks of raw data -- which makes me think that it *might* be possible to manipulate that data.

    What I mean, for example: Somewhere, I haven't discovered where (in terms of Lua or the Table browser), state information for a particular player is stored. Their current attitude towards player X, or if they're a minor civ, their current Quest in relation to player X.

    With a binary memory editor (like AddMoney), I can find and change something like the current Quest value. My assumption is that it's part of the Player Class (as defined in C++), but the field itself is declared as private, and only the Get accessor exists (GetActiveQuestForPlayer()). It's also likely that if indeed this value is stored within a class object, that it will live at the same offset (differing by PlayerID, etc... but the Quest data for Player[0] can always be expected to be at offset X, Player[1] at offset Y, etc.)

    I am not familliar enough with Lua to know if this is possible or not. Bing and Google haven't revealed much to me, as the only place I see any sort of direct addressing is with emulator mods. If it is indeed possible, then it certainly opens up a number of things that we can do, albeit in a very hack-y way. But I can't make enough sense out of what I see in this thread here to fill in those gaps.

    Basically: Is it possible to manipulate bits somewhere in a chunk of memory with the implementation of Lua that we can work with here?
     
  16. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    SaveUtils is mostly for accessing the ScriptData slot attached to all players, plots, and units. It's just a string, but unlike anything else in lua, it persists between saved games. So in order to save and reload complex data structures, you need to serialize and deserialize the data. In essence, that is what SaveUtils does, but more specifically, it handles the string so as to allow all mods to share the same ScriptData slot without overwriting each others data. Additionally, when data is deserialized, by default, it is saved to a cache, so if requested later, it doesn't have to deserialize the string again. However, in order for the same cache reference to be available to all mods so they can all share the same cache data, it's necessary to use the built in LuaEvents to get the data where it needs to go. ShareData is a separate file that, when loaded, provides those lua events. Thus when ShareData is loaded, SaveUtils can share the cache of deserialized ScriptData with all mods that use SaveUtils. It's just a reference passed between lua states via a lua event, and for convienence, SaveUtils also offers easier to use wrapper functions for ShareData's lua events, since these have to be placed in a file separate from ShareData itself.

    How any of that is performed with regards to binary memory, I do not know, but from what I've seen, I'd say it's likely possible in lua and NOT likely possible in Civ. Happy hunting anyway. :)
     
  17. jerseymike25

    jerseymike25 Chieftain

    Joined:
    Dec 24, 2005
    Messages:
    50
    Damn. It's already been figured out we can't call outside of that Lua environment as well (to load a different library), correct? Thanks for the rundown, though.

    Man... make with the SDK DLL already! I want to be rewarded when I connect a damned road to a CS! :)
     
  18. Moaf

    Moaf Warlord

    Joined:
    May 2, 2008
    Messages:
    100
    Location:
    Vienna, Austria
    Hi, just coming by to say Thanks for the great work. I'll publish a mod soon and i will use Saveutils, Sharedata and a modified version of Customnotification (that doesnt modify vanilla notificationpanel) in it.

    Props! :)
     
  19. Whys

    Whys Between the Lines

    Joined:
    Oct 20, 2007
    Messages:
    456
    Sounds good. Give it a thread and i'll link to it from my customnotification page.

    And welcome aboard!

    edit: err... 3 years ago. 9 posts? :]
     
  20. Moaf

    Moaf Warlord

    Joined:
    May 2, 2008
    Messages:
    100
    Location:
    Vienna, Austria
    I already posted a solution in your other thread.

    Hmmm... ermm; well.. *ahem* Just tried to win a prize in lurking i guess :rolleyes:
     

Share This Page