UDT vs Class
sdwI'm pretty sure I know the answer to this, but I'm gonna ask anyways :) Which is faster to use, a UDT or a class? I remember seeing somewhere that a UDT is faster. But is a class better to use in a game as, for example, a sprite? Right now I'm using a sprite class and create a new instance of it and add it to a collection everytime the game needs a new sprite. Would it be better for me to just use UDT array instead, seeing how I only use the Public variables of the class anyways?
SpodiUDT = Faster Class = Slower I have never used a class module before personally, and I've done quite a bit of stuff game-wise. Class modules can be good though. Like, for example, if you want to raise the user's exp, you can make sure it doesn't go to high easily. I think this is what it'd be: Dim Exp as long Public Get Sub Exp(ByVal newExpValue) If newExpValue <= MaxExpValue Then Exp = Exp + newExpValue End Sub But this can also be achieved by raising Experience by calling a sub in the same way if you use a UDT: Public Sub GiveExp(ByVal newExpValue) If newExpValue <= MaxExpValue Then User.Exp = User.Exp + newExpValue End Sub Class modules, I believe, can make things easier and less messy, but UDT will be faster.
sdwYeah, I figured the UDT would be faster. You have any idea how much faster it would be? Right now I can get up to 200 classes loaded into a collection and maintain a framerate of 85 per second. After 220 - 250 instances the framerate begins a steep decline to around 40 - 50 fps. As I get further and further into this project I realize I won't be needing any scripting with the sprites anyways so I should probably just convert them from classes to udt. I only use public variables from the class modules anyways so it would be about the same thing as using a udt anyways. Thx for the reply.
Eric ColemanUsing a class simply to store data isn't really using the class properly. Classes are more of a way to organize a program's logic. Consider this as a simple example, assume you need to draw some shapes. There are two ways to do this, and its really up to you to decide which you like better. The following is just pseudo code. [code] 'Method 1 Enum Shape Circle Square Rect End Enum Draw(vShape as Shape) Case Circle 'draw code Case Square 'draw code Case Rect 'draw code End Function [/code] [code] 'Method 2 Class Circle Draw Sub 'draw code End Sub End Class Class Square Draw Sub 'draw code End Sub End Class Class Rect Draw Sub 'draw code End Sub End Class [/code] I personally prefer method one because if I needed to add a new shape, then any any associated functions that are related to the shape, such as a drawing function, then I can easily look at the code used to draw other shapes. The shapes might not be related, but the drawing code is. For example, the code for drawing of a rectangle and a square would be very similar. The downside to this is that you'll have to edit a bunch of different functions, most likely all scatterd in different files, but the upside is that the function will have the code needed to simply and easily add the shape. In the second example, I didn't show it, but you would probably use some short of polymorphism or use interface inheritance and create some sort of generic 'shape' object and derive things from there. In this method, if you need to add a new shape to your project, then its pretty easy. However, if you need to add a new method or property to your shape, then you'll run into the problem of having to write code for each and every type of class. In a real world case, your classes will be more complicated than a simple shape, and you'll probably have them organized in different locations of your program, most likely in different files. In the case of method 1, you won't have to hunt anything down, you simply write a function to handle all the cases. So you see, the benfits for one method are the bane of the other, and vice versa. It all really dpends on your own style. As for your problem, I think your slowdown is the collection. I think if you used an array of classes it would be faster than a collection of classes. If you use a UDT for data or a Class, the trick with keeping arrays fast is to "over dim" the array. For example, Say you have a function "AddItem(itm)" and itm is either a UDT or a class, doesn't matter. The function AddItem(itm) should be something similar to this, and this isn't the best example, but it shows you the trick. [code] AddItem(itm) Static ndx SizeOfArray = ubound(itmArray) ndx = ndx + 1 if ndx > SizeOfArray Then 'over dim Dim newSize newSize = 0.1 * SizeOfArray 'increase by 10% if newSize < 1000 then newSize = 1000 'to minimize array resising, lets have a minmium resize amount. newSize = newSize + SizeOfArray 'add current size to the size increase. 'This is the part that would slow us down if we did this every time we added an item. 'with a min of 1000, we only resize the array for every 1000th item (worst case) that's added Redim Preserve itmArray(newSize) else 'no need to redim the array since we have extra room. end if 'Add the item to the array. itmArray(ndx) = itm [/code]
VBBRHum... It may seem a dumb question, but... What does "UDT" mean? I know it's something like a class (from what you say here) but does "UDT" mean something like "Unstable Domestic Thread"? Apologize for my dumbness: I deducted UDT was equal to module or something like that, but KNEW what a User Defined Type was. Just didn't recall "User Defined Type' when I saw "UDT"...
BrykovianUDT = User Defined Type And it's not "a module" but only a structure that can be used within any code module (including inside of class modules). -Bryk
Lachlan87UDT = User Defined Type. Ex: [code] Private Type Booger Gooey As Boolean Size As Integer Color As Long End Type Private Sub PickNose() Dim myBooger as Booger Booger.Size = int(rnd * 10) End Sub [/code] Edit: Whups! Bryk's wasn't there when I hit reply.
KriscThe answer is quite simple really... Use a UDT when you don't need the object to hold any methods. Use a class when you do need it to have methods and also maybe inherit. I am doing some pretty cool things in C# for my game and my Player class is going to inherit from Ship class as will the Enemy class...first I think I need to go Direct3D...bleh!
SpodiUDT = Uranium Death Turret Use it when you have 100,000 flesh-eating cannables coming after you with knives and forks. Do not use it when you are standing in front of it or you are, for some reason, licking the inside of the barrels.
VBBRAh...
VBBR
quote:
UDT = User Defined Type And it's not "a module" but only a structure that can be used within any code module.
Well I DO know what's a User Defined Type but didn't know it was a UDT... I said "it's a module" by guessing what was written here... Sorry if I made you think i'm so dumb in VB that I don't know what a "User Defined Type" was....
quote:
UDT = User Defined Type. Ex: Private Type Booger Gooey As Boolean Size As Integer Color As Long End Type Private Sub PickNose() Dim myBooger as Booger Booger.Size = int(rnd * 10) End Sub
" Yeah, I know, I know... " Just wanted to link the WHAT to the HOW DO YOU SPELL IT [:D] Sorry now I see the idiot thing i said... Will edit it.
Spodi
quote:
Sorry if I made you think i'm so dumb
You should be sorry, dumby McDumbs-alot. :D
IAC249There is also an interesting little trick you can do with UDT's when reading from sequential access files. For example, say you have the text file player.dat with the following records: ValdoranM256 ValkirieF234 You can read the file like this: Private Type AllCharacter allRecord As String * 12 End Type Private Type Character name As String * 8 gender As String * 1 hitPoints as String * 3 End Type ' - The following code should be dropped into a button click event. Dim hFile As Integer Dim udtAllChar As AllCharacter Dim udtChar As Character hFile = FreeFile() Open "player.dat" For Input As #hFile While Not EOF(hFile) Line Input #hFile, udtAllChar.allRecord LSet udtChar = udtAllChar MsgBox udtChar.name MsgBox udtChar.gender MsgBox udtChar.hitPoints Wend Close #hFile This is a pretty sweet way to read in data from formatted files without having to resort to Mid$() function calls. - iac249