Redimming a Structure
Redimming a Structure
Redimming a Structure
Redimming a Structure
Redimming a Structure
Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure
Redimming a Structure Redimming a Structure
Redimming a Structure
Go Back  Xtreme Visual Basic Talk > > > Redimming a Structure


Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2012, 03:03 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default Redimming a Structure


I can Redim a structure just fine like:

Code:
Structure sGame
Public Walkable as Boolean
Public Xpos as Integer
Public Ypos as Integer
Then i could Redim that structure like:

Code:
Dim Game(,) as sGame
Redim Game(2,3)
But has anyone every heard of Redimming the whole
structure that has already been redimensioned?

Like:

Code:
Redim(Game(2,3), 2))
This would be used to hold game maps in memory
before they are saved to file.

To start out with, you would only have one Redimmed
Game Structure but if you wanted to create another map,
without saving the first one, you would need another Game
structure for the next map, so i thought that the only
way to solve that problem, would be to have the whole
Redimmed game structure, Redimmed into an array.

I hope this makes sense, because i'm confused to. LOL
Reply With Quote
  #2  
Old 11-16-2012, 05:28 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

You can disregard this post. I see it isn't possible.
Thank-you
Reply With Quote
  #3  
Old 11-16-2012, 11:34 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

You know me. I had to play around with this idea
and i think that i stumbled across something.

It looks like it is possible to Redim a structure, even
when there is data in it but not quite.

I started out with a small structure and dimmed a
structure array with the first element filled in with
a number. The first number will represent how many
times that i can create a new instance of that structure
array. The last 3 elements will hold the layer number of
the map and the 2 - x amd y coordinates.

Code:
Private Structure sGame
      Public Layer As Integer
      Public tIndex As Integer
End Structure

' 50 is the number of times
' that i can create a new instance 
' of the array.

Dim Map(50)(,,) As sGame
I put the rest of the code in a button:

Code:
 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        ' Arrays start at 0, so this
        ' will be the first instance of
        ' the array.

        ReDim Map(0)(1, 1, 1)

        Dim tAcross As Integer
        Dim tDown As Integer
        Dim w, h As Integer

        ' Layer 1 of map, 2nd 0 from left

        Map(0)(0, 0, 0).Layer = 1
        Map(0)(0, 0, 0).tIndex = 1

        Map(0)(0, 0, 1).Layer = 1
        Map(0)(0, 0, 1).tIndex = 2

        Map(0)(0, 1, 0).Layer = 1
        Map(0)(0, 1, 0).tIndex = 3

        Map(0)(0, 1, 1).Layer = 1
        Map(0)(0, 1, 1).tIndex = 4

       ' Next i Redimmed the array
       ' again but with this one being
       ' the 2nd instance of the array.

        ReDim Map(1)(1, 1, 1)

        ' Layer 2 - 2nd 1 from left
        Map(1)(1, 0, 0).Layer = 2
        Map(1)(1, 0, 0).tIndex = 1

        Map(1)(1, 0, 1).Layer = 2
        Map(1)(1, 0, 1).tIndex = 2

        Map(1)(1, 1, 0).Layer = 2
        Map(1)(1, 1, 0).tIndex = 3

        Map(1)(1, 1, 1).Layer = 2
        Map(1)(1, 1, 1).tIndex = 4


        tAcross = 2
        tDown = 2

        ' I put all the values from the 2nd instance of the array
        ' into a textbox first. I wanted to see if i lost anything
        ' from the first instance of the tile array.

        For w = 0 To tAcross - 1
            For h = 0 To tDown - 1
                TextBox1.AppendText("Map(1)(1," & w & "," & h & ").Layer = " & Map(1)(1, w, h).Layer & vbCrLf)
                TextBox1.AppendText("Map(1)(1," & w & "," & h & ").tIndex = " & Map(1)(1, w, h).tIndex & vbCrLf)
            Next
        Next

        TextBox1.AppendText(vbCrLf)

        tAcross = 2
        tDown = 2

        ' Here is the first instance of the 
        ' array, being put into a textbox.

        For w = 0 To tAcross - 1
            For h = 0 To tDown - 1
                TextBox1.AppendText("Map(0)(0," & w & "," & h & ").Layer = " & Map(0)(0, w, h).Layer & vbCrLf)
                TextBox1.AppendText("Map(0)(0," & w & "," & h & ").tIndex = " & Map(0)(0, w, h).tIndex & vbCrLf)
            Next
        Next
    End Sub

