MTA:Eir/FileSystem/file/readFloat
Jump to navigation
Jump to search
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 [-]
ServerThis snippet demonstrates a binary vehicle handling format. This saves space in comparison to an .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( theOperation, defaultOut )
local output = theOperation();
if not ( output ) then
return defaultOut;
end
return output;
end
local function loadVehicleHandling( theFile )
local handlingEntry = {
mass = attemptFileRead( theFile.readDouble, 1000 ),
turnMass = attemptFileRead( theFile.readDouble, 1000 ),
dragCoeff = attemptFileRead( theFile.readFloat, 0 ),
{
[1] = attemptFileRead( theFile.readFloat, 0 ),
[2] = attemptFileRead( theFile.readFloat, 0 ),
[3] = attemptFileRead( theFile.readFloat, 0 )
},
percentSubmerged = attemptFileRead( theFile.readInt, 100 ),
tractionMultiplier = attemptFileRead( theFile.readDouble, 1.0 ),
tractionLoss = attemptFileRead( theFile.readFloat, 1.0 ),
tractionBias = attemptFileRead( theFile.readFloat, 1.0 ),
numberOfGears = attemptFileRead( theFile.readShort, 4 ),
maxVelocity = attemptFileRead( theFile.readDouble, 240 ),
engineAcceleration = attemptFileRead( theFile.readDouble, 9 ),
engineInertia = attemptFileRead( theFile.readFloat, 3 ),
driveType = attemptFileRead( function() return readFileString( theFile ) end, "fwd" ),
engineType = attemptFileRead( function() return readFileString( theFile ) end, "diesel" ),
brakeDeceleration = attemptFileRead( theFile.readDouble, 5 ),
brakeBias = attemptFileRead( theFile.readFloat, 1.0 ),
ABS = attemptFileRead( theFile.readBoolean, false ),
steeringLock = attemptFileRead( theFile.readFloat, 180 ),
suspensionForceLevel = attemptFileRead( theFile.readFloat, 10 ),
suspensionDamping = attemptFileRead( theFile.readFloat, 10 ),
suspensionHighSpeedDamping = attemptFileRead( theFile.readFloat, 50 ),
suspensionUpperLimit = attemptFileRead( theFile.readFloat, 0 ),
suspensionLowerLimit = attemptFileRead( theFile.readFloat, 0 ),
suspensionFrontRearBias = attemptFileRead( theFile.readFloat, 0.4 ),
suspensionAntiDiveMultiplier = attemptFileRead( theFile.readFloat, 10 ),
seatOffsetDistance = attemptFileRead( theFile.readFloat, 4 ),
collisionDamageMultiplier = attemptFileRead( theFile.readFloat, 0.5 ),
monetary = attemptFileRead( theFile.readInt, 10000 ),
modelFlags = attemptFileRead( theFile.readInt, 0 ),
handlingFlags = attemptFileRead( theFile.readInt, 0 ),
headLight = attemptFileRead( function() return readFileString( theFile ) end, "big" ),
tailLight = attemptFileRead( function() return readFileString( theFile ) end, "big" ),
animGroup = attemptFileRead( 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
);