1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Lead your forces to battle in the 15th Techtree Contest. The call is yours, commander!
    Dismiss Notice
  4. The reforging of the races is complete. Come see the 14th Techtree Contest Results.
    Dismiss Notice
  5. It's time to choose your horse in the race - the 32nd Modeling Contest Poll is up!
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[GUI] Save and Load in GUI

Discussion in '"Graveyard"' started by Chaosy, Oct 24, 2014.

  1. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17

    Easy Save and Load in GUI


    -Only works in Single Player until updated!-

    Introduction


    Greetings. I decided to create a tutorial for saving/loading in GUI. I know that there are existing GUI systems out there. Code Gen and Ace Harts are the most famous ones. However this tutorial is directed for those that want to create their own, or the existing systems lack something that they need in terms of saving, whatever that might be. I also quickly want to mention that I wont explain any GUI actions in this tutorial since it's not for starters, unless you don't understand simple GUI actions yet, you should go and learn some GUI before heading back to this tutorial.

    Difficulty


    5/10


    What is needed?


    *above basic knowledge of GUI in general
    *New Gen Editor (JNGP) - Download
    *FILE I/O - Download
    *Local Files enabled (Extensions > Enable Local Files)

    What is file I/O and why do we use it?


    File I/O allows the user to create and read files which contain information of our choice.

    Pros:
    *Encrypts/Decrypts the information we decide to store into our files
    *Saves us a good chunck of work and effort
    *We don't need to care about a save code, which means using this method the user can type "-load" instead of "-load xxx xxxx xxx xx xxxxx"

    Cons:
    *file I/O is made in vJASS, which forces us to use custom scripts more than we should.


    Preperations


    In order to understand this tutorial fully you will need to know about GetLocalPlayer().

    Once you've done that. We will create our variable for the GetLocalPlayer().

    • Init
      • Events
        • Map initialization
      • Conditions
      • Actions
        • Custom script: set udg_local_player = GetLocalPlayer()


    Before we start, you also need to decide on what you want to store. In my example I decided to save the following values.
    *Player Name
    *Main Hero
    *Hero Level
    *Player Gold
    *Player Lumber

    For learning purposes I strongly suggest that you don't start with loading a unit, that's the most annoying part. Try doing it with gold or something like that at first.

    Note: You can only save string values, and not other variable types.

    This is my setup so I got some values I can save in my map.

    • Just for testing
      • Events
        • Map initialization
      • Conditions
      • Actions
        • Player - Add 666 to Player 1 (Red) Current lumber
        • Player - Add 1337 to Player 1 (Red) Current gold
        • Set hero_types[1] = Paladin
        • Set hero_types[2] = Archmage
        • Set hero_types[3] = Mountain King
        • Set hero_types[4] = Blood Mage
        • Set hero_names[1] = Paladin
        • Set hero_names[2] = Archmage
        • Set hero_names[3] = Mountain King
        • Set hero_names[4] = Blood Mage
        • Set hero_count = 4
        • Set spawn_loc = (Center of Spawn Region <gen>)
        • Unit - Create 1 hero_types[(Random integer number between 1 and hero_count)] for Player 1 (Red) at spawn_loc facing Default building facing degrees
        • Set player_hero = (Last created unit)
        • Hero - Set player_hero Hero-level to (Random integer number between 2 and 10), Hide level-up graphics


    Saving


    Now to the real triggering part of the tutorial.

    We'll use the following script to declare (create) a file variable. Note that the variable is of the type "File" and is named "file".
    • Custom script: local File file


    That line need to be on the top of the trigger else JNGP will throw errors in your face.

    Now that we got our file variable, we need to make some settings before we can actually start to do stuff.

    • Custom script: local File file
    • Set mapName = testMap
    • Set file = exp


    *mapName = the folder that the file will be created in.
    *file = file name

    The result will be: Warcraft III/GameData/mapName/file

    Now we're ready to finally start saving values. We need to set our file variable before we can start with the actual saving though.

    • Custom script: set file = File.open(udg_mapName, udg_file, File.Flag.WRITE)


    Once that's placed out, we can start to use the file.write() basically it writes something on the first line of a empty text document.

    • Set what_to_save = (Name of (Triggering player))
    • Custom script: call file.write(udg_what_to_save)


    In my case the player name will be saved on the first row of the file.

    After that you simply repeat the process over and over until everything is saved.

    • GUI Save
      • Events
        • Player - Player 1 (Red) skips a cinematic sequence
      • Conditions
      • Actions
        • Custom script: local File file
        • Set mapName = testMap
        • Set file = exp
        • Set player = (Triggering player)
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • local_player Equal to Player 1 (Red)
          • Then - Actions
            • Custom script: set file = File.open(udg_mapName, udg_file, File.Flag.WRITE)
            • Set what_to_save = (Name of (Triggering player))
            • Custom script: call file.write(udg_what_to_save)
            • Set what_to_save = (Name of player_hero)
            • Custom script: call file.write(udg_what_to_save)
            • Set what_to_save = (String((Level of player_hero)))
            • Custom script: call file.write(udg_what_to_save)
            • Set what_to_save = (String((player Current gold)))
            • Custom script: call file.write(udg_what_to_save)
            • Set what_to_save = (String((player Current lumber)))
            • Custom script: call file.write(udg_what_to_save)
            • Game - Display to (All players) the text: saved!
            • Custom script: call file.close()
          • Else - Actions



    Load


    Loading the information we've stored use the same method as before with a few changes. We start off just like before.
    • Custom script: local File file
    • Set mapName = testMap
    • Set file = exp
    • Set player = (Triggering player)


    However when setting our file variable to a value there is a slight difference. File.Flag.WRITE > File.Flag.READ
    • Custom script: set file = File.open(udg_mapName, udg_file, File.Flag.READ)


    To start the actual loading, I use a string variable named "what_to_load" and makes it read the upper row inside our file.
    • Custom script: set udg_what_to_load = file.read()


    After that we just need to use that variable for our purposes in. In my case it's the player name.
    • Custom script: set udg_what_to_load = file.read()
    • Player - Set name of player to what_to_load


    After that, it's just copy > paste pretty much. re-store the variable, and reuse it.
    • GUI Load
      • Events
        • Player - Player 1 (Red) types a chat message containing load as An exact match
      • Conditions
      • Actions
        • Custom script: local File file
        • Set mapName = testMap
        • Set file = exp
        • Set player = (Triggering player)
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • local_player Equal to Player 1 (Red)
          • Then - Actions
            • Custom script: set file = File.open(udg_mapName, udg_file, File.Flag.READ)
            • Custom script: set udg_what_to_load = file.read()
            • Player - Set name of player to what_to_load
            • Custom script: set udg_what_to_load = file.read()
            • For each (Integer A) from 1 to hero_count, do (Actions)
              • Loop - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • what_to_load Equal to hero_names[(Integer A)]
                  • Then - Actions
                    • Unit - Create 1 hero_types[(Integer A)] for Player 1 (Red) at spawn_loc facing Default building facing degrees
                  • Else - Actions
            • Custom script: set udg_what_to_load = file.read()
            • Hero - Set (Last created unit) Hero-level to (Integer(what_to_load)), Hide level-up graphics
            • Custom script: set udg_what_to_load = file.read()
            • Player - Add (Integer(what_to_load)) to Player 1 (Red) Current gold
            • Custom script: set udg_what_to_load = file.read()
            • Player - Add (Integer(what_to_load)) to Player 1 (Red) Current lumber
            • Custom script: call file.close()
          • Else - Actions


    Credits


    Nestharus - File I/O

     
    Last edited: Jun 22, 2015
  2. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Why not just use gamecache? : )


    Without synchronization, File I/O is a glorified gamecache = P
     
  3. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17
    I'll make sure to include it later on. I am just a little tired atm. Thanks!
     
  4. What do you mean by without synchronization?


    Also good tutorial, colors look pleasant.
    Well at the moment this is just as good as game cache.

    Also this:
    • Custom script: local File file
    • Set mapName = testMap
    • Set file = exp

    You have udg_file and file here, it might be misleading for some GUI users.
     
  5. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    File only on one machine. What happens when only 1 player makes a unit or something? :)
     
  6. I see, well I'm intrigued to see how OP will turn this to mp :)
     
  7. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    They have to usr my Network lib, which is too slow and needs an update. If they use it, this tut can't be used unless u wanna freeze the game for 5 mminutes while sync takes place.

    We already know how to fix Network, all of us that code core resources. The prob is that nobody has time to update it.

    If it's updated, you can just use my pure GUI codeless save/load stuff. Will be easier to use and faster.
     
  8. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17
    Ouch, I didn't take that into consideration. I will test if it works without a huge load time with network. If it's too slow I will think of something else.
     
  9. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    I already told you what happens... lol
     
  10. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Ever got around to testing it in multiplayer? As for Nes' statement, I assume that the lag only happens when the file is large? Or does the freeze happen in any other situation?
     
  11. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17
    No I did not, I kinda trust Nes to know about Save/Load.
     
  12. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,873
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    This tutorial completely fails to mention that this only works with "enable local files" enabled in the Windows registry.
     
  13. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17
    Thanks for pointing that out. I honestly never thought of that, I always have it enabled myself since I don't see a reason not too. I will mention it somewhere.

    rep+
     
  14. I really liked this. Until I read it was singleplayer only. In that case, what is the point of this? Save game works much easier and much better and saves the hassle of looking for files, copying a large amount of characters etc... Also like Zwie said.

    Its great as a tutorial on how to make your own.

    But it sucks if its only for singleplayer.
     
  15. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,816
    Resources:
    17
    Maps:
    1
    Spells:
    10
    Tutorials:
    6
    Resources:
    17
    From what I understand it work in multiplayer but it's very slow apparently. Though I have never tested to check exactly how slow.
     
  16. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Well, the reason it's slow is because the thread locking that Network uses needs to be optimized a bit and needs to use asynchronous things to achieve its synchronous behavior. Right now locking floods the network.
     
  17. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,873
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    It has a purpose in singleplayer, as the normal method of saving/loading via the game menu will break periodic timers and waits.

    Of course, the mapper *can* technically account for these things with workarounds, but more than often it is easier (and also desirable) to implement a manual save/load into a singleplayer map over the menu based save/load.
     
  18. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Zwiebelchen, can just use gamecache directly in single player.
     
  19. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,381
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    It's basicily a tutorial for GUI users how to use File IO with a few of custom scripts.
    Save/Load tequniques usually use some sort of encryption, and also some synchronisation library.

    I suggest to screw the idea of making full Save/Load tutorial. They should go with existing systems and learn them to use.
    But a simple tutorial for GUI users how to properly use File IO as easy as possible. Why not. ¯\_(ツ)_/¯
     
  20. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,381
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Submission: Save Load in GUI
    Date: 28 November 2016
    Status: Graveyard
    Note:

    Some things needs to be changed and clarified. Related to my post above.
    In case you will be working on it just ping here with your thoughts. For now, Graveyard.