Qbasicnews.com
May 26, 2020, 06:25:26 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Using files to store/load data rather than types  (Read 3234 times)
worchyld
New Member

Posts: 6


« on: January 14, 2004, 09:23:05 AM »

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?
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #1 on: January 14, 2004, 03:16:22 PM »

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.
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #2 on: January 14, 2004, 03:22:50 PM »

Besides, there's nothing wrong with TYPEs.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #3 on: January 14, 2004, 03:23:18 PM »

exactimundo
Logged
Jark
Ancient Guru
****
Posts: 566



WWW
« Reply #4 on: January 14, 2004, 10:21:20 PM »

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...
Logged

hink Global, Make Symp' All !
worchyld
New Member

Posts: 6


« Reply #5 on: January 15, 2004, 06:59:29 AM »

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.
Logged
adosorken
*/-\*
*****
Posts: 3655



WWW
« Reply #6 on: January 15, 2004, 07:54:55 AM »

Quote from: "Jark"
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 Cheesy
Logged

I'd knock on wood, but my desk is particle board.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #7 on: January 15, 2004, 12:46:04 PM »

Quote from: "Jark"
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.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
worchyld
New Member

Posts: 6


« Reply #8 on: January 16, 2004, 10:38:19 AM »

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.
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #9 on: January 16, 2004, 03:43:02 PM »

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?
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #10 on: January 16, 2004, 03:59:42 PM »

What I did to display/move all the men is just run through a FOR loop 50 times, iterator called i, (assuming you have 50 men), and for each man(i), move him.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
LooseCaboose
I hold this place together
*****
Posts: 981



« Reply #11 on: January 17, 2004, 02:04:08 AM »

You can combine the use of types and files for storing data. Try the following:
Code:

'**** Type for storing player information ****
TYPE playerType
  cname AS STRING * 8
  weapon AS INTEGER
  health AS INTEGER
END TYPE

DIM player(3) AS playerType

'**** Setup 3 players ****
player(1).cname = "Fred"
player(1).weapon = 2
player(1).health = 100

player(2).cname = "Bob"
player(2).weapon = 9
player(2).health = 54

player(3).cname = "Sam"
player(3).weapon = 5
player(3).health = 34

'**** Save the player information to a file ****
OPEN "players.dat" FOR BINARY AS #1
FOR i = 1 TO 3
  PUT #1, , player(i)
NEXT
CLOSE #1

'**** Clear the player information ****
FOR i = 1 TO 3
  player(i).cname = "no name"
  player(i).weapon = 0
  player(i).health = 0
NEXT

'**** Load the player information from the file ****
OPEN "players.dat" FOR BINARY AS #1
FOR i = 1 TO 3
  GET #1, , player(i)
NEXT
CLOSE #1

'**** Display the player information ****
FOR i = 1 TO 3
  PRINT "Player"; i; " name: "; player(i).cname
  PRINT "Player"; i; " weapon:"; player(i).weapon
  PRINT "Player"; i; " health:"; player(i).health
  PRINT
NEXT


You can use this to save memory by only loading the types that you currently need into memory and leaving the rest on disk. For example, if your game allowed you to play as one of 10 different characters, you could have the information for each of the 10 characters saved in a file, when the player makes their choice you then load only the character they selected.
Logged

esus saves.... Passes to Moses, shoots, he scores!
worchyld
New Member

Posts: 6


« Reply #12 on: January 20, 2004, 12:22:27 PM »

Thanks for that, I'll run with those ideas and use them the way I can.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!