Save game/Load game
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35

Thread: Save game/Load game

  1. #1
    Join Date
    Sep 2009
    Posts
    126

    Question Save game/Load game

    Hello.
    Here is my question/problem.
    I have made an game and I want the players to could save the situation they are in.
    So I have made the save thing and the code is:
    Private Sub Command39_Click()
    Dim namefile As String
    namefile = Text3
    If Text3.Text = "" Then
    MsgBox "Fill in a Username!"
    Else
    Open App.Path & "\Saves\" + namefile + ".txt" For Output As #1
    Print #1, Label5 + Label1
    Print #1, Label10 + Label11
    Print #1, Label38 + Label39
    Print #1, Label31 + Label33
    Print #1, Label32 + Label34
    Print #1, Label42 + Label43
    Print #1, Label19 + Label24
    Print #1, Label17 + Label25
    Print #1, Label16 + Label26
    Print #1, Label20 + Label27
    Print #1, Label21 + Label28
    Print #1, Label22 + Label29
    Print #1, Label23 + Label30
    Close #1
    End If
    End Sub

    if you wounder why it shall print so many things, those are the data/info. Example: Label5 is the text "Current money" and label1 is the money situation.
    But now I need to get the Load thing.
    So I need the code so that when my player press load and chose a save to load the label1 (for example) will be that what it is on the save. So for example when I press "Save" and save my info/data and let say my current money was: 8000$. When I start up the game ones more my current money is: 10000$ but when I press "Load" and select the save my money shall be: 8000$ =the amount I save it on. So any one that can help me?
    //Equx
    Sorry for my bas english!

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    Open the file for input

    Use the Line Input # statement to retrive each line

    see your online help for details

  3. #3
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by DataMiser View Post
    Open the file for input

    Use the Line Input # statement to retrive each line

    see your online help for details
    Can you write the exact code?
    I'm noob on codes and VB atm, trying to get better and better.

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    Look up line input in the help file. It should tell you all you need to know.

  5. #5
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by DataMiser View Post
    Look up line input in the help file. It should tell you all you need to know.
    I haven't got the help file. OR I have but it's not working. That why I wrote here!
    BTW: When I open the help file there comes just an emty error box and when I press the "X" the hole program will exit

  6. #6
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    *sorry for dubble post!*
    Now I have made some progress:
    The save button code:
    Dim namefile As String
    namefile = Text3
    If Text3.Text = "" Then
    MsgBox "Fill in a Username!"
    Else
    Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Output As #1
    Print #1, Label5 + Label1 + Label10 + Label11 + Label38 + Label39 + Label31 + Label33 + Label32 + Label34 + Label42 + Label43 + Label19 + Label24 + Label17 + Label25 + Label16 + Label26 + Label20 + Label27 + Label21 + Label28 + Label22 + Label29 + Label23 + Label30
    Close #1
    End If

    The load button code:
    namefile = Text2
    Open "D:\Projects\GR_Manager\Data\" + namefile + ".txt" For Input As #3
    Line Input #3, entry
    Text4 = entry
    Close #3

    But the problem is still: How can I get the "data/text" from label1 to the label1 agean when I load. For example, if label1 = 3000 when I save, I whant it to be 3000 when I load it agean.
    Any one how can help?

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    First of all since you are combining the two lables together when you save them they will be combined when they are read which apparently is not what you want. You should use a delimiter to seperate them or better still assuming that the first label is a static caption and the second is the actual variable data simply write just the data fromt he second label to the file and then read it back into the program.

    Also if it were me I would use a label array to make it simplier

    Code:
    Dim FH as integer
    Dim DataIn as string
    Dim Counter as Integer
    FH=FreeFile
    Open Filename for input as #FH
    do while not EOF(FH)
       line input #fh,DataIn
       label1(Counter).Caption=DataIn
       Counter=Counter+1
    loop
    Close #FH

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Save game/Load game

    Definitively you need a delimiter to separate the two label contents.
    Look at this sample:
    Code:
    Private Sub SaveFile()
      Open App.Path & "\Saves\" + namefile + ".txt" For Output As #1
      Print #1, label5 + ";" + label1
      Print #1, label10 + ";" + label11
      Print #1, Label38 + ";" + Label39
      '...
      Close #1
    
    End Sub
    Private Sub LoadFile()
      Dim a$, b$()
      Open App.Path + "\Saves\" + namefile + ".txt" For Input As #1
      Line Input #1, a$
      b = Split(a$, ";")
      label5.Caption = b(0)
      label1.Caption = b(1)
      
      Line Input #1, a$
      b = Split(a$, ";")
      label10.Caption = b(0)
      label11.Caption = b(1)
      
      '...
      
      
    End Sub
    You are writing label followed by ";" followed by other label in a line.
    When reading back in, you read one line, split it up at the ";" and put the two parts back to the labels.

    This is some work, because you have to write a block
    Line Input #1, a$
    b = Split(a$, ";")
    label10.Caption = b(0)
    label11.Caption = b(1)
    for each pair of labels you have written to the file.

    If you'd follow dataMisers advice you'd better create an array or even better 2 arrays of labels. One for the name, one for the value.
    In your code you have 13 pairs of names/values.
    So you create tow arrays of labels: lblName and lblValue, each having 13 items.
    Then you can use code like this:
    Code:
    Private Sub SaveFile()
      Dim i%
      Open App.Path & "\Saves\" + namefile + ".txt" For Output As #1
      For i = 0 To lblName.UBound
          Print #1, lblName(i).Caption + "=" + lblValue(i).Caption
      Next
      Close #1
    
    End Sub
    
    Private Sub LoadFile()
      Dim a$, b$(), i%
      Open App.Path + "\Saves\" + namefile + ".txt" For Input As #1
      For i = 0 To lblName.UBound
          Line Input #1, a$
          b = Split(a$, "=")
          lblName(i).Caption = b(0)
          lblValue (i), Caption = b(1)
      Next
      Close #1
    End Sub
    In the file you will see lines which look like:
    valuename=10000
    Note that I have used "=" as a delimiter here.

  9. #9
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Quote Originally Posted by WoF View Post
    Definitively you need a delimiter to separate the two label contents.
    Look at this sample:
    Code:
    Private Sub SaveFile()
      Open App.Path & "\Saves\" + namefile + ".txt" For Output As #1
      Print #1, label5 + ";" + label1
      Print #1, label10 + ";" + label11
      Print #1, Label38 + ";" + Label39
      '...
      Close #1
    
    End Sub
    Private Sub LoadFile()
      Dim a$, b$()
      Open App.Path + "\Saves\" + namefile + ".txt" For Input As #1
      Line Input #1, a$
      b = Split(a$, ";")
      label5.Caption = b(0)
      label1.Caption = b(1)
      
      Line Input #1, a$
      b = Split(a$, ";")
      label10.Caption = b(0)
      label11.Caption = b(1)
      
      '...
      
      
    End Sub
    You are writing label followed by ";" followed by other label in a line.
    When reading back in, you read one line, split it up at the ";" and put the two parts back to the labels.

    This is some work, because you have to write a block
    Line Input #1, a$
    b = Split(a$, ";")
    label10.Caption = b(0)
    label11.Caption = b(1)
    for each pair of labels you have written to the file.

    If you'd follow dataMisers advice you'd better create an array or even better 2 arrays of labels. One for the name, one for the value.
    In your code you have 13 pairs of names/values.
    So you create tow arrays of labels: lblName and lblValue, each having 13 items.
    Then you can use code like this:
    Code:
    Private Sub SaveFile()
      Dim i%
      Open App.Path & "\Saves\" + namefile + ".txt" For Output As #1
      For i = 0 To lblName.UBound
          Print #1, lblName(i).Caption + "=" + lblValue(i).Caption
      Next
      Close #1
    
    End Sub
    
    Private Sub LoadFile()
      Dim a$, b$(), i%
      Open App.Path + "\Saves\" + namefile + ".txt" For Input As #1
      For i = 0 To lblName.UBound
          Line Input #1, a$
          b = Split(a$, "=")
          lblName(i).Caption = b(0)
          lblValue (i), Caption = b(1)
      Next
      Close #1
    End Sub
    In the file you will see lines which look like:
    valuename=10000
    Note that I have used "=" as a delimiter here.
    ok thanx
    But there comes and error when I load an save:
    "Subscript out of range"

    Here is the code I use:
    Dim a$, b$()
    Dim namefile As String
    namefile = Text2
    Open App.Path + "\Data\" + namefile + ".txt" For Input As #1
    Line Input #1, a$
    b = Split(a$, ";")
    Label5.Caption = b(0)
    Label1.Caption = b(1)
    Label10.Caption = b(2)
    Label11.Caption = b(3)
    Label38.Caption = b(4)
    Label39.Caption = b(5)
    Label31.Caption = b(6)
    Label33.Caption = b(7)
    Label32.Caption = b(8)
    Label34.Caption = b(9)
    Label42.Caption = b(10)
    Label43.Caption = b(11)
    Label19.Caption = b(12)
    Label24.Caption = b(13)
    Label17.Caption = b(14)
    Label25.Caption = b(15)
    Label16.Caption = b(16)
    Label26.Caption = b(17)
    Label20.Caption = b(18)
    Label27.Caption = b(19)
    Label21.Caption = b(20)
    Label28.Caption = b(21)
    Label22.Caption = b(22)
    Label29.Caption = b(23)
    Label23.Caption = b(24)
    Label30.Caption = b(25)

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    You are only reading one line, which has only 2 values but you are trying to assign many more than you have read hence the error.

    You should use label arrays here like both myself and wof have done in the examples.

    To create a pair of label arrays simply create your first 2 labels as usual then selected both labels and copy. Once you have done this click on the form and choose paste. It will tell you that you already have a label by this name, do you want to create an array where you will say yes. Continue to paste the label pair until you have the desired number.

    Once you have your array created properly then you will be able to use a loop like shown in the examples.

    That said there is not a problem if you want to store the static text in the file but if it were me I would store only the data.

  11. #11
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    Using the method you have tryed you would need to do it this way

    Code:
    Line Input #1, a$
    b = Split(a$, ";")
    Label5.Caption = b(0)
    Label1.Caption = b(1)
     
    Line Input #1, a$
    b = Split(a$, ";")
    Label10.Caption = b(0)
    Label11.Caption = b(1)
     
    Line Input #1, a$
    b = Split(a$, ";")
    Label38.Caption = b(0)
    Label39.Caption = b(1)
     
    'contiue for each label pair
    Note there are only 2 values on any given line b(0) and b(1) assuming of course that when you saved the file you used the ; as a delimiter between the values.


    On another note the code you have will work if you change the way you write the file

    Code:
    Print #1, label5 + ";" + label1 +";";
    Print #1, label10 + ";" + label11 +";";
    Print #1, Label38 + ";" + Label39 +";";
    When you get to the last pair do not add the +";"; part on the end. The result will be a file with only one line that has all you data in it but is much harder to read
    Last edited by DataMiser; September 2nd, 2009 at 10:33 AM.

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Save game/Load game

    That's right DataMiser. If he would print out all labels separated with ";" his reading code would work perfectly.
    Even if you print the last item with a ";", too. It doesn't matter, because the last empty item created by the split is not reassigned to any label.

  13. #13
    Join Date
    Sep 2009
    Posts
    126

    Re: Save game/Load game

    Hello once more!
    Sorry for that I'm asking simple questions but I'm very noob on VB, but only in this tropic I have learn a lot! So thank you all!
    But when I put in the code Datamiser wrote there comes up and new error:
    Run time error '62': Inout past end of file
    anyone that can help me once agean?

  14. #14
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,875

    Re: Save game/Load game

    He didn't WRITE that file. Apparently you changed something.

    Post the code that you use to SAVE the file. That might be enough to either spot the error, or correct how to read it back.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  15. #15
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,786

    Re: Save game/Load game

    Quote Originally Posted by Equx View Post
    But when I put in the code Datamiser wrote there comes up and new error:
    Run time error '62': Inout past end of file
    anyone that can help me once agean?
    The two pieces of code I posted in that last post is an either or type of thing if you used both then this error should be expected as you would be writing only one line but trying to read more than one line.

    When you use Print #1,VariableName it writes a line to the open file and adds a CRLF on the end

    When you use Print #1,VariableName; it writes the data to the current position of the file but does not add a crlf at the end.

Page 1 of 3 123 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center