Skip to content

table_util

Source: base/tealdef/scripts/table_util.d.tl

Notes

Types of /scripts/table_util.tl (TableUtil): deep comparison and copying of tables and simple array search helpers. Load it with local table_util = ug_require "/scripts/table_util.tl" as TableUtil.

TableUtil

record global base/tealdef/scripts/table_util.d.tl:3

global record TableUtil

Load: local table_util = ug_require "::/scripts/table_util.tl" as TableUtil

Notes

Table helpers returned by ug_require "/scripts/table_util.tl".

Functions

deepEquals(arg1 : any, arg2 : any, allowFunctions? : boolean, key? : any) : boolean base/tealdef/scripts/table_util.d.tl:4

Call as table_util.deepEquals(…)

Notes

Recursively compares two values; tables are equal when they have the same keys with deeply equal values.

Parameter Type Description
arg1 any First value.
arg2 any Second value.
allowFunctions? boolean If true, functions are compared by identity. Otherwise comparing functions logs an error, unless both tables carry a <key>_compareProxy sibling value (compared instead) or the key ends in ecipe.
key? any Key under which the values are stored; used for function comparison and error messages.

Returns boolean: True if the values are deeply equal.

Details

Userdata values must implement an equals method; it is called for the comparison.

Used in the base game: 140 times in 50 files

base/content/base/base/difficulty_util.tl:213

if table_util.deepEquals(current, preset) then
base/content/game_mechanics/game_mechanics/achievements/achievements.script.tl:501
if not table_util.deepEquals(newSfxEarned, achievementGuiState.earnedSFX) then
base/content/game_mechanics/game_mechanics/company/company_react_util.tl:180
if prev == nil or not table_util.deepEquals(prev, def, true) then
base/content/game_mechanics/game_mechanics/finance/finances_charts.tl:50
if table_util.deepEquals(state, newState) then
base/content/game_mechanics/game_mechanics/notifications/notification_util.tl:229
if table_util.deepEquals(newEntitiesAndParam.entities, entry.persisting) then
base/content/game_mechanics/game_mechanics/towns/town_growth.script.tl:185
elseif not table_util.deepEquals(scriptState.cargoDemandsSorted, allCargoTypesList) then
… and 44 more files.

copy<T>(obj : T) : T base/tealdef/scripts/table_util.d.tl:5

Call as table_util.copy(…)

Notes

Deep copy of a table (keys and values copied recursively); non-table values are returned unchanged.

Returns T

Details

Metatables are not copied, so vectors from vec3.new lose their operators.

Used in the base game: 238 times in 67 files

base/content/climates/climates/gen/mapgenutil.tl:126

local riverMapBackup = table_util.copy(config.map.riverMap)
base/content/game_mechanics/game_mechanics/celebrations/celebration_react_util.tl:155
local newState = table_util.copy(currentState)
base/content/game_mechanics/game_mechanics/company/company.script.tl:29
local newConsumedPermits : {ResName : {integer}} = table_util.copy(companyState.consumedPermits)
base/content/game_mechanics/game_mechanics/finance/finances_react_util.tl:194
local isNotTransported = table_util.copy(isNotTransportedState:old())
base/content/game_mechanics/game_mechanics/fun_elements/fireworks.script.tl:71
local copy = table_util.copy(rocketParams)
base/content/game_mechanics/game_mechanics/notifications/gui/notification_log.tl:384
local newState = table_util.copy(current)
… and 61 more files.

shallowCopy<T>(obj : T) : T base/tealdef/scripts/table_util.d.tl:6

Call as table_util.shallowCopy(…)

Notes

Copies the top-level key/value pairs of a table into a new table; nested tables are shared.

Returns T

Used in the base game: 67 times in 20 files

base/content/game_mechanics/game_mechanics/finance/finances_charts.tl:48

