TGC Codebase Backup



Using simple .ini files, configurations, and tracking saved games by Linehan

17th Sep 2009 6:58
Summary

I will show you how to check for the existence of text files; such as a .ini file. Create it if it does not exist. Read configuration information if it DOES exist. I also how how t



Description

I CREATED THIS TUTORIAL CODE TO SHOW PEOPLE SOME SIMPLE EXAMPLES OF HOW THEY CAN CREATE, STORE, LOAD AND SAVE GAME CONFIGURATION DATA. I ALSO SHOW HOW YOU CAN KEEP TRACK OF SAVED GAMES AS WELL. OBVIOUSLY THIS SOURCE CODE WILL NOT COMPILE AND RUN. IT IS SIMPLY A STUDY GUIDE FOR THE BASICS OF TEXT FILE CREATION, READING AND WRITING. FEEL FREE TO USE ANY PART OF TYHIS SOURCE CODE IN YOUR OWN GAMES, BUT IF YOU DO, PLEASE GIVE CREDIT TO THE ORIGINAL AUTHOR. I know this is nothing spectacular as far as tutorials go, but I am learning quickly and I just wanted to do my part to share with the community.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REMSTART

Configuration and text file manipulation tutorial

MARK@LINEHAN.COM

I am looking for people who would like to work as a team on creating some games. I especially would like to make a game creating system similar to the BYOND system that my son enjoys using because I think we could use DBPRO to make something like that except MUCH better.

REMEND

`Assign default values to the variables
INSTALLATION_PATH$="C:\PROGRAM FILES\YOURGAME\"
SAVEGAME_PATH$="C:\PROGRAM FILES\YOURGAME\SAVES\"
VERSION_INFO$="YOURGAME v1.0"

X = FILE EXIST("YOURGAME.INI")	`0 = IF THE .INI IS NOT THERE
                                `1 IF THE .INI FILE IS THERE
IF X = 1	`IF THE .INI FILE IS THERE
	OPEN TO READ 1, "YOURGAME.INI"	`Open the .ini file to be read from as file #1
		READ FILE 1, INSTALLATION_PATH$	`Read a variable from file #1
		READ FILE 1, SAVEGAME_PATH$
		READ FILE 1, VERSION_INFO$
	CLOSE FILE 1	`Always remember to close files that you opened
ENDIF

IF X = 0	`IF THE .INI FILE IS MISSING CREATE IT WITH DEFAULT VARIABLE VALUES
	OPEN TO WRITE 1, "YOURGAME.INI"	`Open the .ini file to be written to as file #1
		WRITE FILE 1, INSTALLATION_PATH$	`Write the variables to file #1
		WRITE FILE 1, SAVEGAME_PATH$
		WRITE FILE 1, VERSION_INFO$
	CLOSE FILE 1	`Always close the files you opened.
ENDIF

`Create path variables for sound files, images, maps and other data files
IMAGES_PATH$=INSTALLATION_PATH$+"IMAGES\"
SOUNDS_PATH$=INSTALLATION_PATH$+"SOUNDS\"
MAPS_PATH$=INSTALLATION_PATH$+"MAPS\"
ANIMATIONS_PATH$=INSTALLATION_PATH$+"ANIMATIONS\"
SPRITES_PATH$=INSTALLATION_PATH$+"SPRITES\"

`Get a list of all saved games from SAVEGAME_PATH$ by using an if exist and for next loop
FOR X = 1 TO 20 `There are a maximum of 20 saved games possible.
	FILENAME$=SAVEGAME_PATH$+"SAVE"+STR$(X)
	SAVEGAME(X) = FILE EXIST(FILENAME$)	`If 0 then there is no saved game X.
NEXT X                                          `If a 1 then there is a saved game X.

REMSTART

The above method for checking for the existence of saved games is just one of many methods for tracking saved games. Another method would be to have the program track saved games and keep a tally of them in the .ini file. The method of tracking saved games using the .ini file would be useful if you want the user to be able to name the saved games anything they like. Below is one method for this using the players documents folder as the storage location for the ini file.

REMEND

DOCUMENTS_PATH$=MYDOCDIR$()+"\YOURGAME\YOURGAME.INI"	`MYDOCDIR$() RETURNS THE PATH TO 
DIM SAVEDGAME(20) AS INTEGER                            `THE USERS DOCUMENTS FOLDER
DIM SAVEDNAME$(20) AS STRING

IF FILE EXIST(DOCUMENTS_PATH$)			`IF THE FILE EXISTS OPEN IT AND READ THE 
	OPEN TO READ 1,DOCUMENTS_PATH$          `VARIABLES FROM IT.
		READ FILE 1, INSTALLATION_PATH$
		READ FILE 1, SAVEGAME_PATH$
		READ FILE 1, VERSION_INFO$
		FOR X = 1 TO 20			     `THE SYSTEM WILL READ 20 BOOLEANS
			READ FILE 1,SAVEDGAME(X)     `AND 20 SAVED GAME FILENAMES
			READ FILE 1,SAVEDNAME$(X)
		NEXT X
	CLOSE FILE 1                                  `CLOSE THE FILE WHEN DONE READING
ELSE
	DELETE FILE DOCUMENTS_PATH$
	OPEN TO WRITE 1, DOCUMENTS_PATH$	`THIS CREATES A NON-EXISTENT FILE
		WRITE FILE 1, "C:\PROGRAM FILES\YOURGAME\"
		WRITE FILE 1, MYDOCDIR$()+"\YOURGAME\SAVES\"
		WRITE FILE 1, "YOUR GAME v1.0"
		FOR X = 1 TO 20
			SAVED$="SAVE_"+STR$(X)+".SAV"
			WRITE FILE 1, 0
			WRITE FILE 1, SAVED$
		NEXT X
	CLOSE FILE 1				`CLOSE THE FLE WHEN DONE CREATING IT