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

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


Line 15: Line 15:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local function writeFileString( theFile, string )
local function writeFileString( theFile, string )
     theFile.writeShort( #string );
     theFile:writeShort( #string );
     theFile.write( string );
     theFile:write( string );
end
end


local function readFileString( theFile )
local function readFileString( theFile )
     local stringLen = theFile.readShort();
     local stringLen = theFile:readShort();


     if not ( stringLen ) then
     if not ( stringLen ) then
Line 31: Line 31:
local function saveVehicleHandling( theFile, handlingDict )
local function saveVehicleHandling( theFile, handlingDict )
     -- Write down the details.
     -- Write down the details.
     theFile.writeDouble( handlingDict.mass );
     theFile:writeDouble( handlingDict.mass );
     theFile.writeDouble( handlingDict.turnMass );
     theFile:writeDouble( handlingDict.turnMass );
     theFile.writeFloat( handlingDict.dragCoeff );
     theFile:writeFloat( handlingDict.dragCoeff );


     theFile.writeFloat( handlingDict.centerOfMass[1] );
     theFile:writeFloat( handlingDict.centerOfMass[1] );
     theFile.writeFloat( handlingDict.centerOfMass[2] );
     theFile:writeFloat( handlingDict.centerOfMass[2] );
     theFile.writeFloat( handlingDict.centerOfMass[3] );
     theFile:writeFloat( handlingDict.centerOfMass[3] );


     theFile.writeInt( handlingDict.percentSubmerged );
     theFile:writeInt( handlingDict.percentSubmerged );
     theFile.writeDouble( handlingDict.tractionMultiplier );
     theFile:writeDouble( handlingDict.tractionMultiplier );
     theFile.writeFloat( handlingDict.tractionLoss );
     theFile:writeFloat( handlingDict.tractionLoss );
     theFile.writeFloat( handlingDict.tractionBias );
     theFile:writeFloat( handlingDict.tractionBias );
     theFile.writeShort( handlingDict.numberOfGears );
     theFile:writeShort( handlingDict.numberOfGears );
     theFile.writeDouble( handlingDict.maxVelocity );
     theFile:writeDouble( handlingDict.maxVelocity );
     theFile.writeDouble( handlingDict.engineAcceleration );
     theFile:writeDouble( handlingDict.engineAcceleration );
     theFile.writeFloat( handlingDict.engineInertia );
     theFile:writeFloat( handlingDict.engineInertia );
      
      
     writeFileString( theFile, tostring( handlingDict.driveType ) );
     writeFileString( theFile, tostring( handlingDict.driveType ) );
     writeFileString( theFile, tostring( handlingDict.engineType ) );
     writeFileString( theFile, tostring( handlingDict.engineType ) );


     theFile.writeDouble( handlingDict.brakeDeceleration );
     theFile:writeDouble( handlingDict.brakeDeceleration );
     theFile.writeBoolean( handlingDict.ABS );
     theFile:writeBoolean( handlingDict.ABS );
     theFile.writeFloat( handlingDict.steeringLock );
     theFile:writeFloat( handlingDict.steeringLock );
     theFile.writeFloat( handlingDict.suspensionForceLevel );
     theFile:writeFloat( handlingDict.suspensionForceLevel );
     theFile.writeFloat( handlingDict.suspensionDamping );
     theFile:writeFloat( handlingDict.suspensionDamping );
     theFile.writeFloat( handlingDict.suspensionHighSpeedDamping );
     theFile:writeFloat( handlingDict.suspensionHighSpeedDamping );
     theFile.writeFloat( handlingDict.suspensionUpperLimit );
     theFile:writeFloat( handlingDict.suspensionUpperLimit );
     theFile.writeFloat( handlingDict.suspensionLowerLimit );
     theFile:writeFloat( handlingDict.suspensionLowerLimit );
     theFile.writeFloat( handlingDict.suspensionFrontRearBias );
     theFile:writeFloat( handlingDict.suspensionFrontRearBias );
     theFile.writeFloat( handlingDict.suspensionAntiDiveMultiplier );
     theFile:writeFloat( handlingDict.suspensionAntiDiveMultiplier );
     theFile.writeFloat( handlingDict.seatOffsetDistance );
     theFile:writeFloat( handlingDict.seatOffsetDistance );
     theFile.writeFloat( handlingDict.collisionDamageMultiplier );
     theFile:writeFloat( handlingDict.collisionDamageMultiplier );
     theFile.writeInt( handlingDict.monetary );
     theFile:writeInt( handlingDict.monetary );
     theFile.writeInt( handlingDict.modelFlags );
     theFile:writeInt( handlingDict.modelFlags );
     theFile.writeInt( handlingDict.handlingFlags );
     theFile:writeInt( handlingDict.handlingFlags );
      
      
     writeFileString( theFile, handlingDict.headLight );
     writeFileString( theFile, handlingDict.headLight );
     writeFileString( theFile, handlingDict.tailLight );
     writeFileString( theFile, handlingDict.tailLight );
      
      
     theFile.writeShort( handlingDict.animGroup );
     theFile:writeShort( handlingDict.animGroup );
end
end


local function attemptFileRead( theOperation, defaultOut )
local function attemptFileRead( theFile, theOperation, defaultOut )
     local output = theOperation();
     local output = theOperation( theFile );


     if not ( output ) then
     if not ( output ) then
Line 85: Line 85:
local function loadVehicleHandling( theFile )
local function loadVehicleHandling( theFile )
     local handlingEntry = {
     local handlingEntry = {
         mass = attemptFileRead( theFile.readDouble, 1000 ),
         mass = attemptFileRead( theFile, theFile.readDouble, 1000 ),
         turnMass = attemptFileRead( theFile.readDouble, 1000 ),
         turnMass = attemptFileRead( theFile, theFile.readDouble, 1000 ),
         dragCoeff = attemptFileRead( theFile.readFloat, 0 ),
         dragCoeff = attemptFileRead( theFile, theFile.readFloat, 0 ),


         {
         {
             [1] = attemptFileRead( theFile.readFloat, 0 ),
             [1] = attemptFileRead( theFile, theFile.readFloat, 0 ),
             [2] = attemptFileRead( theFile.readFloat, 0 ),
             [2] = attemptFileRead( theFile, theFile.readFloat, 0 ),
             [3] = attemptFileRead( theFile.readFloat, 0 )
             [3] = attemptFileRead( theFile, theFile.readFloat, 0 )
         },
         },


         percentSubmerged = attemptFileRead( theFile.readInt, 100 ),
         percentSubmerged = attemptFileRead( theFile, theFile.readInt, 100 ),
         tractionMultiplier = attemptFileRead( theFile.readDouble, 1.0 ),
         tractionMultiplier = attemptFileRead( theFile, theFile.readDouble, 1.0 ),
         tractionLoss = attemptFileRead( theFile.readFloat, 1.0 ),
         tractionLoss = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
         tractionBias = attemptFileRead( theFile.readFloat, 1.0 ),
         tractionBias = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
         numberOfGears = attemptFileRead( theFile.readShort, 4 ),
         numberOfGears = attemptFileRead( theFile, theFile.readShort, 4 ),
         maxVelocity = attemptFileRead( theFile.readDouble, 240 ),
         maxVelocity = attemptFileRead( theFile, theFile.readDouble, 240 ),
         engineAcceleration = attemptFileRead( theFile.readDouble, 9 ),
         engineAcceleration = attemptFileRead( theFile, theFile.readDouble, 9 ),
         engineInertia = attemptFileRead( theFile.readFloat, 3 ),
         engineInertia = attemptFileRead( theFile, theFile.readFloat, 3 ),
          
          
         driveType = attemptFileRead( function() return readFileString( theFile ) end, "fwd" ),
         driveType = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "fwd" ),
         engineType = attemptFileRead( function() return readFileString( theFile ) end, "diesel" ),
         engineType = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "diesel" ),


         brakeDeceleration = attemptFileRead( theFile.readDouble, 5 ),
         brakeDeceleration = attemptFileRead( theFile, theFile.readDouble, 5 ),
         brakeBias = attemptFileRead( theFile.readFloat, 1.0 ),
         brakeBias = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
         ABS = attemptFileRead( theFile.readBoolean, false ),
         ABS = attemptFileRead( theFile, theFile.readBoolean, false ),
         steeringLock = attemptFileRead( theFile.readFloat, 180 ),
         steeringLock = attemptFileRead( theFile, theFile.readFloat, 180 ),
         suspensionForceLevel = attemptFileRead( theFile.readFloat, 10 ),
         suspensionForceLevel = attemptFileRead( theFile, theFile.readFloat, 10 ),
         suspensionDamping = attemptFileRead( theFile.readFloat, 10 ),
         suspensionDamping = attemptFileRead( theFile, theFile.readFloat, 10 ),
         suspensionHighSpeedDamping = attemptFileRead( theFile.readFloat, 50 ),
         suspensionHighSpeedDamping = attemptFileRead( theFile, theFile.readFloat, 50 ),
         suspensionUpperLimit = attemptFileRead( theFile.readFloat, 0 ),
         suspensionUpperLimit = attemptFileRead( theFile, theFile.readFloat, 0 ),
         suspensionLowerLimit = attemptFileRead( theFile.readFloat, 0 ),
         suspensionLowerLimit = attemptFileRead( theFile, theFile.readFloat, 0 ),
         suspensionFrontRearBias = attemptFileRead( theFile.readFloat, 0.4 ),
         suspensionFrontRearBias = attemptFileRead( theFile, theFile.readFloat, 0.4 ),
         suspensionAntiDiveMultiplier = attemptFileRead( theFile.readFloat, 10 ),
         suspensionAntiDiveMultiplier = attemptFileRead( theFile, theFile.readFloat, 10 ),
         seatOffsetDistance = attemptFileRead( theFile.readFloat, 4 ),
         seatOffsetDistance = attemptFileRead( theFile, theFile.readFloat, 4 ),
         collisionDamageMultiplier = attemptFileRead( theFile.readFloat, 0.5 ),
         collisionDamageMultiplier = attemptFileRead( theFile, theFile.readFloat, 0.5 ),
         monetary = attemptFileRead( theFile.readInt, 10000 ),
         monetary = attemptFileRead( theFile, theFile.readInt, 10000 ),
         modelFlags = attemptFileRead( theFile.readInt, 0 ),
         modelFlags = attemptFileRead( theFile, theFile.readInt, 0 ),
         handlingFlags = attemptFileRead( theFile.readInt, 0 ),
         handlingFlags = attemptFileRead( theFile, theFile.readInt, 0 ),
          
          
         headLight = attemptFileRead( function() return readFileString( theFile ) end, "big" ),
         headLight = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "big" ),
         tailLight = attemptFileRead( function() return readFileString( theFile ) end, "big" ),
         tailLight = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "big" ),


         animGroup = attemptFileRead( theFile.readShort, 1 )
         animGroup = attemptFileRead( theFile, theFile.readShort, 1 )
     };
     };


