MTA:Eir/FileSystem/file/readBoolean: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
boolean file.readBoolean ()
boolean file:readBoolean ()
</syntaxhighlight>
</syntaxhighlight>


Line 18: Line 18:


     -- Write basic details about the player instance.
     -- Write basic details about the player instance.
     theFile.writeShort( getElementModel( player ) );
     theFile:writeShort( getElementModel( player ) );
     theFile.writeInt( getPlayerMoney() );
     theFile:writeInt( getPlayerMoney() );
      
      
     -- Next write down special details given by the game.
     -- Next write down special details given by the game.
     local isQuickSave = saveDetails.isQuickSave and true or false;
     local isQuickSave = saveDetails.isQuickSave and true or false;


     theFile.writeBoolean( isQuickSave );
     theFile:writeBoolean( isQuickSave );


     if not ( isQuickSave ) then
     if not ( isQuickSave ) then
Line 31: Line 31:
         local dimension, interior = getElementDimension( player ), getElementInterior( player );
         local dimension, interior = getElementDimension( player ), getElementInterior( player );


         theFile.writeDouble( posX ); theFile.writeDouble( posY ); theFile.writeDouble( posZ );
         theFile:writeDouble( posX ); theFile:writeDouble( posY ); theFile:writeDouble( posZ );
         theFile.writeShort( dimension ); theFile.writeShort( interior );
         theFile:writeShort( dimension ); theFile:writeShort( interior );
     end
     end


Line 38: Line 38:
     local realTime = getRealTime();
     local realTime = getRealTime();


     theFile.writeShort( realTime.minute ); theFile.writeShort( realTime.hour );
     theFile:writeShort( realTime.minute ); theFile:writeShort( realTime.hour );
     theFile.writeShort( realTime.monthday ); theFile.writeShort( realTime.month + 1 ); theFile.writeShort( realTime.year + 1900 );
     theFile:writeShort( realTime.monthday ); theFile:writeShort( realTime.month + 1 ); theFile:writeShort( realTime.year + 1900 );
     return true;
     return true;
end
end


local function readBasicData( operator, callback )
local function readBasicData( obj, operator, callback )
     local data = operator();
     local data = operator(obj);


     if ( data ) then
     if ( data ) then
Line 57: Line 57:


     -- Restore the basic information.
     -- Restore the basic information.
     readBasicData( theFile.readShort, function(model) setElementModel( player, model ); end );
     readBasicData( theFile, theFile.readShort, function(model) setElementModel( player, model ); end );
     readBasicData( theFile.readInt, function(money) setPlayerMoney(money); end );
     readBasicData( theFile, theFile.readInt, function(money) setPlayerMoney(money); end );


     -- Read advanced parameters,
     -- Read advanced parameters,
     local isQuickSave = theFile.readBoolean();
     local isQuickSave = theFile:readBoolean();
     saveDetails.isQuickSave = isQuickSave;
     saveDetails.isQuickSave = isQuickSave;


     if not ( isQuickSave ) then
     if not ( isQuickSave ) then
         -- Restore advanced details.
         -- Restore advanced details.
         local posX, posY, posZ = theFile.readDouble(), theFile.readDouble(), theFile.readDouble();
         local posX, posY, posZ = theFile:readDouble(), theFile:readDouble(), theFile:readDouble();
         local dimension, interior = theFile.readShort(), theFile.readShort();
         local dimension, interior = theFile:readShort(), theFile:readShort();


         -- Set them, if read correctly.
         -- Set them, if read correctly.
Line 79: Line 79:
     -- Get the time the snapshot was taken at.
     -- Get the time the snapshot was taken at.
     saveDetails.saveTime = {
     saveDetails.saveTime = {
         minute = theFile.readShort(),
         minute = theFile:readShort(),
         hour = theFile.readShort(),
         hour = theFile:readShort(),
         monthday = theFile.readShort(),
         monthday = theFile:readShort(),
         month = theFile.readShort(),
         month = theFile:readShort(),
         year = theFile.readShort()
         year = theFile:readShort()
     };
     };


Line 103: Line 103:
             saveLocalPlayerState( saveFile, saveDetails );
             saveLocalPlayerState( saveFile, saveDetails );


             saveFile.destroy();
             saveFile:'''Bold text'''destroy();


             outputChatBox( "successfully saved complete player status" );
             outputChatBox( "successfully saved complete player status" );
Line 123: Line 123:
             saveLocalPlayerState( saveFile, saveDetails );
             saveLocalPlayerState( saveFile, saveDetails );


             saveFile.destroy();
             saveFile:destroy();


             outputChatBox( "successfully saved quick player status" );
             outputChatBox( "successfully saved quick player status" );
