Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Creating a game in DOS - Saving game to a file?  (Read 3399 times)

0 Members and 1 Guest are viewing this topic.

Tomwatts

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows XP
    Creating a game in DOS - Saving game to a file?
    « on: July 06, 2015, 10:26:21 AM »
    Hi guys, just for the *censored* of it i want to make a text-based adventure game in MSDOS, I was wondering, is there a way I can save the users game progress to a file?

    For example:
    I have a piece of code which asks the user to pick a Class, so how could I now save this to a file and then call upon it later?

    Thank you! :)

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Creating a game in DOS - Saving game to a file?
    « Reply #1 on: July 06, 2015, 10:44:49 AM »
    save the variables to a file
    Code: [Select]
    >>savedgame.txt echo CLASS=%CLASS%
    reload the variables from the file
    Code: [Select]
    FOR /F "delims=" %%G in (savedgame.txt) do SET %%G

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Creating a game in DOS - Saving game to a file?
    « Reply #2 on: July 06, 2015, 01:32:13 PM »
    This caught my interest... not hijacking this thread, but just wanted to inquire for additional info on this which might help Tomwatts with his game design.

    When dealing with multiple variables written for a single file to write to, what is the best method of reading back the data in batch?

    Such as if you had:

    Class = Human
    Level = 3
    Gold = 2400
    WeaponMinDamage= 5
    WeaponMaxDamage= 10
    Armor = 23
    XLocation = 6
    YLocation = 84


    and had a file appended such as SavedGame.log with

    Quote
    Human
    3
    2400
    5
    10
    23
    6
    84

    Would it be better to be dealing with delimited data such as
    Quote
    Human,3,2400,5,10,23,6,84

    In which you know that the whole of the data contained between comma delimiters should be used so that you don't have to read in an unknown length of characters?

    And if the best method is to use a delimited data file, could an example be given on how to best implement this? As well as if just appended data is best to have each variables value on a separate line and it doesnt matter of length because of character line isolation from the following lines in which there is an instruction to read an entire line to end of line such as maximum allowed character length per line, an example of implementing that?


    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Creating a game in DOS - Saving game to a file?
    « Reply #3 on: July 06, 2015, 02:08:07 PM »
    If you are going to use delimited data then you need to make sure all separated values also have quotes because the FOR command will drop empty tokens.
    I prefer the method I used above.  Makes it real easy to define the variable.

    But you could also do a flat file with one line for each variable and redirect the file into a code block.

    Code: [Select]
    < savedgame.txt (
    set /p class=
    set /p level=
    set /p gold=
    set /p weapondamage=
    )

    Tomwatts

      Topic Starter


      Rookie

      • Experience: Familiar
      • OS: Windows XP
      Re: Creating a game in DOS - Saving game to a file?
      « Reply #4 on: July 06, 2015, 02:50:43 PM »
      Thank you so much for that! That's worked a treat!

      Another thing - How could I create some field validation for the character selection page?

      set /p input=
      if %input% == 1 goto Warrior
      if %input% == 2 goto Hunter

      so at the bottom i'd have something like..

      if %input% == 3> echo Invalid Selection

      Any ideas? :) Or am i thinking into this too much?

      Tomwatts

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows XP
        Re: Creating a game in DOS - Saving game to a file?
        « Reply #5 on: July 06, 2015, 02:52:50 PM »
        And thank you very much guys!

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Creating a game in DOS - Saving game to a file?
        « Reply #6 on: July 06, 2015, 03:09:00 PM »
        Use the CHOICE command if you are using the batch file on Windows versions greater than XP.  Windows XP did not have the choice command.  The choice command will not continue until you enter in a valid choice.

        Tomwatts

          Topic Starter


          Rookie

          • Experience: Familiar
          • OS: Windows XP
          Re: Creating a game in DOS - Saving game to a file?
          « Reply #7 on: July 10, 2015, 03:53:33 AM »
          Hello!

          Firstly my apologies for the rubbish reply, been so busy with work the past few days!

          The CHOICE command works perfectly, such a nice solution to the problem I was having! I'll be implementing this!

          It's great having a community to talk to about this, i'm sure when i'm done you'd like a copy? If so, i'll make you get one.

          The next thing I am going to do is implement a data file so I can have an 'Inventory' of sorts too!

          I'm not using Windows XP btw, i'm using Win 7, but for some reason my profile says Win XP, i need to change this.

          Regards,
          Tom