Download

FArrayWriter.Empty() doesn't seem to work correctly

Hello,

I am trying to use FArrayWriter to serialize a sequence of UDP packets in a loop. I would like to allocate one writer of fixed size, i.e. 1500 and then repeatedly reuse the allocated space each time through the loop. Here is the relevant snippet:

FTerrainServerPacketSendTile SendTile;
    FArrayWriter SendBuffer;
    // Cycle through all of the requested tiles
    for (size_t iTile = 0; iTile < GetTiles.N_tiles; iTile++)
    {
        // Indicies into the array of tile indices
        auto iix = iTile * 2;
        auto iiy = iix + 1;

        // Tile grid indices
        auto ix = GetTiles.tiles[iix];
        auto iy = GetTiles.tiles[iiy];

        QueryTile(GetTiles, ix, iy, SendTile);
        SendBuffer.Empty(1500); // <-- This does not seem to work.
        SendTile.Serialize(SendBuffer);
        if (!this->UDPSender->TrySendUDPMessage(SendBuffer, this->SendIPAddress, this->SendPort))
        {
            UE_LOG(LogTerrainServerActor, Error, TEXT("DoCommandGetTiles: Failed to send a UDP message"));
        }
    }

According to the documentation and source the Empty() method should reset the buffer pointer to 0, but the buffer keeps on growing. My packet is 1454 bytes. But each time through the loop the packet keeps growing by 1454 at each pass. I have also tried the SetNum() with the same result.

I am using UE4.25.4.

Any help would be appreciated.

Thank you.

Mark