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

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function attempts to read a double (native type) from a file and return it. The amount of bytes read should be eight. ==Syntax== <syntaxhighlight lang="lua"> double file.readDou...")
 
mNo edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
double file.readDouble ()
double file:readDouble ()
</syntaxhighlight>
</syntaxhighlight>


Line 30: Line 30:


         -- Write the parameters of the object into the stream.
         -- Write the parameters of the object into the stream.
         theFile.writeDouble( posX ); theFile.writeDouble( posY ); theFile.writeDouble( posZ );
         theFile:writeDouble( posX ); theFile:writeDouble( posY ); theFile:writeDouble( posZ );
         theFile.writeDouble( rotX ); theFile.writeDouble( rotY ); theFile.writeDouble( rotZ );
         theFile:writeDouble( rotX ); theFile:writeDouble( rotY ); theFile:writeDouble( rotZ );
         theFile.writeShort( model );
         theFile:writeShort( model );
         theFile.writeShort( dimension ); theFile.writeShort( interior );
         theFile:writeShort( dimension ); theFile:writeShort( interior );
     end
     end
end
end
Line 40: Line 40:
local function loadObjects( theFile )
local function loadObjects( theFile )
     -- Get the amount of entries.
     -- Get the amount of entries.
     local objectCount = theFile.readShort();
     local objectCount = theFile:readShort();


     -- Create the objects.
     -- Create the objects.
Line 46: Line 46:


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


         -- Create this particular object, if all values are properly read.
         -- Create this particular object, if all values are properly read.

Latest revision as of 23:42, 16 January 2022

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