The outcome was great. All the numbers that were
put into the textbox were correct. Now i guess i need
to put this into real use and see if it will handle multiple
maps, without being saved.

I'm worried about this being a memory hog though.
What do you all this about it?

I only did 2 instances of the game array but it can do
up to 50.
Reply With Quote
  #4  
Old 11-16-2012, 11:40 PM
passel's Avatar
passelRedimming a Structure passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

{edit: I see you've figured out the third method I mention below, preallocating an array to "point" to arrays that you redim when needed. }

You could have your map array inside another structure or class, then have an array of those.
Code:
  Structure TileType
    Public Walkable As Boolean
    Public Xpos As Integer
    Public Ypos As Integer
  End Structure

  Structure MapType
    Public Map(,) As TileType
  End Structure

  Dim Maps(0) As MapType

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ReDim Maps(0).Map(15, 10)  'Make Map slot 0 a map of 16x11 tiles

    Maps(0).Map(0, 0).Walkable = True    'set a couple of items in map(0) for testing
    Maps(0).Map(0, 1).Walkable = True
  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ReDim Preserve Maps(1)     'preserve the Map(0) values
    ReDim Maps(1).Map(30, 30)  'make maps(1) map a 31x31 size

    Maps(1).Map(20, 30).Walkable = True
    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0).Map(0, 1).Walkable)       'Map 0 still ok
    Debug.Print("map 1, (20,30).Walkable = {0}", Maps(1).Map(20, 30).Walkable)
    Debug.Print("Map 0, (1,1).Walkable = {0}", Maps(0).Map(1, 1).Walkable)       'Show an unset value is false, just for an output difference

    Maps(1).Map = New TileType(,) {}  'empty the Maps1 array
    ReDim Preserve Maps(0)
    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0).Map(0, 1).Walkable)   'Map 0 still ok
  End Sub
You could use a ragged array (array of arrays)
Code:
  Structure TileType
    Public Walkable As Boolean
    Public Xpos As Integer
    Public Ypos As Integer
  End Structure

  Dim Maps()(,) As TileType

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ReDim Maps(0)
    ReDim Maps(0)(20, 15)

    Maps(0)(0, 0).Walkable = True    'set a couple of items in map(0) for testing
    Maps(0)(0, 1).Walkable = True

  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ReDim Preserve Maps(1)
    ReDim Maps(1)(30, 30)

    Maps(1)(20, 30).Walkable = True
    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0)(0, 1).Walkable)       'Map 0 still ok
    Debug.Print("map 1, (20,30).Walkable = {0}", Maps(1)(20, 30).Walkable)
    Debug.Print("Map 0, (1,1).Walkable = {0}", Maps(0)(1, 1).Walkable)       'Show an unset value is false, just for an output difference

    ReDim Preserve Maps(0)
    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0)(0, 1).Walkable)   'Map 0 still ok

  End Sub
You could preallocate the "slots" of the maximum number of concurrent maps you wanted in memory, and you wouldn't have to Redim Preserve since you wouldn't Redim the main array, only the second level 2d array. The main array wouldn't take much memory, since it would essentially only be pointers to the 2d arrays, and you can allocate and deallocate the memory taken by the 2d arrays by "emptying" them as shown.
Code:
  Structure TileType
    Public Walkable As Boolean
    Public Xpos As Integer
    Public Ypos As Integer
  End Structure


  Dim Maps(3)(,) As TileType    'Allow up to 4 maps in memory at once

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ReDim Maps(0)(20, 15)

    Maps(0)(0, 0).Walkable = True    'set a couple of items in map(0) for testing
    Maps(0)(0, 1).Walkable = True

  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ReDim Maps(1)(30, 30)

    Maps(1)(20, 30).Walkable = True
    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0)(0, 1).Walkable)       'Map 0 still ok
    Debug.Print("map 1, (20,30).Walkable = {0}", Maps(1)(20, 30).Walkable)
    Debug.Print("Map 0, (1,1).Walkable = {0}", Maps(0)(1, 1).Walkable)       'Show an unset value is false, just for an output difference

    Maps(1) = New TileType(,) {}  'empty the Maps1 array

    Debug.Print("Map 0, (0,1).Walkable = {0}", Maps(0)(0, 1).Walkable)   'Map 0 still ok
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 11-16-2012 at 11:46 PM.
Reply With Quote
  #5  