local newState = table_util.shallowCopy(state)
base/content/gui/gui/construction/construction.tl:1321
local newQueue = table_util.shallowCopy(newParamsQueue:old())
base/content/gui/gui/construction/construction_react_util.tl:4045
local definition = doCopy and table_util.shallowCopy(originalDefinition) or originalDefinition
base/content/gui/gui/entity_window/bridge_and_tunnel.tl:105
local newState = table_util.shallowCopy(state)
base/content/gui/gui/entity_window/industry/industry.tl:285
local eowState = table_util.shallowCopy(state:old())
base/content/gui/gui/entity_window/make_entity_window.tl:259
local entityWindowContentParams = table_util.shallowCopy(res.param)
… and 14 more files.

arrayFind<T>(arr : {T}, value : T) : integer base/tealdef/scripts/table_util.d.tl:7

Call as table_util.arrayFind(…)

Notes

Index of the first element equal (==) to value.

Returns integer: The 1-based index, or 0 if the value is not in the array.

Used in the base game: 13 times in 7 files

base/content/gui/gui/entity_window/double_slip_switch.tl:118

local locked = table_util.arrayFind(selectedState.lockedLanes, lane) > 0
base/content/gui/gui/game_bar/game_bar_widgets.tl:353
local selectedIndex = table_util.arrayFind(calSpeedFactors, currentFactor)
base/content/gui/gui/main/industry_util.tl:568
if table_util.arrayFind(segmentsToRemove, segment) == 0 then
base/content/gui/gui/main/town_hud_react_util.tl:16
local index = table_util.arrayFind(result, c[1])
base/content/gui/gui/menu/mod_manager_react_util.tl:2335
local notApplied = table_util.arrayFind(params.tags, tag) == 0
base/content/mission/mission/mission_framework/mission_framework_react.tl:1059
local index = table_util.arrayFind(queue, taskUid)
… and 1 more files.

arrayFindIf<T>(arr : {T}, pred : function(T) : boolean) : integer base/tealdef/scripts/table_util.d.tl:8

Call as table_util.arrayFindIf(…)

Notes

Index of the first element for which pred returns true.

Returns integer: The 1-based index, or 0 if no element matches.

Used in the base game: 5 times in 4 files

base/content/game_mechanics/game_mechanics/company/explorations/exploration_plane.script.tl:300