Line 142: Line 142:
             local saveData = loadLocalPlayerState( saveFile );
             local saveData = loadLocalPlayerState( saveFile );


             saveFile.destroy();
             saveFile:destroy();


             outputChatBox( "restored from a save (date was " .. saveData.saveTime.monthday .. "." .. saveData.saveTime.month .. "." .. saveData.saveTime.year .. ")" );
             outputChatBox( "restored from a save (date was " .. saveData.saveTime.monthday .. "." .. saveData.saveTime.month .. "." .. saveData.saveTime.year .. ")" );

Latest revision as of 23:44, 16 January 2022

This function attempts to read a boolean (native type) from a file and return it. The amount of bytes read should be one.

Syntax

boolean file:readBoolean ()

Returns

Returns a boolean if it was successfully read from the file, nil otherwise.

Example

Click to collapse [-]
Client

This snippet demonstrates a variable file format that stores player properties depending on whether they are required.

local function saveLocalPlayerState( theFile, saveDetails )
    local player = localPlayer;

    -- Write basic details about the player instance.
    theFile:writeShort( getElementModel( player ) );
    theFile:writeInt( getPlayerMoney() );
    
    -- Next write down special details given by the game.
    local isQuickSave = saveDetails.isQuickSave and true or false;

    theFile:writeBoolean( isQuickSave );

    if not ( isQuickSave ) then
        -- Save things like player position, dimension and interior.
        local posX, posY, posZ = getElementPosition( player );
        local dimension, interior = getElementDimension( player ), getElementInterior( player );

        theFile:writeDouble( posX ); theFile:writeDouble( posY ); theFile:writeDouble( posZ );
        theFile:writeShort( dimension ); theFile:writeShort( interior );
    end

    -- Save the time this snapshot was taken.
    local realTime = getRealTime();

    theFile:writeShort( realTime.minute ); theFile:writeShort( realTime.hour );
    theFile:writeShort( realTime.monthday ); theFile:writeShort( realTime.month + 1 ); theFile:writeShort( realTime.year + 1900 );
    return true;
end

local function readBasicData( obj, operator, callback )
    local data = operator(obj);

    if ( data ) then
        callback( data );
    end
end

-- Function to restore details from a snapshot file stream.
local function loadLocalPlayerState( theFile )
    local player = localPlayer;
    local saveDetails = {};

    -- Restore the basic information.
    readBasicData( theFile, theFile.readShort, function(model) setElementModel( player, model ); end );
    readBasicData( theFile, theFile.readInt, function(money) setPlayerMoney(money); end );

    -- Read advanced parameters,
    local isQuickSave = theFile:readBoolean();
    saveDetails.isQuickSave = isQuickSave;

    if not ( isQuickSave ) then
        -- Restore advanced details.
        local posX, posY, posZ = theFile:readDouble(), theFile:readDouble(), theFile:readDouble();
        local dimension, interior = theFile:readShort(), theFile:readShort();

        -- Set them, if read correctly.
        if ( interior ) then
            setElementPosition( player, posX, posY, posZ );
            setElementDimension( player, dimension );
            setElementInterior( player, interior );
        end
    end

    -- Get the time the snapshot was taken at.
    saveDetails.saveTime = {
        minute = theFile:readShort(),
        hour = theFile:readShort(),
        monthday = theFile:readShort(),
        month = theFile:readShort(),
        year = theFile:readShort()
    };

    -- Return the save details so the runtime can react accordingly.
    return saveDetails;
end

-- Command to save a complete snapshot of the player.
addCommandHandler( "save",
    function()
        local saveDetails =
        {
            isQuickSave = false
        };

        local saveFile = fileCreate( "savestate.dat" );

        if ( saveFile ) then
            saveLocalPlayerState( saveFile, saveDetails );

            saveFile:'''Bold text'''destroy();

            outputChatBox( "successfully saved complete player status" );
        end
    end
);

-- Command to save a quick savestate.
addCommandHandler( "qsave",
    function()
        local saveDetails =
        {
            isQuickSave = true
        };

        local saveFile = fileCreate( "savestate.dat" );

        if ( saveFile ) then
            saveLocalPlayerState( saveFile, saveDetails );

            saveFile:destroy();

            outputChatBox( "successfully saved quick player status" );
        end
    end
);

-- Command to load a savestate from disk.
addCommandHandler( "loadstate",
    function()
        if not ( fileExists( "savestate.dat" ) ) then
            return;
        end

        local saveFile = fileOpen( "savestate.dat" );

        if ( saveFile ) then
            local saveData = loadLocalPlayerState( saveFile );

            saveFile:destroy();

            outputChatBox( "restored from a save (date was " .. saveData.saveTime.monthday .. "." .. saveData.saveTime.month .. "." .. saveData.saveTime.year .. ")" );
        end
    end
);

FileSystem File Functions