This Defold native extension implements functions to manipulate tables in Lua scripts.
Warning: in Lua, table values are not guaranteed to have a stable order.
-
etable.empty(table)
-
Clears all values from the given table.
-
etable.remove(table, key)
-
Removes a value with a specific key from a table.
-
etable.index_of(table, value)
-
Finds the index/key of the first occurrence of a value in the given table.
-
etable.slice(table, start_index, end_index)
-
Creates a new table containing a subset of the elements from an existing table.
- Edit game.project
- Add dependency
https://github.com/thinknathan/defold-etable/archive/main.zip
for the current version- Or add a specific release
This extension includes types for use with TypeScript + Defold.
- Install these types
yarn add git+https://git@github.com/thinknathan/defold-etable.git#^1.0.0 -D
# or
npm install git+https://git@github.com/thinknathan/defold-etable.git#^1.0.0 --save-dev
- Add
defold-etable
totypes
intsconfig.json
{
"compilerOptions": {
"types": [
+ "defold-etable",
],
}
}
- Add
node_modules/@types
totypeRoots
intsconfig.json
if it's not already there
{
"compilerOptions": {
"typeRoots": [
+ "node_modules/@types",
],
}
}
-- Sample table
local myTable = { "apple", "banana", "cherry", "date", "fig" }
-- index_of
-- Finds the index/key of the first occurrence of a value in the given table.
-- If the table is structured like an array, the numerical index is returned.
-- If the table is structured in key/value pairs, the key is returned.
local index = etable.index_of(myTable, "banana") -- Result: Index of 'banana': 2
-- remove
-- Removes a value with a specific key from a table.
etable.remove(myTable, 4) -- Result: After remove: apple, banana, cherry, fig
-- slice
-- Creates a new table containing a subset of the elements from an existing table.
-- Start: The starting index
-- End: The ending index (exclusive)
local slicedTable = etable.slice(myTable, 2, 4) -- Result: Sliced table: banana, cherry, date
-- empty
-- Clears all values from the given table.
etable.empty(myTable) -- Result: After empty: { }