if table_util.arrayFindIf(explorationPlaneState.explorationPlanes,
base/content/gui/gui/construction/construction.tl:970
local index = table_util.arrayFindIf(objects, function(object : MenuCategoryUtil.FilterObject) : boolean
base/content/gui/gui/entity_window/bridge_and_tunnel.tl:109
if table_util.arrayFindIf(newState.availableObjects, function(object : MenuCategoryUtil.FilterObject) : boolean
base/content/gui/gui/main/menu_category_util.tl:368
local buttonIndex = table_util.arrayFindIf(param.filterBoxParam.objectsSorted, function(object : MenuCategoryUtil.FilterObject) : boolean

arrayRemove<T>(arr : {T}, value : T) : boolean base/tealdef/scripts/table_util.d.tl:9

Call as table_util.arrayRemove(…)

Notes

Removes the first element equal to value from the array, in place.

Returns boolean: True if an element was removed.

Used in the base game: 1 time in 1 file

base/content/economy/economy/placementparamsutil.lua:665

table_util.arrayRemove(placementParam.tags, tag)

arrayContains<T>(arr : {T}, value : T) : boolean base/tealdef/scripts/table_util.d.tl:10

Call as table_util.arrayContains(…)

Notes

True if the array contains an element equal (==) to value.

Returns boolean

Used in the base game: 104 times in 32 files

base/content/game_mechanics/game_mechanics/achievements/achievements.script.tl:230

if IsDefaultModCargo(cargoName) and not table_util.arrayContains(achievementState.cargoTypesDelivered, cargoName) then
base/content/game_mechanics/game_mechanics/notifications/types/con_availability.script.tl:20
if table_util.arrayContains(notificationParams.categories, "street_constructions") then
base/content/game_mechanics/game_mechanics/towns/town_growth.script.tl:256
if table_util.arrayContains(cat.landUses, landUseType) then
base/content/gui/gui/construction/construction.tl:3350
and table_util.arrayContains(cargoSpecializationClasses, "UNIVERSAL")
base/content/gui/gui/construction/construction_react_util.tl:317
add = table_util.arrayContains(avail.carriers, carrier)
base/content/gui/gui/construction/tools/mixed_assets.script.tl:7
if table_util.arrayContains(category.filterCategories, filter) then
… and 26 more files.

find<K, V>(dict : {{K, V}}, key : K) : V base/tealdef/scripts/table_util.d.tl:11

Call as table_util.find(…)

Notes

Looks up key in a list of {key, value} pairs.

Returns V: The value of the first pair whose first element equals key, or nil.

Used in the base game: 5 times in 2 files

base/content/gui/gui/menu/new_game_or_map_settings_page.tl:94

map.climateId = table_util.find(configDict, "climate")
base/content/gui/gui/menu/new_game_react_util.tl:207
local climateId = table_util.find(configDict, "climate")

concatenate<T>(...: {T}) : {T} base/tealdef/scripts/table_util.d.tl:12

Call as table_util.concatenate(…)

Notes

Returns a new array with the elements of all given arrays, in order.

Returns {T}

Used in the base game: 28 times in 16 files

base/content/gui/gui/entity_window/industry/industry.tl:300

widgets = table_util.concatenate(widgets, gui_react_util.usePlugins(industry_eow.IndustryEowExtensionPoint, { firstRef = initialFocusRef, }, extensionParams))
base/content/gui/gui/entity_window/vehicle/vehicle_eow.script.tl:139
local colorPalette = color_util.toArray3(table_util.concatenate((api.gui.genericRep.get(api.gui.genericRep.find("::/gui/line_vehicle_mgmt/line_colors.gres")).da
base/content/gui/gui/game_bar/game_bar.tl:182
children = table_util.concatenate(children, radialMenuPlugins)
base/content/gui/gui/line_vehicle_mgmt/line_react_util.tl:601
local colorPalette = color_util.toArray3(table_util.concatenate((api.gui.genericRep.get(api.gui.genericRep.find("::/gui/line_vehicle_mgmt/line_colors.gres")).da
base/content/gui/gui/line_vehicle_mgmt/manager_window.tl:3600
local colorPalette = color_util.toArray3(table_util.concatenate((api.gui.genericRep.get(api.gui.genericRep.find("::/gui/line_vehicle_mgmt/line_colors.gres")).da
base/content/gui/gui/line_vehicle_mgmt/vehicle_store_util.tl:275
vehicleData.cargoClasses = table_util.concatenate(vehicleData.cargoClasses, cargoClasses)
… and 10 more files.

merge<T, V>(...: {T : V}) : {T : V} base/tealdef/scripts/table_util.d.tl:13

Call as table_util.merge(…)

if same key is in multiple tables, the last one wins.

Returns {T : V}

Used in the base game: 11 times in 3 files

base/content/gui/gui/menu/graphics_presets_util.tl:169

consolePresetsSettings[i] = table_util.merge(proSettings, preset)
mods/release/urbangames_campaign_mission_02/content/mission/mission/mission_story.tl:80
local allowBuyVehiclesAfterTrainExtension : {string : integer} = table_util.merge(allowBuyVehiclesTrainExtension)
mods/release/urbangames_campaign_mission_05/content/mission/mission/mission_story.tl:527
return table_util.merge(observed_red, observed_yellow)

removeDuplicates<T>(arr : {T}) : {T} base/tealdef/scripts/table_util.d.tl:14

Call as table_util.removeDuplicates(…)

Notes

Returns a new array with repeated elements removed, keeping the first occurrence and the order.

Returns {T}

Used in the base game: 2 times in 2 files

base/content/gui/gui/line_vehicle_mgmt/vehicle_store_util.tl:367

vehicleData.cargoClasses = table_util.removeDuplicates(vehicleData.cargoClasses)
base/content/gui/gui/main/cargo_react_util.tl:709
local cargoClasses : { string } = table_util.removeDuplicates(data.cargoClasses)