Old 11-17-2012, 09:35 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

I'm liking the last one you presented to me.
It's similar to my original code but all 3
examples, do the same thing.

I'm going to make a small demo, to see
what you think. Your expert opinion is
important to me.

That catch is though, is that you will have
to draw your own maps, since the idea was to
hold map data in memory, without saving them
first.

Thanks for your time and efforts.
Reply With Quote
  #6  
Old 11-19-2012, 01:22 AM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

OK. I got a working example of an
arrayed game map structure.

As it turns out, that part wasn't to bad
but i wanted to use a treeview control
in this example and that gave me hell.

I couldn't figure out where to store some needed
data, so that i could reference it later. I didn't
realize that a tree node had a tag property attached
ti it.

OK. About the example:

This example is in a game map editor style
but it isn't one yet. The only thing it does
right now is to hold bitmaps and map arrays
in memory. After all, that was the reason that
i came here for help to begin with. Other than
that, you can fill a newly created map with a
selected tile.

I just wanted to post this to show that it can
be done. Thanks for your help Passel.
Attached Files
File Type: zip RedimStructure.zip (89.7 KB, 2 views)
Reply With Quote
  #7  
Old 11-19-2012, 03:27 PM
bokeh bokeh is offline
Regular
 
Join Date: Aug 2012
Posts: 60
Default Some extra treeview code..

I noticed running the attachment to your last post that the TreeView control was highlighting anything when the form was loaded.

I haven't played around much with selecting treeview controls programmatically so i tried making up a sub:
Code:
Private Sub SelectAlteriaNode_OutputNodeText()
    TreeView1.Select()
    nodSelected = TreeView1.GetNodeAt(60, 27) 'Select Alteria Node
    TreeView1.SelectedNode = nodSelected
    Label1.Text = "Current Index = " & CurrentIndex : Label2.Text = "Tiles Across = " & TilesAcross
    Label3.Text = "Tiles Down = " & TilesDown : Label4.Text = "Node Tag Data = " & nodSelected.Tag
End Sub
Also it was confusing to me the way the picTiles MouseMove event kept moving around the yellow selection rectangle.
I had a hard time telling which tile was going to be used by FillMap sub when you pressed the button.

So I got rid of the picTile MouseMove and instead of using moving the CreateGraphics code to the picTiles MouseDown event I used this way of coding instead:
Code:
Private Sub picTiles_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picTiles.MouseDown
    Dim intX, intY As Integer
    intX = Int(e.X \ 32) : intY = Int(e.Y \ 32) ' Gets x and y co-ordinates of locator
    srcRect.X = intX * 32
    srcRect.Y = intY * 32 ' Set the dest x and x rect locations for selected tile.
    picTiles.Refresh() ' Refresh picturebox to update yellow square drawing 
End Sub

Private Sub picTiles_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles picTiles.Paint
    YellowPen = New Pen(Brushes.Yellow, 4) ' Put pen properties into YellowPen
    e.Graphics.DrawRectangle(YellowPen, srcRect.X, srcRect.Y, 32, 32)
End Sub
Also with a minor change the CreateSrcBitmap() sub wasn't needed anymore.
Don't like scrolling much - so I condensed the code and enlarge the split panel to show all tiles at once.
Attached Files
File Type: zip RedimStructurePlus.zip (50.0 KB, 1 views)

Last edited by bokeh; 11-19-2012 at 03:33 PM.
Reply With Quote
  #8  
Old 11-19-2012, 10:37 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Thanks for your input Bokeh. This is not a finished
program. That is why you are finding some faults
in it. I was just demonstrating mainly on how to
redimension a map structure array, so that it could
hold many maps in memory, with out the need to
save them first.

I never did get around to highlighting a newly created
map in the treeview. My concern was on the map array.

I'll check out your code. Thanks....

Yeah. I could have put the yellow rectangle code in the
mouse_down event but for some reason, i didn't. Not sure
if i want to do it that why yet.

Last edited by passel; 11-20-2012 at 02:56 AM. Reason: added the word "not", figuring this is "not a finished program"
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Redimming a Structure
Redimming a Structure
Redimming a Structure Redimming a Structure
Redimming a Structure
Redimming a Structure
Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure Redimming a Structure
Redimming a Structure
Redimming a Structure
 
Redimming a Structure
Redimming a Structure
 
-->