dcsimg
Results 1 to 5 of 5

Thread: Looping an Error Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Looping an Error Problem

    Hi everyone,
    I have been working on a game/small engine for the game, and what part of it is, is to edit the game maps, where you can change the tiles on the ground to different images. However due to a feature to keep things looking the same by randomizing the images from the same type of image. However some will have more randoms than others. I knew that if the random number generated a number higher than the amount of that image type, then it will produce an Error 53, file not found. So my idea on fixing it would be to simply keep repeating the random number till is gets one that works, using the error handler to jump back and retry.
    The problem is that it will jump back once only, after that it wont use the error handler.
    This is the code.

    Code:
    Public Function ChangeTile(ByVal Index As Integer, ByVal ChangeDirection As Integer)
    
    Retry:
    On Error GoTo Cleanup
    
    Dim RndImg As Integer
    Dim Xval As Integer
    Dim Yval As Integer
    Dim Error As Byte
    
    Xval = Index Mod Map.SizeX
    Yval = Index \ Map.SizeX
    
    Error = 1
    If ChangeDirection > 0 Then
        Map.Tile(Xval, Yval).Type = Map.Tile(Xval, Yval).Type + 1
    ElseIf ChangeDirection < 0 Then
        Map.Tile(Xval, Yval).Type = Map.Tile(Xval, Yval).Type - 1
    End If
    ChangeDirection = 0
    Error = 2
    RndImg = Random(0, 3)
    frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-" & RndImg & ".gif")
    
    Exit Function
    Cleanup:
    
    Err.Clear
    If Error = 1 Then
        Map.Tile(Xval, Yval).Type = 0
        RndImg = Random(0, 3)
        frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-" & RndImg & ".gif")
    ElseIf Error = 2 Then
        GoTo Retry
    End If
    
    End Function
    Is there are way to fix it so it will repeat, or is there another way of fixing it without knowing how many files there really is?
    Thanks in Advance.

    Regards
    Michael

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Looping an Error Problem

    I have changed the code a bit, and this works better, however it doesn't solve my problem.

    Code:
    Public Function ChangeTile(ByVal Index As Integer, ByVal ChangeDirection As Integer)
    
    Dim RndImg As Integer
    Dim Xval As Integer
    Dim Yval As Integer
    Dim Error As Byte
    On Error GoTo Cleanup
    
    Xval = Index Mod Map.SizeX
    Yval = Index \ Map.SizeX
    
    Error = 1
    If ChangeDirection > 0 Then
        Map.Tile(Xval, Yval).Type = Map.Tile(Xval, Yval).Type + 1
    ElseIf ChangeDirection < 0 Then
        Map.Tile(Xval, Yval).Type = Map.Tile(Xval, Yval).Type - 1
    End If
    frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-0.gif")
    ChangeDirection = 0
    
    Retry:
    On Error GoTo Cleanup
    Error = 2
    RndImg = Random(0, 3)
    frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-" & RndImg & ".gif")
    
    Exit Function
    Cleanup:
    
    Err.Clear
    If Error = 1 Then
        Map.Tile(Xval, Yval).Type = 0
        frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-0.gif")
        GoTo Retry
    ElseIf Error = 2 Then
        GoTo Retry
    End If
    
    End Function
    Last edited by mixtah; Oct 17th, 2010 at 07:33 AM. Reason: Again, just updated the code, was only removing a useless line.

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Looping an Error Problem

    Instead of randomizing the file name like here

    frmMain.MapTile(Index).Picture = LoadPicture("C:\Documents and Settings\Mike\My Documents\RPG Game\Images\MAP" & Map.Tile(Xval, Yval).Type & "-" & RndImg & ".gif")

    Use Dir function (or FSO, or FiliListBox) to load all files to a list, then randomize a number in the range of the number of items in the list and based on that use a file from the list.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Looping an Error Problem

    It's quite a good idea, I like it.
    I've used filelistbox's before, one thing I don't know how to do is to specify the specific filename as well, since showing the directory will show lots of images, all but 1 or 2 will be just clutter.
    for instance I'll need to specify that i the filename it will be
    "Map" & Map.Tile(Xval, Yval).Type & "-"

    which I'm sure it's not hard, however something which I don't even know how to do, so I doubt the answer will be that obvious.
    I'll look it up now, however if you know the answer, mind just telling me.
    thanks

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: Looping an Error Problem

    actually don't worry, I made a function which searched for any string you put in, used a loop and added 1 to the function every time it found one.
    thanks for the idea.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width