Line 146: Line 146:
                 saveVehicleHandling( handlingFile, handling );
                 saveVehicleHandling( handlingFile, handling );


                 handlingFile.destroy();
                 handlingFile:destroy();


                 outputChatBox( "successfully saved handling" );
                 outputChatBox( "successfully saved handling" );
Line 168: Line 168:
                     local handling = loadVehicleHandling( handlingFile );
                     local handling = loadVehicleHandling( handlingFile );


                     handlingFile.destroy();
                     handlingFile:destroy();


                     -- Apply the saved handling.
                     -- Apply the saved handling.

Revision as of 23:40, 16 January 2022

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

Syntax

float file:readFloat ()

Returns

Returns a float if it was successfully read from the file, false otherwise.

Example

Click to collapse [-]
Server

This snippet demonstrates a binary vehicle handling format. This saves space in comparison to a .XML based format.

local function writeFileString( theFile, string )
    theFile:writeShort( #string );
    theFile:write( string );
end

local function readFileString( theFile )
    local stringLen = theFile:readShort();

    if not ( stringLen ) then
        return false;
    end

    return theFile.read( stringLen );
end

local function saveVehicleHandling( theFile, handlingDict )
    -- Write down the details.
    theFile:writeDouble( handlingDict.mass );
    theFile:writeDouble( handlingDict.turnMass );
    theFile:writeFloat( handlingDict.dragCoeff );

    theFile:writeFloat( handlingDict.centerOfMass[1] );
    theFile:writeFloat( handlingDict.centerOfMass[2] );
    theFile:writeFloat( handlingDict.centerOfMass[3] );

    theFile:writeInt( handlingDict.percentSubmerged );
    theFile:writeDouble( handlingDict.tractionMultiplier );
    theFile:writeFloat( handlingDict.tractionLoss );
    theFile:writeFloat( handlingDict.tractionBias );
    theFile:writeShort( handlingDict.numberOfGears );
    theFile:writeDouble( handlingDict.maxVelocity );
    theFile:writeDouble( handlingDict.engineAcceleration );
    theFile:writeFloat( handlingDict.engineInertia );
    
    writeFileString( theFile, tostring( handlingDict.driveType ) );
    writeFileString( theFile, tostring( handlingDict.engineType ) );

    theFile:writeDouble( handlingDict.brakeDeceleration );
    theFile:writeBoolean( handlingDict.ABS );
    theFile:writeFloat( handlingDict.steeringLock );
    theFile:writeFloat( handlingDict.suspensionForceLevel );
    theFile:writeFloat( handlingDict.suspensionDamping );
    theFile:writeFloat( handlingDict.suspensionHighSpeedDamping );
    theFile:writeFloat( handlingDict.suspensionUpperLimit );
    theFile:writeFloat( handlingDict.suspensionLowerLimit );
    theFile:writeFloat( handlingDict.suspensionFrontRearBias );
    theFile:writeFloat( handlingDict.suspensionAntiDiveMultiplier );
    theFile:writeFloat( handlingDict.seatOffsetDistance );
    theFile:writeFloat( handlingDict.collisionDamageMultiplier );
    theFile:writeInt( handlingDict.monetary );
    theFile:writeInt( handlingDict.modelFlags );
    theFile:writeInt( handlingDict.handlingFlags );
    
    writeFileString( theFile, handlingDict.headLight );
    writeFileString( theFile, handlingDict.tailLight );
    
    theFile:writeShort( handlingDict.animGroup );
end

local function attemptFileRead( theFile, theOperation, defaultOut )
    local output = theOperation( theFile );

    if not ( output ) then
        return defaultOut;
    end

    return output;
end

local function loadVehicleHandling( theFile )
    local handlingEntry = {
        mass = attemptFileRead( theFile, theFile.readDouble, 1000 ),
        turnMass = attemptFileRead( theFile, theFile.readDouble, 1000 ),
        dragCoeff = attemptFileRead( theFile, theFile.readFloat, 0 ),

        {
            [1] = attemptFileRead( theFile, theFile.readFloat, 0 ),
            [2] = attemptFileRead( theFile, theFile.readFloat, 0 ),
            [3] = attemptFileRead( theFile, theFile.readFloat, 0 )
        },

        percentSubmerged = attemptFileRead( theFile, theFile.readInt, 100 ),
        tractionMultiplier = attemptFileRead( theFile, theFile.readDouble, 1.0 ),
        tractionLoss = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
        tractionBias = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
        numberOfGears = attemptFileRead( theFile, theFile.readShort, 4 ),
        maxVelocity = attemptFileRead( theFile, theFile.readDouble, 240 ),
        engineAcceleration = attemptFileRead( theFile, theFile.readDouble, 9 ),
        engineInertia = attemptFileRead( theFile, theFile.readFloat, 3 ),
        
        driveType = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "fwd" ),
        engineType = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "diesel" ),

        brakeDeceleration = attemptFileRead( theFile, theFile.readDouble, 5 ),
        brakeBias = attemptFileRead( theFile, theFile.readFloat, 1.0 ),
        ABS = attemptFileRead( theFile, theFile.readBoolean, false ),
        steeringLock = attemptFileRead( theFile, theFile.readFloat, 180 ),
        suspensionForceLevel = attemptFileRead( theFile, theFile.readFloat, 10 ),
        suspensionDamping = attemptFileRead( theFile, theFile.readFloat, 10 ),
        suspensionHighSpeedDamping = attemptFileRead( theFile, theFile.readFloat, 50 ),
        suspensionUpperLimit = attemptFileRead( theFile, theFile.readFloat, 0 ),
        suspensionLowerLimit = attemptFileRead( theFile, theFile.readFloat, 0 ),
        suspensionFrontRearBias = attemptFileRead( theFile, theFile.readFloat, 0.4 ),
        suspensionAntiDiveMultiplier = attemptFileRead( theFile, theFile.readFloat, 10 ),
        seatOffsetDistance = attemptFileRead( theFile, theFile.readFloat, 4 ),
        collisionDamageMultiplier = attemptFileRead( theFile, theFile.readFloat, 0.5 ),
        monetary = attemptFileRead( theFile, theFile.readInt, 10000 ),
        modelFlags = attemptFileRead( theFile, theFile.readInt, 0 ),
        handlingFlags = attemptFileRead( theFile, theFile.readInt, 0 ),
        
        headLight = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "big" ),
        tailLight = attemptFileRead( theFile, function(theFile) return readFileString( theFile ) end, "big" ),

        animGroup = attemptFileRead( theFile, theFile.readShort, 1 )
    };

    return handlingEntry;
