Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Loading / Saving system
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tjmoore1993




PostPosted: Sat Apr 04, 2009 12:28 pm   Post subject: [Tutorial] Loading / Saving system

In this topic I will be discussing how the following code works and how to use it. A loading / saving system involves 2 steps "LOAD" and "SAVE". These 2 steps need to have their own procedure so when we need them... We can just enter in a line that basically says, "SaveGame or LoadGame".

To get started we will need some variables inorder to retrieve the data and re-use it again.

Turing:

var stream, filePosition,                    % These 2 must be declared as integers because they use whole values.
    Selection : int                          % Selection can be declared as anything we want.
var FileName, Extension, FileDir,            % These 3 will be explained later.
    Sex, Name : string                       % These 2 will be used for testing.


The variables shown above are used to do both procedures that will be made. Now that we have declared variables we can go to the next step of defining them. To define a variable we simply give it a value or a text.

Turing:

Extension := ".db"                           % This variable has been defined.
FileDir := "C:\\"                            % This variable has been defined.


Now that we have defined the variables we will be able to go onto the next step. Time to create our procedures!

SAVE PROCEDURE
Turing:

procedure SaveFile                           % Start of SaveFile.
    Extension := ".db"                           % This variable has been defined.
    FileDir := "C:\\"                            % This variable has been defined.
    FileName := Name                         % "FileName := Name" FileName will then equal what the End-User entered for their name.
    FileName += Extension                    % "FileName += Extension" Puts FileName and Extension together as 1 word.
    FileDir += FileName                      % "FileDir += FileName" Puts directory before FileName.
    open : stream, FileDir, put, seek        %
    tell : stream, filePosition              %
    put : stream, Name                       % Puts the End-Users "Name" into the document on line 1
    put : stream, Sex                        % Puts the End-Users "Sex" into the document on line 2
    seek : stream, filePosition              %
    close : stream                           % When finished saving it closes file. (Required for a stable application)
end SaveFile                                 % End of SaveFile


LOAD PROCEDURE
Turing:

procedure LoadFile                           % Start of LoadFile
    loop                                     % Infinite Loop.
        put "What is your name?"             % Asks the End-User what their name is.
        get FileName                         % Gets FileName instead of Name to save memory.
        Extension := ".db"                   % This variable has been defined.
        FileDir := "C:\\"                    % This variable has been defined.
        FileName += Extension                % "FileName += Extension" Puts FileName and Extension together as 1 word.
        FileDir += FileName                  % "FileDir += FileName" Puts directory before FileName.
        if File.Exists (FileDir) then        % Checks if file exists.
            open : stream, FileDir, get      %
            loop                             % Infinite Loop.
                exit when eof (stream)       % Exits loop when end-of-file.
                get : stream, Name           % Sets the variable Name.
                get : stream, Sex            % Sets the Variable Sex.
            end loop                         % End Loop.
            close : stream                   % When finished saving it closes file. (Required for a stable application)
            exit                             % Exits loop if load was successful.
        else                                 % If error then it will run;
            put "The file does not exist."   % Bullet-Proofing.
            delay (600)                      % Warns End-User of their error.
        end if                               % Ends the IF-Statement.
    end loop                                 % End Loop.
end LoadFile                                 % End LoadFile


My definitions stink, anyways... To make more sense of this pretend procedures are our friends. Friends that we can always depend on. When we need our friends we just call them... Right?

Turing:

LoadFile                                     % Call LoadFile
                                             % OR
SaveFile                                     % Call SaveFile


Now that we have 2 dependable friends we can call them when we need to! Now in order to figure out when to call them we must have some sort of IF-Statement.
IF-STATEMENT
Turing:

loop                                                    % Infinite Loop
    put "Welcome to my Saving / Loading system."        % Greets End-User.
    put "What task would you like to perform?"          % Asks End-User what option they would like to perform.
    put "1 - New File"                                  % Option 1.
    put "2 - Load File"                                 % Option 2.
    get Selection                                       % Gets Selection.
    if Selection = 1 then                               % If Selection is 1 then...
        put "What is your name?"                 % Asks the End-User what their name is.
        get Name                                 % Gets "Name"
        put "Which sex are you?"                 % Asks the End-User which sex they are. (Male or Female)
        get Sex                                  % Gets "Sex"
        cls                                             % Clear Screen.
        SaveFile                                        % Call SaveFile.
        exit                                            % Exits Loop.
    elsif Selection = 2 then                            % Elsif Selection is 2 then...
        cls                                             % Clear Screen
        LoadFile                                        % Call LoadFile.
        exit                                            % Exits Loop
    else                                                % If Selection was not 1 or 2 then...
        put "Your selection did not match any options"  % Tell End-User they've did something wrong.
        cls                                             % Clear Screen.
    end if                                              % Ends the IF-Statement.
