Qbasicnews.com

Full Version: Speed of UDT's
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I suggest you to use UDTs. Better coding, more readable, more comfortable and with more features, i.e.

Code:
DECLARE SUB ShowStats (player as PlayerType)

versus

Code:
DECLARE SUB ShowStats (strength AS INTEGER, power AS INTEGER, intelligence AS INTEGER, dexterity AS INTEGER, mp AS INTEGER, gp AS INTEGER, fp AS INTEGER, experience AS INTEGER, level AS INTEGER, handsomeness AS INTEGER)

You decide Wink
Well its obvious what ill choose Smile
I hate clutter and UDT's IMO reduce clutter!

But i do have a question about them

(not actual code)

say i have this UDT for all my items

Type Iteminfo
...
flag as integer ' For the type IE potion, gun and so on
value as integer ' for the monetary value
...
end type

This could be for all the items in the game (item info is stored in a seperate file ) I may break it down to different declarations for item types if i have alot of items...

DIM item(0 to 99) as iteminfo

How could i assign an inventory to the player and monsters (some will have items you would expect IE gun jacket and so on) efficently

Im trying to store all the players/monsters info in a single Type

I know the answer is right under my nose but i cannot seem to figure it out ...
Use indexes. If you have a type structure to store your monsters, etc., add a "itemslist" array (you can't do this in QB4.5, beware. That can be done only in PDS 7.1 or later):

Code:
Const MAXITEMSPERCHARACTER = 10

Type characterType
   ...
   itemsList (MAXITEMSPERCHARACTER - 1) As Integer
End Type

Everytime a character gets an item, you add a value to the array which is exactly the index of that item in the items array. I.e. if you have a leather jacket in the items array on position #5, you store a 5 in a free position of that array.

In case you are using QB45 which doesn't allow arrays as type structure members, you can do it the way around, i.e. storing in the items array which character has that item.
but then wont I have to check every item to see if the char has it?

maybe ill use a string instead of an array (using qb4.5) with comma delimeters and parse it..

IE

player.inventory = player.inventory + ",1" for an Axe

and so on

whatever is faster I suppose
You could use linked lists, but that'd require some dirty hacking with pointers and such or a suboptimal implementation with arrays and indicies.
Well im using QB45 .. i would use linked lists but the lack of pointers kinda hinders it .

though i was debating FB pointers ae sexy.. and well.... hmm .. helpful
If I were you, I'd rather download VBDOS which is the last version of MS BASIC for MSDOS. It allows arrays in UDTs and compiles much better code (with some 386 opcodes).

Doing it with strings can be very slow. Addin an object may be easy, but eliminating one will require more (very time consumming) operations.

You could also use bitwise operations and a LONG integer, but that would only allow 32 objects in the game.

Trust me, get VBDOS Wink
Pages: 1 2