Qbasicnews.com

Full Version: Using files to store/load data rather than types
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm currently trying to code a game - every time I start I always use types - there are currently loads of types and I want to reduce the number of types I'm using.

In my 'game' there are a maximum of 8 players, each with their cart to hold products. Each player has their own 'max' cart size - which can only go up to 10.

For example;
Code:
type player
        cartSize as integer
end type
      dim player(8) as player

type cart
        item as integer
        qty as integer
        pricepaid as double
end type
      dim cart(8,10) as cart

Instead of doing it this way, is there a way of using .dat files or random access - the thing I can't get my head around is what happens if the player's cart is empty - or if one item is empty and yet the next isn't.

Or what happens if a player dies, thus his cart has to be deleted/emptied.

I guess I'm trying to go for a OO-approach to using types through file management.

I'm trying to move away from types - mainly because I'm using so many at the moment - for some reason I believe file management may hold they key.

Anyone willing to give us a step in the right direction?
Well, using file access to store in game data isnt very fast, nor is it good practice. I would stick with your original code if I were you.
Besides, there's nothing wrong with TYPEs.
exactimundo
A few months ago, I think Agamemnus told me to use arrays instead of types... but he did not tell why!

The only thing with Types is that you create large variables very easily, so you can reach the 64k limit of the stack very rapidly (in case you declare a array as a user type). With arrays only, the limit applies array by array...

For example:

TYPE Ball
x0 AS SINGLE
y0 AS SINGLE
z0 AS SINGLE
Radius AS SINGLE
END TYPE

DIM Ball(nBalls&) AS Ball

You can get only 64000/16=4000 balls, while you could get 64000/4 = 16000 balls with four arrays (one for x0, one for y0, etc...) instead of Type-Array as above.

There may be some speed issues also, depending on the way the variables are located in the RAM and on the way the prog reads/writes them...
Wow - I didn't expect so many replies. Many thanks for your suggestions and help with this.

I know types are a good way of storing, retreving data - the only reason I wanted to use file management to store/load everything was because I've got so many types it sometimes takes 60-70% of my code (increases the file size).

I haven't coded in QB for quite some time - the only reason I went back to it is because I want to code this management game once and for all and get it out of my head.

Having read what you've all had to say I've decided to stick with Types.

Thanks again.
Quote:A few months ago, I think Agamemnus told me to use arrays instead of types... but he did not tell why!
Because Agamemnus is qbasicnews.com's #1 TYPE hater, of course Big Grin
Quote:There may be some speed issues also, depending on the way the variables are located in the RAM and on the way the prog reads/writes them...

Nah, no speed issues. When the code is compiled, tipes are undone, and you just have addresses, just like in simple arrays. Also, the 64 K limitation can be overriden if you run QB with the /AH command line option. In PDS it will work even better at it has better memory management (putting strings in a far segment, leaving more DATA space) and it will be a like a blessing if you use VBDOS.
I wish to thank all those who helped, there is one slight sticking point I'd like to address;

Each player can have up to a maximum of 10 soliders - but ideally I'd love to have it 50 soldiers.

Code:
Type playerMen
         cname as string * 10
         cwpn as integer
         health as integer
end type
       dim playerMen(8,50) as playerMen

So far so good, and I understand the reasoning behing using types, but displaying all 50 men on one screen? Which is the best way of achieving this? Microsoft Money manager uses dos interrupts to allow paging up and down, and I'm interested in using the 'viewport' feature - but I am unsure of how best to procede.

Any suggestions would be apprecated.

Many thanks.
Some tips which would probably apply to your code:

1) If you have a common attribute for a set of objects, make that another type/array instead of using that common attribute lots of times.

2) In regards to your first question, what you want to have is variating lengths. There are many ways to accomplish this. On one extreme, you have to use a lot of memory but it is very fast to access things. On the other hand, you use minimal memory but accessing things is very slow. It depends on the exact situation, but usually the middle-of-the-road solution works best.

Now, the first extreme where you use huge amounts of memory:
Have a maximum amount of integers reserved for each player and another integer specifying whether the player is dead/how many of the integers should actually be used.

My approach would be like this. It doesn't use types 'cuz I don't like them..:

Have an array that can hold all the stuff of all players, like this:
(yes now I agree that it should start at zero in most cases..)

maximumStuff% = 50
stuff(maximumStuff% - 1)

Now there are at least two things you can do. For memory efficiency, use approach 1. For maximum array-change speed (you'll want to change the size of each player's stuff list), there is another approach. It takes more memory. However my mind is blank on the matter currently.

Approach 1:
have another array:
playerStuffStart(maximumPlayers%)

Now, this is going to say the range of stuff each player owns in the stuff() array. For example, the first player's range is: playerStuffStart(0) TO playerStuffStart(1) - 1. See?
Pages: 1 2