Buffer.create

From GiderosMobile
Revision as of 19:58, 9 October 2026 by MoKaLux (talk | contribs)

Available since: Gideros 2025.1
Class: buffer_luau

Description

Creates a buffer of the requested size with all bytes initialized to 0. Size limit is 1 GiB, or 1,073,741,824 bytes. Keep in mind that larger buffers might fail to allocate if device is running low on memory.

(buffer) = buffer.create(size)

Parameters

size: (number) the size of the buffer

Return values

Returns (buffer) the created buffer

Examples

-- creates a buffer of the requested size with all bytes initialized to 0
local b: buffer = buffer.create(16)
print(buffer.len(b))

Real-Time Plasma Effect Example AI

-- 1. Setup small dimensions for a retro pixelated feel 
-- (Fewer pixels = significantly faster calculations per frame)
local width = 64
local height = 64

-- 2. Allocate a fixed block of memory buffer for the pixels (Width * Height * 4 bytes for RGBA)
local totalBytes = width * height * 4
local pixelBuffer = buffer.create(totalBytes)

-- 3. Create a Bitmap to display our dynamic texture
local texture = Texture.new(pixelBuffer, width, height, false) -- false = sharp retro pixel filtering
local displayScreen = Bitmap.new(texture)

-- Scale up the 64x64 grid so it fills a large portion of the screen nicely
displayScreen:setScale(8)
displayScreen:setPosition(50, 50)
stage:addChild(displayScreen)

-- 4. Mathematical variables for the plasma waves
local time = 0

-- 5. The Real-time Game Loop
local function onEnterFrame(event)
    time = time + 0.04 -- Controls speed of the plasma animation
    
    local offset = 0
    for y = 0, height - 1 do
        -- Pre-calculate normalized Y coordinates (-1.0 to 1.0)
        local ny = (y / height) * 2 - 1
        
        for x = 0, width - 1 do
            -- Pre-calculate normalized X coordinates (-1.0 to 1.0)
            local nx = (x / width) * 2 - 1
            
            -- Combine multiple sine waves to generate an organic fluid plasma pattern
            local wave1 = math.sin(nx * 4 + time)
            local wave2 = math.sin(ny * 4 + math.cos(time))
            local wave3 = math.sin(math.sqrt(nx*nx + ny*ny) * 6 - time)
            
            -- Combine and normalize final value between 0.0 and 1.0
            local plasmaValue = (wave1 + wave2 + wave3) / 3
            plasmaValue = (plasmaValue + 1) / 2 
            
            -- Map the plasma value into standard retro colors (e.g., Psychedelic Purple & Cyan)
            local r = math.floor(plasmaValue * 255)
            local g = math.floor((1 - plasmaValue) * 128)
            local b = math.floor(math.sin(plasmaValue * math.pi) * 255)
            local a = 255 -- Fully opaque
            
            -- Write directly to memory offsets via Luau's high-speed buffer writes
            buffer.writeu8(pixelBuffer, offset, r)
            buffer.writeu8(pixelBuffer, offset + 1, g)
            buffer.writeu8(pixelBuffer, offset + 2, b)
            buffer.writeu8(pixelBuffer, offset + 3, a)
            
            offset = offset + 4
        end
    end
    
    -- 6. Re-upload the newly updated buffer straight to the GPU
    texture:update(pixelBuffer, width, height) 
end

-- Hook up the loop execution
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)