Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can I save the records of a game in QB?

How can I save the records of a game in QB?

How can I save the records of a game in QB?

I am making a game and I want to save then records.
I tried to write it in another file, but do you know a different way?

RE: How can I save the records of a game in QB?

Let's see the code and we'll help you with it.

this is a simple output to a file
open "gameoutc.jhn" for ourput as #1
outputstr$=name$+" "+str$(score)
print #1,outputstr$
close #1

input from the file
open "gameoutc.jhn" for input as #1
if lof(1)>0 then
 line input #1,inputstr$
end if
close #1

'above examples use sequential files.
'learn them first then move to random
'and/or binary.

RE: How can I save the records of a game in QB?

You will need to save all variables in the game, and character positions.  Make sure you save them in a nice easily read fashion, or the codeing can become a nightmare.

RE: How can I save the records of a game in QB?

Try using GET and PUT

'to save to a file
OPEN "file.sav" FOR RANDOM AS #1
PUT #1, 1, points
PUT #1, 2, xpos
PUT #1, 3, ypos

'to read the file
OPEN "file.sav" FOR RANDOM AS #1
GET #1, 1, points
GET #1, 2, xpos

RE: How can I save the records of a game in QB?

Thank you, I'll try it!

RE: How can I save the records of a game in QB?

I tried it and it worked!

Thank you!

RE: How can I save the records of a game in QB?

To add to QbasicKing's example...

If you do it like he specifies there's no way of organizing your data.  Lets say you have some game data---weapons for instance.  Each weapon has the same kind of attributes, but the attributes are different, which makes each weapon unique.  Here's a basic example:

First we make a record template:

Type weapondata
     wName As String * 15
     wType As Integer
     wDamage As Integer
     wSpeed As Integer
End Type

Each record contains a weapon name, type, damage and speed.  Now lets take this record template and store it into a word, making it an object.  Underneath the object are the record fields called Name, Type, Damage and Speed which are used as Weapon.keyword (i.e. Weapon.Name).  First lets DIM our Weapon object:

Dim Weapon As weapondata

Now we should open the data file for the info to be stored in.  In QBasicKing's example there was no order to the data being written.  When you use a Type, it allows you to keep data consistent as records.  i.e. record one would have a weapon name, type, damage and speed.  The way we open the file is to take the record object (Weapon) and use its length as the record length for the file as follows:

Open "Weapons.dat" for Random Access Read Write as 1 Len=Len(Weapon)

Now lets say you have an existing Weapons.dat and want to find out how many weapons there are in the file.  Take the length of the file and divide it by the length of Weapon, which tells us exactly how many weapons (records) are in the file:

MaxWeapons = LOF(1) / Len(Weapon)

Now that we have this information we can read all weapons into an array, or read them at will.  We simply specify which record (a number between 1 and MaxWeapons) and GET it:

Get 1, recordnumber, Weapon

This reads data from the corresponding record into Weapon.Name, Weapon.Type, Weapon.Damage and Weapon.Speed

Likewise, if you want to write to a record simply specify the recordnumber to write data to.  If you want to create a new record simply write to MaxWeapons + 1.  Be sure to recalculate MaxWeapons after writing a new record.

Weapon.Name = "Cracked Mace"
Weapon.Type = 1   '1=blunt, 2=slash, 3=pierce, etc.
Weapon.Damage = 12
Weapon.Speed = 5

Put 1, recordnumber, Weapon

What you end up with is a file containing records like a database.  Each record is the same length.  Weapon.Name is stored as 15 characters regardless if it is less than that.  When you read in Weapon.Name you have to do RTRIM$(Weapon.Name) to remove spaces from the end, because it will be 15 characters.

The way QBasicKing described would write indescriminantly to the random access file, as if you were writing to a data file using sequential file writing (i.e. Open myfile for Input/Output).  Random access files are powerful because they can be set up to hold huge amounts of data in an orderly fashion (records), and data can be retrieved super fast.  If I were writing a game, I'd sure be using random access files instead of plain ole sequential data files.  You can keep track of your data easier and there's no parsing at all.

Hope I've helped.  Good luck.

RE: How can I save the records of a game in QB?

In complement to cgfiend, you can even make it more sophisticated by adding some type into other type.


Your weapon can fire standard bullets and can shoot grenades too. What can you do about that?

Make a new record type about munitions

TYPE sMunition
  wNumOfBullets as integer
  wNumOfGrenades as integer

Now that your new data type has been created, you can insert it into your old Type as a type itself.

TYPE sWeaponData
     wName As String * 15
     wType As Integer
     wDamage As Integer
     wSpeed As Integer
     wMunition As sMunition

DIM weapon AS sWeaponData

That's it. Now, you can set the number of rounds like this:

weapon.wMunition.wNumOfBullets = 10
weapon.wMunition.wNumOfGrenades = 5

It is pretty useful for data structures with date.

TYPE sDate
  wDay as string * 2
  wMonth as string * 2
  wYear as string * 4

TYPE sInfo
  wName as string * 20
  wTel as string * 8
  wBirthDate as sDate

DIM user AS sInfo

user.wName = "Ron Jeremy"
user.wTel = "877-5646"
user.wBirthDate.wDay = "14"
user.wBirthDate.wMonth = "10"
user.wBirthDate.wYear = "1978"


RE: How can I save the records of a game in QB?

Oak, you're the man.   I didn't know you could do that!  We're all here to learn.

RE: How can I save the records of a game in QB?

There no problem at all. :) I've got a cousre about structures 6 month ago: it has to serve!

Hey! I've just went into an idea! My course was about structure handlind in C with dynamic memory access with pointers. Pointers, that qbasic can't handel so far can be access with this idea. I don't know if it works but I'm sure it can be.

In my courses, we got to make structures with data fields that correspond to the data structure adresse memory. I didn't try it so far but I'm sure that we can affect the data structure adresse to a structure itself.

But I've think of it, qbasic doesn't have support direct memory allocation, so forget it.

Glad that I've learn you something new!

RE: How can I save the records of a game in QB?

Oak. I realize you can write data to the HDD from QB, such as variables, but I need help with the COM port. Lets say you wanted to take INFO$ and send it through the COM port to your friends computer down the street via the internet. Exactly how much code would that take? I'm not looking for a huge program that supports like email and stuff, I just want the simple code to access the modem, dial up, and transferr the data. Is it really that complicated? What are your thoughts?


RE: How can I save the records of a game in QB?


You should really create a seperate posting for your question.

RE: How can I save the records of a game in QB?

Basterm.bas you could use to call a modem anywhere and transfer info through it, it is less than 200 lines

If a man says something in the forest and no woman hears it is he still wrong?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close