Mirage Source

Free ORPG making software.
It is currently Fri Dec 19, 2025 3:46 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Adjusting an array
PostPosted: Wed Apr 29, 2009 2:59 am 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: [email protected]
Here's some code to adjust an array. This could mean boundless / dynamic arrays...

The problem with them is for example:

Player(1 To 1)

When a player logs in it fills the first data, then a second comes in and you do ReDim Player(1 To UBound(Player) + 1) and you just have to loop through the UBound of the Player array...however, here is where adjusting an array comes in.

Say for example player 2 leaves, you just do ReDim Preserve Player(1 To UBound(Player) - 1)...well what if Player 1 leaves? If you just get rid of the last part of the array it will leave the player 1 data and clear the player 2...so you have to switch the array around. Here's a form giving an example of rearranging an array I made:

http://mayhem.auburnflame.com/frmArrayTest.frm

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
 Post subject: Re: Adjusting an array
PostPosted: Wed Apr 29, 2009 6:44 pm 
Offline
Pro
User avatar

Joined: Tue Nov 13, 2007 2:42 pm
Posts: 509
You should learn to use "CopyMemory" api and copy the parts of the array.

Later tonight I'll write up a bit of code to show how it's done.


Top
 Profile  
 
 Post subject: Re: Adjusting an array
PostPosted: Wed Apr 29, 2009 9:34 pm 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: [email protected]
Yeah I'm sure there's better ways...I never use CopyMemory / ZeroMemory because I haven't really studied up on them yet.

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
 Post subject: Re: Adjusting an array
PostPosted: Thu Apr 30, 2009 3:50 pm 
Offline
Pro
User avatar

Joined: Tue Nov 13, 2007 2:42 pm
Posts: 509
Here's a real quick class that will use a byte array. You can insert, insert at certain indexes and remove at certain indexes.

I kinda tested it, if you see any problems let me know.

Code:
' **************
' * By : Dugor *
' **************

Option Explicit

' Our copy memory API call
Private Declare Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private bArr() As Byte      ' A byte array we are using
Private Size As Long        ' The size of the array

Private Sub Class_Initialize()
    Size = -1
End Sub

' Will insert the value at the end of the array
Public Sub Insert(ByRef Value As Byte)
    Size = Size + 1
    ReDim Preserve bArr(Size)
    bArr(Size) = Value
End Sub

' Will insert the value at the index you specify
' It will then move the rest of the array down
Public Sub InsertAt(ByRef Index As Long, ByRef Value As Byte)
    If Index < 0 Then Exit Sub
   
    ' If the index you're trying to insert at is above our size
    ' just insert it at the end
    If Index > Size Then
        Insert Value
    Else
        Size = Size + 1
        ReDim Preserve bArr(Size)
        CopyMemory bArr(Index + 1), bArr(Index), Size - Index
        bArr(Index) = Value
    End If
End Sub

' Will remove a part of the array
Public Sub Remove(ByRef Index As Long)
    If Index < 0 Then Exit Sub
   
    ' If this is the last element we have to handle it different
    If Index = Size Then
        Size = Size - 1
        ReDim Preserve bArr(Size)
    Else
        If Index > Size Then Exit Sub
        CopyMemory bArr(Index), bArr(Index + 1), Count - Index
        Size = Size - 1
        ReDim Preserve bArr(Size)
    End If
End Sub

Public Function Count() As Long
    Count = (UBound(bArr) - LBound(bArr)) + 1
End Function

Public Sub Display()
Dim i As Long
    Debug.Print "Amount of items in array: ", Count
    For i = LBound(bArr) To UBound(bArr)
        Debug.Print "Index:"; i, "Value:", bArr(i)
    Next
End Sub


This is a quick example of how to use it.
Code:
Dim t As New clsArray
   
    t.Insert 1
    t.Insert 2
    t.Insert 8
    t.InsertAt 1, 5
    t.Display
   
    t.Remove 3
    t.Display
   
    t.InsertAt 4, 10
    t.Display


This is the output:
Quote:
Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 8
Amount of items in array: 3
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Amount of items in array: 4
Index: 0 Value: 1
Index: 1 Value: 5
Index: 2 Value: 2
Index: 3 Value: 10



Top
 Profile  
 
 Post subject: Re: Adjusting an array
PostPosted: Fri May 29, 2009 4:11 am 
Offline
Community Leader
User avatar

Joined: Sun May 28, 2006 10:29 pm
Posts: 1762
Location: Salt Lake City, UT, USA
Google Talk: [email protected]
copymemory and zeromemory = <3

_________________
I'm on Facebook! Google Plus LinkedIn My Youtube Channel Send me an email Call me with Skype Check me out on Bitbucket Yup, I'm an EVE Online player!
Why not try my app, ColorEye, on your Android devlce?
Do you like social gaming? Fight it out in Battle Juice!

I am a professional software developer in Salt Lake City, UT.


Top
 Profile  
 
 Post subject: Re: Adjusting an array
PostPosted: Fri May 29, 2009 5:03 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
The memory API is so fucking easy to use, and so much faster, I don't see why anyone wouldn't learn it.

I'll check your code out later tonight, see if it's better than the one I use myself.

_________________
Quote:
Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?


Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group