Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 UDT vs Class
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sdw
Warrior

USA
160 Posts

Posted - Mar 30 2004 :  5:47:38 PM  Show Profile  Visit sdw's Homepage  Click to see sdw's MSN Messenger address  Reply with Quote
I'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?

Spodi
Warrior

USA
142 Posts

Posted - Mar 31 2004 :  7:18:45 PM  Show Profile  Visit Spodi's Homepage  Send Spodi an AOL message  Click to see Spodi's MSN Messenger address  Send Spodi a Yahoo! Message  Reply with Quote
UDT = 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.


vbGORE
Go to Top of Page

sdw
Warrior

USA
160 Posts

Posted - Mar 31 2004 :  9:21:48 PM  Show Profile  Visit sdw's Homepage  Click to see sdw's MSN Messenger address  Reply with Quote
Yeah, 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.
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Apr 01 2004 :  12:17:02 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
Using 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.

  
'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
  


  
'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  
  


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.

  
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  
  
  
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Apr 01 2004 :  10:05:14 AM  Show Profile  Reply with Quote
Hum... 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"...

Whatever. Who knows...

Edited by - VBBR on Apr 02 2004 4:46:51 PM
Go to Top of Page

Brykovian
Bryk the Cheese Slayer!

USA
58 Posts

Posted - Apr 01 2004 :  11:55:47 AM  Show Profile  Visit Brykovian's Homepage  Click to see Brykovian's MSN Messenger address  Reply with Quote
UDT = 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

www.mwgames.com
Go to Top of Page

Lachlan87
Moderator

USA
160 Posts

Posted - Apr 01 2004 :  11:58:19 AM  Show Profile  Reply with 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
  



Edit: Whups! Bryk's wasn't there when I hit reply.


Edited by - Lachlan87 on Apr 01 2004 7:40:55 PM
Go to Top of Page

Krisc
Knave

69 Posts

Posted - Apr 01 2004 :  4:47:02 PM  Show Profile  Visit Krisc's Homepage  Send Krisc an AOL message  Click to see Krisc's MSN Messenger address  Reply with Quote
The 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!
Go to Top of Page

Spodi
Warrior

USA
142 Posts

Posted - Apr 02 2004 :  1:53:20 PM  Show Profile  Visit Spodi's Homepage  Send Spodi an AOL message  Click to see Spodi's MSN Messenger address  Send Spodi a Yahoo! Message  Reply with Quote
UDT = 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.

vbGORE
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Apr 02 2004 :  3:36:44 PM  Show Profile  Reply with Quote
Ah...

Whatever. Who knows...
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Apr 02 2004 :  4:41:49 PM  Show Profile  Reply with Quote
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

Sorry now I see the idiot thing i said... Will edit it.

Whatever. Who knows...
Go to Top of Page

Spodi
Warrior

USA
142 Posts

Posted - Apr 02 2004 :  6:57:21 PM  Show Profile  Visit Spodi's Homepage  Send Spodi an AOL message  Click to see Spodi's MSN Messenger address  Send Spodi a Yahoo! Message  Reply with Quote
quote:
Sorry if I made you think i'm so dumb

You should be sorry, dumby McDumbs-alot.

:D

vbGORE
Go to Top of Page

IAC249
Squire

USA
39 Posts

Posted - Apr 03 2004 :  9:36:33 PM  Show Profile  Visit IAC249's Homepage  Send IAC249 a Yahoo! Message  Reply with Quote
There 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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.23 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.