-2

I am making my game and i just added saving the world. This is my code:

Imports Game_Functions_VB.NET.Functions.VBFuncs
Imports System.IO
Namespace SaveMap
Public Class SaveMap
    Public Shared CurrentMap As Byte
    Public Shared Worlds(254) As String
    Public Shared MaxWorlds As Byte = 0
    Public Shared LMap As Boolean
    Public Shared SMap As Boolean
    Shared sw As StreamWriter
    Shared sr As StreamReader

    Shared Sub SaveMap(MapName As String)

        '   Try

        Directory.CreateDirectory("C:\Better Survival\Saves\" & MapName)
        Dim newfile As FileStream = File.Create("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")
        newfile.Close()
        sw = New StreamWriter("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")
        Dim strline As String = ""
        For Y As Short = 0 To 1024
            For X As Short = 0 To 1024
                strline = strline & Map(X, Y, 0) & ","
            Next
            sw.WriteLine(strline)
            strline = ""
        Next

        sw = New StreamWriter("C:\Better Survival\Saves\" & MapName & "\" & "data.mf")
        sw.WriteLine(LocX)
        sw.WriteLine(LocY)
        sw.Flush()
        sw.Close()
        sw.Dispose()
        SMap = True


        '  Catch ex As Exception
        '  MsgBox("Error saving the map.", MsgBoxStyle.Critical)
        SMap = False
        ' End Try

    End Sub

    Shared Sub LoadMap(MapName As String)
        Dim X As Short
        Dim Y As Short
        Dim gotit As Boolean

        If IO.File.Exists("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf") Then
            Try
                sr = New StreamReader("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")

                gotit = True
            Catch ex As Exception
                gotit = False
            End Try
        End If
        If gotit = True Then

            Try
                Dim strline As String = ""
                Do Until sr.EndOfStream
                    strline = sr.ReadLine
                    strline = strline.Replace(strline.LastIndexOf(","), "")
                    For Each item As String In Split(strline, ",", -1)
                        If item = "" Then
                            item = 0
                        End If

                        If X <= 1024 Then
                            Map(X, Y, 0) = Int(item)
                        End If
                        X = X + 1
                    Next
                    X = 0
                    Y = Y + 1
                Loop
                sr = New StreamReader("C:\Better Survival\Saves\" & MapName & "\" & "data.mf")
                Dim line As Byte = 0
                Do Until sr.EndOfStream
                    Select Case line
                        Case 0
                            LocX = sr.ReadLine
                        Case 1
                            LocY = sr.ReadLine
                    End Select
                    line += 1
                Loop
                LMap = True
                sr.Close()
                sr.Dispose()

            Catch ex As Exception
                MsgBox("Error loading the map.", MsgBoxStyle.Critical)
                LMap = False
            End Try
        Else
            MsgBox("The map does not exist", MsgBoxStyle.Critical)
        End If


    End Sub

    Shared Sub GetMaps()
        MaxWorlds = 0
        For t As Byte = 0 To 254
            Worlds(t) = ""
        Next

        Dim i As Byte = 0
        For Each File As String In My.Computer.FileSystem.GetDirectories("C:\Better Survival\Saves")
            Worlds(i) = RewritePath(File)
            i += 1
            MaxWorlds += 1
        Next
    End Sub

    Shared Sub ChangeMap()
        If CurrentMap < MaxWorlds - 1 Then
            CurrentMap = CurrentMap + 1
        End If
    End Sub

    Shared Sub ChangeMapLess()
        If CurrentMap > 0 Then
            CurrentMap = CurrentMap - 1
        End If
    End Sub

    Shared Function RewritePath(file As String) As String
        Dim line As String = ""
        If file <> "" Then

            For i As Byte = 25 To file.Length - 1
                line = line & file.Chars(i)
            Next
        End If
        Return line
    End Function

    Shared Sub DeleteWorld()
        Try
            Kill("C:\Better Survival\Saves\" & Worlds(CurrentMap) & "\" & Worlds(CurrentMap) & ".mf")
            Kill("C:\Better Survival\Saves\" & Worlds(CurrentMap) & "\" & "data.mf")
            Directory.Delete("C:\Better Survival\Saves\" & Worlds(CurrentMap))
            GetMaps()
        Catch ex As Exception
            MsgBox("Error deleting the world", MsgBoxStyle.Critical)
        End Try
    End Sub

    Shared Function CheckBeforeDeleting() As Boolean
        If MaxWorlds <> 0 Then
            Return True
        Else
            MsgBox("You have no worlds. How is it possible for me to delete one?", MsgBoxStyle.Exclamation)
            Return False
        End If
    End Function

    Shared Function CheckIfExists(MapName As String) As Boolean
        If IO.Directory.Exists("C:\Better Survival\Saves\" & Worlds(CurrentMap)) Then
            Return True
        End If
        Return False
    End Function

    Shared Function CheckIfMapExists(mapname As String) As Boolean
        If Directory.Exists(mapname) Then
            Return True
        End If

        Return False
    End Function
End Class
End Namespace

The sub cycling happens this way. While the game is in the world choosing menu, it executes constantly the GatMaps(). Then a world is loaded and it is shown in my screen. If i click saveworld when i dont use the "Try" the program says that that file is being used. I wasn't using this file in any other process so i gess it's from my game. What can it be? Thanks.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Do you have a specific reason why you create a FileStream before creating the StreamWriter? Also for disposable objects its usually preferable to use a using block so cleanup will happen automatically even if you run into an exception. – schudel Oct 22 '17 at 14:02
  • i removed it now but still the same error. the sw and the sr are now created in the subs but still the same error – A cool programmer Oct 22 '17 at 14:08
  • you are not closing your first StreamReader before you are assigning a new StreamReader to sr for data.mf directly after the loop. – schudel Oct 22 '17 at 14:13
  • i am not really experienced with this things. I just started saving files in the hdd yesterday and when i added multiple file sources i didnt know what to do. thanks – A cool programmer Oct 22 '17 at 14:16
  • so did it work? – schudel Oct 22 '17 at 14:20
  • Fully working. I needed to do it for the sw and the sr. How do i add the resolved tag to this question – A cool programmer Oct 22 '17 at 14:24
  • @Acoolprogrammer 1) There is no resolved tag. You can either delete the question (not good for reputation on this site if you're going to come back) or accept an answer. 2) An easy and reliable way of getting the streams disposed of when they are finished with is to use a [`Using`](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement) construct. The documentation has useful examples. – Andrew Morton Oct 22 '17 at 14:44

1 Answers1

0

you are not closing your first StreamReader before you are assigning a new StreamReader to sr for data.mf directly after the loop.

schudel
  • 1,235
  • 2
  • 11
  • 18