Buffer.create
From GiderosMobile
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)
Real-Time Retro Camera Filter Example AI
-- 1. Setup dimensions for a retro viewport resolution
local width = 128
local height = 128
-- 2. Allocate the high-speed data buffer (Width * Height * 4 bytes for RGBA)
local totalBytes = width * height * 4
local pixelBuffer = buffer.create(totalBytes)
-- 3. Create the filter texture layer and overlay bitmap
local filterTexture = Texture.new(pixelBuffer, width, height, false) -- false = pixelated edges
local cameraOverlay = Bitmap.new(filterTexture)
-- Stretch it to fill a massive portion of the display canvas smoothly
cameraOverlay:setScale(4)
cameraOverlay:setPosition(50, 50)
stage:addChild(cameraOverlay)
-- 4. Tracking loop variable for moving interference/rolling lines
local frameCount = 0
-- 5. The Active Processing Loop
local function onEnterFrame(event)
frameCount = frameCount + 1
local offset = 0
for y = 0, height - 1 do
-- Math logic to calculate CRT Scanline darkness based on screen position
-- This creates a repeating grid pattern every few pixels
local scanlineIntensity = 1.0
if y % 3 == 0 then
scanlineIntensity = 0.65 -- Make every third horizontal row darker
end
-- Create a rolling tracking line that slowly crawls down the screen
local rollingLinePos = math.floor(frameCount * 0.5) % height
if math.abs(y - rollingLinePos) < 2 then
scanlineIntensity = scanlineIntensity * 0.4 -- Heavy visual darkening/distortion strip
end
for x = 0, width - 1 do
-- Generates dynamic monochrome noise (values from -30 to +30)
-- math.random() adds a grainy analog feel that updates instantly per frame
local analogGrain = math.random(-30, 30)
-- Base Scene Color (A vibrant green-tinted night-vision/retro terminal look)
local baseR = 10
local baseG = 180
local baseB = 40
-- Combine base colors with the noise grain, clamped between 0 and 255
local r = math.clamp(baseR + analogGrain, 0, 255)
local g = math.clamp(baseG + analogGrain, 0, 255)
local b = math.clamp(baseB + analogGrain, 0, 255)
-- Apply the scanline and tracking multiplier to the color values
r = math.floor(r * scanlineIntensity)
g = math.floor(g * scanlineIntensity)
b = math.floor(b * scanlineIntensity)
local a = 255 -- Opaque filter overlay
-- Push modifications directly to the memory stack
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. Upload buffer with explicit width and height parameters to update the GPU texture
filterTexture:update(pixelBuffer, width, height)
end
-- Hook up the execution framework
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)