end loop                                                % End Loop


When all is understood you should be able to use the information in your next project or even so, make a better loading / saving system then me.

FINAL CODE
Turing:

var stream, filePosition,                    % These 2 must be declared as integers because they use whole values.
    Selection : int                          % Selection can be declared as anything we want.
var FileName, Extension, FileDir,            % These 3 will be explained later.
    Sex, Name : string                       % These 2 will be used for testing.

procedure SaveFile                           % Start of SaveFile.
        Extension := ".db"                   % This variable has been defined.
        FileDir := "C:\\"                           % Start of SaveFile.
    FileName := Name                         % "FileName := Name" FileName will then equal what the End-User entered for their name.
    FileName += Extension                    % "FileName += Extension" Puts FileName and Extension together as 1 word.
    FileDir += FileName                      % "FileDir += FileName" Puts directory before FileName.
    open : stream, FileDir, put, seek        %
    tell : stream, filePosition              %
    put : stream, Name                       % Puts the End-Users "Name" into the document on line 1
    put : stream, Sex                        % Puts the End-Users "Sex" into the document on line 2
    seek : stream, filePosition              %
    close : stream                           % When finished saving it closes file. (Required for a stable application)
end SaveFile                                 % End of SaveFile

procedure LoadFile                           % Start of LoadFile
    loop                                     % Infinite Loop.
        put "What is your name?"             % Asks the End-User what their name is.
        get FileName                         % Gets FileName instead of Name to save memory.
        Extension := ".db"                   % This variable has been defined.
        FileDir := "C:\\"                    % This variable has been defined.
        FileName += Extension                % "FileName += Extension" Puts FileName and Extension together as 1 word.
        FileDir += FileName                  % "FileDir += FileName" Puts directory before FileName.
        if File.Exists (FileDir) then        % Checks if file exists.
            open : stream, FileDir, get      %
            loop                             % Infinite Loop.
                exit when eof (stream)       % Exits loop when end-of-file.
                get : stream, Name           % Sets the variable Name.
                get : stream, Sex            % Sets the Variable Sex.
            end loop                         % End Loop.
            close : stream                   % When finished saving it closes file. (Required for a stable application)
            exit                             % Exits loop if load was successful.
        else                                 % If error then it will run;
            put "The file does not exist."   % Bullet-Proofing.
            delay (600)                      % Warns End-User of their error.
        end if                               % Ends the IF-Statement.
    end loop                                 % End Loop.
end LoadFile                                 % End LoadFile

loop                                                    % Infinite Loop
    put "Welcome to my Saving / Loading system."        % Greets End-User.
    put "What task would you like to perform?"          % Asks End-User what option they would like to perform.
    put "1 - New File"                                  % Option 1.
    put "2 - Load File"                                 % Option 2.
    get Selection                                       % Gets Selection.
    if Selection = 1 then                               % If Selection is 1 then...
    put "What is your name?"                 % Asks the End-User what their name is.
    get Name                                 % Gets "Name"
    put "Which sex are you?"                 % Asks the End-User which sex they are. (Male or Female)
    get Sex                                  % Gets "Sex"
        cls                                             % Clear Screen.
        SaveFile                                        % Call SaveFile.
        exit                                            % Exits Loop.
    elsif Selection = 2 then                            % Elsif Selection is 2 then...
        cls                                             % Clear Screen
        LoadFile                                        % Call LoadFile.
        exit                                            % Exits Loop
    else                                                % If Selection was not 1 or 2 then...
        put "Your selection did not match any options"  % Tell End-User they've did something wrong.
        cls                                             % Clear Screen.
    end if                                              % Ends the IF-Statement.
end loop                                                % End Loop

Sponsor
Sponsor
Sponsor
sponsor
Savage Reindeer




PostPosted: Sun Apr 26, 2009 8:29 am   Post subject: RE:[Tutorial] Loading / Saving system

This is a little bit beyond me at the moment. But by glancing over it I can tell that this is well written. Can't wait to read it later!
kalev




PostPosted: Tue May 12, 2009 10:40 am   Post subject: RE:[Tutorial] Loading / Saving system

using parameters for your load/save procs would add portability
tjmoore1993




PostPosted: Tue May 12, 2009 3:13 pm   Post subject: Re: RE:[Tutorial] Loading / Saving system

kalev @ Tue May 12, 2009 10:40 am wrote:
using parameters for your load/save procs would add portability


I tried to simplify it. If you'd like to add to it, feel free.
efan41




PostPosted: Fri Oct 23, 2009 1:28 pm   Post subject: Re: [Tutorial] Loading / Saving system

Shouldn't the file open onto a different screen when you ask it too?

Or am I doing something wrong?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: