Tile Engine "Clicking" unwanted tiles

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 864 Views - Last Post: 02 May 2013 - 01:18 PM Rate Topic: -----

#1 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 03:35 AM

This seems random, but when you click a tile it recognizes the click and it is the right tile. However when the data gets passed to "ToolHandler.vb" it seems to effect that tile and a few around it of the same type.

ClickTile(Handles All Tile Clicks)
Private Sub ClickTile(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim Clicked As PictureBox = sender
        Dim P As Point = Clicked.Tag
        Dim X As Integer = P.X
        Dim Y As Integer = P.Y
        Debug.WriteLine("Tile Has Type of: " & CurrentMap.Tiles(X, Y).Type)
        If CurrentMap.Tiles(X, Y).Type = 1 Then
            ToolHandler.Mine(Inventory.GetFirstTool(1), X, Y)
            Exit Sub
        ElseIf CurrentMap.Tiles(X, Y).Type = 2 Then
            ToolHandler.Chop(Inventory.GetFirstTool(2), X, Y)
            Exit Sub
        End If
    End Sub



ToolHandler(Take tile information and changes it)
    Public Shared Sub Mine(ByVal Tool As Item, ByVal TileX As Integer, ByVal TileY As Integer)
        If Tool.Type = 1 Then 'Is Pick
            If Tool.Hardness >= Overworld.CurrentMap.Tiles(TileX, TileY).Strength Then
                'Give Items
                For Each Drop As Item In Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems
                    Dim Ind = Array.IndexOf(Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems, Drop)
                    Inventory.AddInv(Drop.Name, Overworld.CurrentMap.Tiles(TileX, TileY).DroppedNum(Ind))
                Next
                Dim TName As String = Overworld.CurrentMap.Tiles(TileX, TileY).Name
                Select Case TName
                    Case "Grass"
                        Overworld.CurrentMap.Tiles(TileX, TileY).Name = "DeadGrass"
                        Overworld.CurrentMap.Tiles(TileX, TileY).Type = 0
                        Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems = Nothing
                        Overworld.CurrentMap.Tiles(TileX, TileY).DroppedNum = Nothing
                    Case "Mountain"
                        Overworld.CurrentMap.Tiles(TileX, TileY).Name = "Empty"
                        Overworld.CurrentMap.Tiles(TileX, TileY).Type = 0
                        Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems = Nothing
                        Overworld.CurrentMap.Tiles(TileX, TileY).DroppedNum = Nothing
                End Select
            End If
        End If
        Overworld.UpdateTile(TileX, TileY)
    End Sub
    Public Shared Sub Chop(ByVal Tool As Item, ByVal TileX As Integer, ByVal TileY As Integer)
        If Tool.Type = 2 Then 'Is Axe
            If Tool.Hardness >= Overworld.CurrentMap.Tiles(TileX, TileX).Strength Then
                'Give Items
                For Each Drop As Item In Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems
                    Dim Ind = Array.IndexOf(Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems, Drop)
                    Inventory.AddInv(Drop.Name, Overworld.CurrentMap.Tiles(TileX, TileY).DroppedNum(Ind))
                Next
                With Overworld.CurrentMap.Tiles(TileX, TileY)
                    Debug.WriteLine("Changing Tile: " & TileX & "," & TileY & "  to DeadForest,  From:" & .Name)
                    .Name = "DeadForest"
                    .DroppedItems = Nothing
                    .DroppedNum = Nothing
                    .Type = 0
                End With
                'If Overworld.Visible = True Then
                Debug.WriteLine("Sending Updating Tile: " & TileX & "," & TileY)
                Overworld.UpdateTile(TileX, TileY)
                'End If
            End If
        End If
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Tile Engine "Clicking" unwanted tiles

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 06:46 AM

A few thoughts:

If it's happening with both ToolHandler.Mine() and ToolHandler.Chop(), I would say it's likely to be a problem in something common to both Subroutines; perhaps in Overworld.UpdateTile(), or perhaps something you are doing the same (in the ForEach, for example).

Have you set breakpoints and followed the code, checking on the contents of variables and checking to make sure your program is doing what you think you told it to do?

If you aren't familiar with that technique, see our Debugging Tutorial.

As well, If you don't have Option Strict On set, it's worth having a look at the link about it in my signature.
Was This Post Helpful? 0
  • +
  • -

#3 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 01:00 PM

Using breakpoints Ive had trouble narrowing down where the problem is at. I have a feeling it is within my ToolHandler. For reference the UpdateTile Sub is:

Public Sub UpdateTile(ByVal X, ByVal Y)
        CurrentMap.Tiles(X, Y).UpdateImage()
        LocalTiles(X, Y).Image = CurrentMap.Tiles(X, Y).Image
        Me.Controls.Remove(LocalTiles(X, Y))
        Me.Controls.Add(LocalTiles(X, Y))
    End Sub



I think the problem is after the UpdateTile because the unclicked tiles that also change to Type 0 dont have updated images.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 01:43 PM

Follow the logic of that code, does it make sense? This is what is tells me.

Update the tile,
Change the tile,
Remove the tile,
Add the tile back.

Does that seem like the right order?
Was This Post Helpful? 0
  • +
  • -

#5 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 01:49 PM

Forgot to clarify that the array LocalTiles is in fact the pictureboxes I use for the grid of Tiles. So I update the actual tile in the current map which is a custom class "Tile". UpdateImage loads the right image based on its name into its own Image property. Next the old outdated "Tile"(Picturebox) is removed and the updated one is added in its place.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 01:53 PM

You don't have to remove and add. It sounds like all you need to do is change the tile image and your done.
Was This Post Helpful? 0
  • +
  • -

#7 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 02:00 PM

I didnt know that the Local control and the one on the form would update themselves or if I had to re-add them. I dont think that's the problem with the random updates but Ill try it.

Edit: Turns out I dont need to re-add but still have that problem with surrounding tiles being changed to type 0.

This post has been edited by AnimeWarrior: 01 May 2013 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 02:15 PM

What happens when you debug your code? Do you follow it long enough to see where it is changing the other tiles? Something has to be changing them - you will find it if you look hard enough!
Was This Post Helpful? 0
  • +
  • -

#9 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 02:24 PM

somehow the change occurs after the UpdateTile and the end of the Tile Click Handler. But thats makes no sense since there is no code except End ifs... To debug the code I had it print out all the Tile types in the debug window at different points in the code. Im at a loss to explain why its happening.
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 01 May 2013 - 06:39 PM

View PostAnimeWarrior, on 01 May 2013 - 03:24 PM, said:

somehow the change occurs after the UpdateTile and the end of the Tile Click Handler. But thats makes no sense since there is no code except End ifs... To debug the code I had it print out all the Tile types in the debug window at different points in the code. Im at a loss to explain why its happening.

Are the images updating as you debug? Some controls don't update when stepping through the code. When this happens to me, I like to place an "Application.DoEvents" after the line that changes a control. It lets the system update it, and I can see more clearly what's happening, and when it's happening.

Suggestion. If you can reproduce the problem explicity, you can possibly zero in on at least when it's happening. What I mean is that if, for example, you click on Tile(4,5), and Tile (4,6) always gets set to Type 0, you can throw in some Debug.Print statements to print the Type of Tile(4,6). You can scatter them around to try to narrow it down, and of course, you can identify which Debug.Print statement is being executed by including an identifying string along with the Type.

You could also put an innocuous statement in all your routines that you can check while stepping through. Say your Tile(4,6) is changing when an adjacent Tile is clicked. In each of your routins (all the ones you go through for a click), you could place a statement like Dim temp as Integer = Tile(4,6).Type (if you can see it from within the Subroutine). If you can't see it, perhaps you could pass it as an extra (temporary) argument.

When you re-add a PictureBox, are you sure of the array indices you are placing it in, and are you sure the .Tag is pointing at the right positions?

I don't know if your program is commercial or not, but if you get too frustrated, you might want to consider either uploading the whole project, or if you don't want to expose it to the public, make arrangements with someone you trust who could help.
Was This Post Helpful? 0
  • +
  • -

#11 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 02 May 2013 - 03:50 AM

This isnt commercial. The project is attached to this post. Maybe someone can find what I've been missing the past few days.

Attached File(s)


This post has been edited by AnimeWarrior: 02 May 2013 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 02 May 2013 - 10:47 AM

View PostAnimeWarrior, on 02 May 2013 - 04:50 AM, said:

This isnt commercial. The project is attached to this post. Maybe someone can find what I've been missing the past few days.

I have to go out for a while, but I can tell you that the problem is in UpDateAllTiles() or one of the routines it calls.
In the latest test I did, I clicked on the forest at 1,0 (x.y), and my file dump showed that worked, changing Forest to DeadGrass, but during UpdateAllTiles(), it also changed 0,0 and 2,0 the same way. I suspect that the Name ot Type is somehow messing up, but can't be sure yet. I'll play with it some more when I get back in.
Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 02 May 2013 - 01:00 PM

I spoke too soon. I wrote a little routine to dump the .Type of the first two rows of the Tiles, and called the dump from Chop(), twice. It looks like the problem is in the Chop(), in the With/End With block.

Here's the Chop Sub...

    Public Shared Sub Chop(ByVal Tool As Item, ByVal TileX As Integer, ByVal TileY As Integer)
        If Tool.Type = 2 Then 'Is Axe
            If Tool.Hardness >= Overworld.CurrentMap.Tiles(TileX, TileX).Strength Then
                'Give Items
                For Each Drop As Item In Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems
                    Dim Ind = Array.IndexOf(Overworld.CurrentMap.Tiles(TileX, TileY).DroppedItems, Drop)
                    Inventory.AddInv(Drop.Name, Overworld.CurrentMap.Tiles(TileX, TileY).DroppedNum(Ind))
                Next
                Overworld.dump(vbNewLine & "In Chop, before With")
                With Overworld.CurrentMap.Tiles(TileX, TileY)
                    Debug.WriteLine("Changing Tile: " & TileX & "," & TileY & "  to DeadForest,  From:" & .Name)
                    Overworld.sw.Write(" " & "Changing Tile: " & TileX & "," & TileY & "  to DeadForest,  From:" & .Name)
                    .Name = "DeadForest"
                    .DroppedItems = Nothing
                    .DroppedNum = Nothing
                    .Type = 0
                End With
                Overworld.dump(vbNewLine & "In Chop, after End With")
                'If Overworld.Visible = True Then
                'Debug.WriteLine("Sending Updating Tile: " & TileX & "," & TileY)
                Overworld.sw.WriteLine(vbNewLine & "Updating from Chop()")

                Overworld.dump(vbNewLine & "Entering UpdateTile")

                Overworld.UpdateTile(TileX, TileY)
                'End If
            End If
        End If
    End Sub



In case you want it, here's the declaration of sw, and the dump() routine (both in OverWorld.vb). Also, the sw.Close() and .Dispose()

To use it, I just run, start a new game, look for more than 1 adjacent Forests in the top row, and click on one. You'll find the output file in your Debug directory.

    Public sw As New StreamWriter("Test.txt")

    Public Sub dump(ByVal header As String)
        sw.WriteLine(header)
        For i = 0 To 15
            sw.Write(CurrentMap.Tiles(i, 0).Name & " ")
        Next
        sw.WriteLine("")
        For i = 0 To 15
            sw.Write(CurrentMap.Tiles(i, 1).Name & " ")
        Next
        sw.WriteLine(vbNewLine)

    End Sub

    Private Sub Overworld_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        sw.Close()
        sw.Dispose()
    End Sub




And here are the results...

Quote

In Chop, before With
Forest Forest Forest Grass Grass Grass Mountain Mountain Mountain Forest Forest Forest Forest Grass Grass Mountain
Forest Forest Forest Grass Grass Grass Grass Mountain Forest Forest Forest Forest Grass Mountain Mountain Mountain

Changing Tile: 1,0 to DeadForest, From:Forest

In Chop, after End With
DeadForest DeadForest DeadForest Grass Grass Grass Mountain Mountain Mountain Forest Forest Forest Forest Grass Grass Mountain
Forest DeadForest Forest Grass Grass Grass Grass Mountain Forest Forest Forest Forest Grass Mountain Mountain Mountain

Updating from Chop()

Entering UpdateTile
DeadForest DeadForest DeadForest Grass Grass Grass Mountain Mountain Mountain Forest Forest Forest Forest Grass Grass Mountain
Forest DeadForest Forest Grass Grass Grass Grass Mountain Forest Forest Forest Forest Grass Mountain Mountain Mountain

I clicked on Tile(1,0), and as you can see, it changed the ones on each side and the one underneath it.

Currently having a hard time figuring out how the code in the With could change other Tiles.
Was This Post Helpful? 0
  • +
  • -

#14 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 02 May 2013 - 01:09 PM

Update: It's the .Name = "DeadForest" that's doing it. You may have a problem in your code that creates the array of tiles, or perhaps one of the classes that handles the Tiles.
Was This Post Helpful? 1
  • +
  • -

#15 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: Tile Engine "Clicking" unwanted tiles

Posted 02 May 2013 - 01:12 PM

UpdateTile only updates the pictureboxes based on the tile. Turns out my tiles were "linking" during World Generation. If you notice, I have TempTile Dimmed as a new tile and then set to the proper tile. But any other bits added around that tile would reference that tile. I fixed it by resetting TempTile to New Tile.

Thanks for your help, if I have any more problems Ill post them here.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2