MTA:Eir/FileSystem/file/readDouble

From Multi Theft Auto: Wiki

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

Syntax

double file.readDouble ()

Returns

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

Example

Click to collapse [-]
Server

This snippet demonstrates a basic binary object map format. It can be extended to support more parameters.

-- Grab some objects to store into our map.
local storeObjects = getElementsByType( "object" );

-- Function to store data about objects.
local function saveObjects( theFile, objects )
    -- Write the amount of entries into the stream.
    theFile.writeShort( #objects );

    -- Now add every object into it.
    for m,n in ipairs( objects ) do
        local posX, posY, posZ = getElementPosition( n );
        local rotX, rotY, rotZ = getElementRotation( n );
        local model = getElementModel( n );
        local dimension, interior = getElementDimension( n ), getElementInterior( n );

        -- Write the parameters of the object into the stream.
        theFile.writeDouble( posX ); theFile.writeDouble( posY ); theFile.writeDouble( posZ );
        theFile.writeDouble( rotX ); theFile.writeDouble( rotY ); theFile.writeDouble( rotZ );
        theFile.writeShort( model );
        theFile.writeShort( dimension ); theFile.writeShort( interior );
    end
end

-- Function to load back data from a file stream.
local function loadObjects( theFile )
    -- Get the amount of entries.
    local objectCount = theFile.readShort();

    -- Create the objects.
    local n = 1;

    while ( n <= objectCount ) do
        local posX, posY, posZ = theFile.readDouble(), theFile.readDouble(), theFile.readDouble();
        local rotX, rotY, rotZ = theFile.readDouble(), theFile.readDouble(), theFile.readDouble();
        local model = theFile.readShort();
        local dimension, interior = theFile.readShort(), theFile.readShort();

        -- Create this particular object, if all values are properly read.
        -- We can optimize this condition by checking the last value that was read.
        if ( interior ) then
            local object = createObject( model, posX, posY, posZ, rotX, rotY, rotZ );
            
            if ( object ) then
                -- Continue applying advanced properties of creation of object succeeded.
                setElementDimension( object, dimension );
                setElementInterior( object, interior );
            end
        end
    end
end

-- Save our current objects into a file.
local saveFile = fileCreate( "world_objects.dat" );

saveObjects( saveFile, storeObjects );

-- todo: maybe load the objects.

FileSystem File Functions