Results 1 to 9 of 9

Thread: [RESOLVED] Save progress in Text Adventure?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Resolved [RESOLVED] Save progress in Text Adventure?

    I've set up buttons with the Save Dialog and Open Dialog in Visual Basic 2010, but It doesn't really do anything when I save, and there's nothing there when I try to open. I didn't actually put any code in the dialogs or anything. Basically, I just want to beable to have an option in my VB2010 text adventure game for the user to save his/her progress. Please help!

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: Save progress in Text Adventure?

    im also new at this programming language so beware that my advices may be just senseless or wrong, but what i can think of right now is that you should save your "progress" into a .txt file, namely everytime u type a message in a text for example

    "You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
    A:Left
    B:Right
    C:Front
    D: Back

    imagine this example, in the program itself, everytime u change the text for the player try saving it as a variable, example like this:
    in public class you add a variable:
    Public progress as integer = 0

    Now an example
    progress = 1
    Richtextbox1.text = "welcome to a text game would u like to read the tutorial?"


    progress = 10
    Richtextbox1.text = "You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
    A:Left
    B:Right
    C:Front
    D: Back


    and imagine the user saves the game, once he does that you write the variable value into a .txt file,
    and when you click load if progress = 10 then
    Richtextbox1.text = "You are now in front of the mill, at your left side there is a wheel, at the right side there is a key laying on the floor and behind there is a dark forest, which way do u want to go?
    A:Left
    B:Right
    C:Front
    D: Back
    end if

    i suggest that you make richtextboxes text as functions, so you wont have to do my mistake of copying and pasting the same code over and over again. Invoke it with 1 word only,

    if there are more aspects of the game like, hp, gold, weapons, you could also save those as variable and load them up the same way.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Save progress in Text Adventure?

    The OpenFileDialog and SaveFileDialog components don't actually open and save files. How could they? There is basically an unlimited number of file formats out there so they can't know about them all. They simply provide an interface for the user to select a location to open from or save to. It's up to you to provide the appropriate code to read or write the file the user selects in the appropriate format.

    http://www.google.com.au/search?q=re...ient=firefox-a
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: Save progress in Text Adventure?

    oh and here it is what u need to properly save a file and to read a file:
    Save:
    Dim filesave As System.IO.StreamWriter

    Example:
    filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
    filesave.Write(currentprogress) 'this can be anything you want, a variable, a string, an array, whatever....
    filesave.Close() 'everytime you open and save a file you always need to close it because if u do not there will be issues while trying to save to a file which is in use.


    Load:
    Dim fileload as System.IO.StreamReader

    Example:
    fileload = My.Computer.FileSystem.OpenTextFileReader(""../../../AdventureProgress.txt")
    Dim progressload As String = fileload.ReadToEnd 'This is used to save the details from the adventure progress into a string, you should try with an array instead.
    fileload.Close()


    Note1: if you want to check for an existent file here is the code for it
    if My.Computer.FileSystem.FileExists("../../../AdventureProgress.txt") Then
    'Execute your code here
    End if


    Note2: when you save to a file, it is irrelevant if the file exists or not, it will automatically create it.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Re: Save progress in Text Adventure?

    Appreciate the answer, Legendary agent. This is pretty much exactly what I'm looking for - wanting to save variables.

    I must be doing something wrong though, because while I'm not getting any errors my open and save buttons don't appear to do anything.

    This is what I've got:

    Here's my open button:

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fileload As System.IO.StreamReader
            fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
            Dim progressload As String = fileload.ReadToEnd 'This is used to save the details from the adventure progress into a string, you should try with an array instead.
            fileload.Close()
        End Sub
    And here's my save button:

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim filesave As System.IO.StreamWriter
            filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
            filesave.Write(doonce, Clock, HungerBar, SleepBar, XpBar, Intl, Chr, Spd, Stl, Dex, HealthBar) 'this can be anything you want, a variable, a string, an array, whatever....
            filesave.Close() 'everytime you open and save a file you always need to close it because if u do not there will be issues while trying to save to a file which is in use.
        End Sub

  6. #6
    Hyperactive Member
    Join Date
    Apr 2011
    Posts
    268

    Re: Save progress in Text Adventure?

    Ah ic, well you need to split them before you add them to the file, for example lets use the ' symbol to split the variables between each others

    Modified load:

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fileload As System.IO.StreamReader
            fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
            Dim progressload As String = fileload.ReadToEnd
            fileload.Close()
    Dim datasplit As String() = progressload.Split(New Char() {"'"c})
    doonce = datasplit(1)
    Clock = datasplit(2)
    HungerBar = datasplit(3)
    SleepBar = datasplit(4)
    Xpbar = datasplit(5)
    Intl = datasplit(6)
    Chr = datasplit(7)
    Spd = datasplit(8) 
    Stl = datasplit(9)
    Dex = datasplit(10)
    HealthBar = datasplit(11)
    End Sub

    Now to save:


    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim filesave As System.IO.StreamWriter
            filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
            filesave.Write("'" & doonce & "'" & Clock & "'" & HungerBar & "'" & SleepBar & "'" & XpBar & "'" & Intl & "'" & Chr & "'" & Spd & "'" & Stl & "'" & Dex & "'" & HealthBar)
            filesave.Close() 
        End Sub
    [/QUOTE]
    Last edited by Legendary_Agent; Apr 28th, 2011 at 05:02 PM.

  7. #7
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Save progress in Text Adventure?

    Hi Lucien,

    It looks like your trying to save several variables into your text file. There is nothing wrong with this, but it makes things a bit more complicated when it comes to loading your file back into your variables.

    You could save each of your variables into an application setting instead.

    In Visual Studio click: 'Projects' --> 'YourApplicationName Properties...'. On the properties Menu click 'Settings'. You can add a Setting for each Variable you want to save and give it the same data type.

    Heres a quick example. I added a setting called "HungerBar" and set its type to Integer. This is the code that would save/load your variable to this setting:
    VB Code:
    1. Dim HungerBar As Integer = 0 'Your Global Variable
    2.  
    3. Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
    4.     HungerBar += 100 'This is just to show you that any changes are being saved
    5.     My.Settings.HungerBar = HungerBar 'Save your variable into your setting
    6. End Sub
    7.  
    8. Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
    9.     HungerBar = My.Settings.HungerBar 'Load your hunger setting back into its variable
    10.     MsgBox(HungerBar) 'This is the value of the loaded setting
    11. End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Thumbs up Re: Save progress in Text Adventure?

    Works like a charm! I really appreciate it!

    I left out a few details about the variables I was wanting to save, so a few changes were necessary. Going to go ahead and post them in case the information is of use to anyone:

    Load:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fileload As System.IO.StreamReader
            fileload = My.Computer.FileSystem.OpenTextFileReader("../../../AdventureProgress.txt")
            Dim progressload As String = fileload.ReadToEnd
            fileload.Close()
            Dim datasplit As String() = progressload.Split(New Char() {"'"c})
            doonce = datasplit(1)
            XpBar.Value = datasplit(2)
            HungerBar.Value = datasplit(3)
            SleepBar.Value = datasplit(4)
            HealthBar.Value = datasplit(5)
            Intl.Text = datasplit(6)
            Chr.Text = datasplit(7)
            Spd.Text = datasplit(8)
            Stl.Text = datasplit(9)
            Dex.Text = datasplit(10)
            Clockweek = datasplit(11)
            Clocksec = datasplit(12)
            Clockmin = datasplit(13)
            Clockhour = datasplit(14)
    
        End Sub
    Save:

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim filesave As System.IO.StreamWriter
            filesave = My.Computer.FileSystem.OpenTextFileWriter("../../../AdventureProgress.txt", False)
            filesave.Write("'" & doonce & "'" & XpBar.Value & "'" & HungerBar.Value & "'" & SleepBar.Value & "'" & HealthBar.Value & "'" & Intl.Text & "'" & Chr.Text & "'" & Spd.Text & "'" & Stl.Text & "'" & Dex.Text & "'" & Clockweek & "'" & Clocksec & "'" & Clockmin & "'" & Clockhour & "'")
            filesave.Close()
        End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    88

    Thumbs up Re: Save progress in Text Adventure?

    Quote Originally Posted by jay20aiii View Post
    Hi Lucien,

    It looks like your trying to save several variables into your text file. There is nothing wrong with this, but it makes things a bit more complicated when it comes to loading your file back into your variables.

    You could save each of your variables into an application setting instead.

    In Visual Studio click: 'Projects' --> 'YourApplicationName Properties...'. On the properties Menu click 'Settings'. You can add a Setting for each Variable you want to save and give it the same data type.

    Heres a quick example. I added a setting called "HungerBar" and set its type to Integer. This is the code that would save/load your variable to this setting:
    VB Code:
    1. Dim HungerBar As Integer = 0 'Your Global Variable
    2.  
    3. Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
    4.     HungerBar += 100 'This is just to show you that any changes are being saved
    5.     My.Settings.HungerBar = HungerBar 'Save your variable into your setting
    6. End Sub
    7.  
    8. Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
    9.     HungerBar = My.Settings.HungerBar 'Load your hunger setting back into its variable
    10.     MsgBox(HungerBar) 'This is the value of the loaded setting
    11. End Sub
    This looks promising as well, I think I'll give it a try. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width