Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

Aron1

Programmer
Mar 19, 2003
11
HU
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?
Thanks.
 
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.
 
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.
 
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
'...
CLOSE

'to read the file
OPEN "file.sav" FOR RANDOM AS #1
GET #1, 1, points
GET #1, 2, xpos
'...
CLOSE
 
Wow!
I tried it and it worked!

Thank you!
 
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.
 
In complement to cgfiend, you can even make it more sophisticated by adding some type into other type.

Exemple:

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
END TYPE


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
END TYPE

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
END TYPE

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

DIM user AS sInfo

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


Enjoy!
 
Oak, you're the man. :eek:) I didn't know you could do that! We're all here to learn.
 
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!
 
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?

Doug :)
 
qbasicguru;

You should really create a seperate posting for your question.
 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top