end

-- Save the handling of your vehicle, so you can reuse it later.
addCommandHandler( "savehandling",
    function(player)
        local myVehicle = getPedOccupiedVehicle( player );

        if ( myVehicle ) then
            local handling = getVehicleHandling( myVehicle );

            local handlingFile = fileCreate( getPlayerName( player ) .. "_saved_handling.dat" );

            if ( handlingFile ) then
                saveVehicleHandling( handlingFile, handling );

                handlingFile:destroy();

                outputChatBox( "successfully saved handling" );
            end
        end
    end
);

-- Load the handling of the vehicle for reusage.
addCommandHandler( "loadhandling",
    function(player)
        local handlingName = getPlayerName( player ) .. "_saved_handling.dat";

        if ( fileExists( handlingName ) ) then
            local myVehicle = getPedOccupiedVehicle( player );

            if ( myVehicle ) then
                local handlingFile = fileOpen( handlingName );

                if ( handlingFile ) then
                    local handling = loadVehicleHandling( handlingFile );

                    handlingFile:destroy();

                    -- Apply the saved handling.
                    for m,n in pairs( handling ) do
                        setVehicleHandling( myVehicle, m, n );
                    end

                    outputChatBox( "successfully restored handling" );
                end
            end
        end
    end
);

FileSystem File Functions