Buffer:setData
EditCopies data to the Buffer from either a table, Blob
, or Buffer
.
Arguments
Name | Type | Default | Description |
table | table | A flat or nested table of items to copy to the Buffer (see notes for format). | |
destinationIndex | number | 1 | The index of the first value in the Buffer to update. |
sourceIndex | number | 1 | The index in the table to copy from. |
count | number | nil |
The number of items to copy. nil will copy as many items as possible, based on the lengths of the source and destination.
|
Returns
Nothing
Copies a single field to a buffer with numbers (buffer length must be 1).
Arguments
Name | Type | Description |
...numbers | number | Numerical components to copy to the buffer. |
Returns
Nothing
Copies a single vector to a buffer (buffer length must be 1).
Arguments
Name | Type | Description |
vector | * | Vector objects to copy to the buffer. |
Returns
Nothing
Arguments
Name | Type | Default | Description |
blob | Blob | The Blob to copy data from. | |
destinationOffset | number | 0 | The byte offset to copy to. |
sourceOffset | number | 0 | The byte offset to copy from. |
size | number | nil | The number of bytes to copy. If nil, copies as many bytes as possible. |
Returns
Nothing
Arguments
Name | Type | Default | Description |
buffer | Buffer | The Buffer to copy data from. | |
destinationOffset | number | 0 | The byte offset to copy to. |
sourceOffset | number | 0 | The byte offset to copy from. |
size | number | nil | The number of bytes to copy. If nil, copies as many bytes as possible. |
Returns
Nothing
Notes
One gotcha is that unspecified fields will be set to zero. Here's an example:
buffer = lovr.graphics.newBuffer({{ 'x', 'int' }, { 'y', 'int' }})
buffer:setData({ x = 1, y = 1 }) -- set the data
buffer:setData({ x = 1 }) -- set the data, partially
-- buffer data is now {x=1, y=0}!
This doesn't apply to separate items in the buffer. For example, if the Buffer's length is 2 and only the 1st item is set, the second item will remain undisturbed:
buffer = lovr.graphics.newBuffer({{ 'x', 'int' }, { 'y', 'int' }}, 2)
buffer:setData({{ x = 1, y = 1 }, { x = 2, y = 2 }}) -- set the data
buffer:setData({{ x = 1 }}) -- set one item, partially
-- buffer data is now {{x=1, y=0}, {x=2, y=2}}
Example
Various examples of copying different formats.
function lovr.load()
buffer = lovr.graphics.newBuffer('int', 3)
buffer:setData({ 1, 2, 3 })
buffer = lovr.graphics.newBuffer('vec3', 2)
buffer:setData({ 1,2,3, 4,5,6 })
buffer:setData({ { 1, 2, 3 }, { 4, 5, 6 } })
buffer:setData({ vec3(1, 2, 3), vec3(4, 5, 6) })
-- When the Buffer's length is 1, wrapping in table is optional:
buffer = lovr.graphics.newBuffer('vec3')
buffer:setData(1, 2, 3)
buffer:setData(vec3(1, 2, 3))
-- Same for key-value structs
buffer = lovr.graphics.newBuffer({
{ 'x', 'float' },
{ 'y', 'float' }
})
buffer:setData({ x = 1, y = 2 })
-- Key/value formats
buffer = lovr.graphics.newBuffer({
{ 'x', 'float' },
{ 'y', 'float' }
}, 3)
buffer:setData({
{ x = 1, y = 2 },
{ x = 3, y = 4 },
{ x = 5, y = 6 }
})
buffer:setData({ 1, 2, 3, 4, 5, 6 })
buffer:setData({ { 1, 2 }, { 3, 4 }, { 5, 6 } })
-- Nested formats
buffer = lovr.graphics.newBuffer({
{ 'a', {
{'b', {
'c', 'float'
}}
}}
})
buffer:setData({ a = { b = { c = 42 } } })
-- Inner arrays
buffer = lovr.graphics.newBuffer({
{ 'positions', 'vec3', 10 },
{ 'sizes', 'float', 10 },
layout = 'std140'
})
local data = { positions = {}, sizes = {} }
for i = 1, buffer:getLength() do
data.positions[i] = vec3(i, i, i)
data.sizes[i] = i
end
buffer:setData(data)
end