Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
/ defold-etable Public archive

A native extension for Defold with functions to manipulate tables.

License

Notifications You must be signed in to change notification settings

thinknathan/defold-etable

Repository files navigation

Def-eTable

Def-eTable

Build with bob GitHub License

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.

API

  • 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.

Installation

  1. Edit game.project
  2. Add dependency https://github.com/thinknathan/defold-etable/archive/main.zip for the current version

TypeScript Definitions

This extension includes types for use with TypeScript + Defold.

  1. Install these types
yarn add git+https://[email protected]/thinknathan/defold-etable.git#^1.0.0 -D
# or
npm install git+https://[email protected]/thinknathan/defold-etable.git#^1.0.0 --save-dev
  1. Add defold-etable to types in tsconfig.json
{
	"compilerOptions": {
		"types": [
+			"defold-etable",
		],
	}
}
  1. Add node_modules/@types to typeRoots in tsconfig.json if it's not already there
{
	"compilerOptions": {
		"typeRoots": [
+			"node_modules/@types",
		],
	}
}

Usage

-- 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: { }