Unique Table
A unique table is an expanded table object allowing for easy management of a table where the desired usage of the table is to only store unique items.
Also allows easy addition, subtraction and various other functionality to manage the tables via multiple operators implemented through metamethods.
| Loaded in Campaign |
|
| Loaded in Battle |
|
| Loaded in Frontend |
|
Addition Operator (+)
Subtraction Operator (-)
Multiplication Operator (*)
Division Operator (/)
Example - Creating and Inserting:
Only unique entires are added to the table, hence in the example below the second attempt to add "first_string" does not work
local ut1 = unique_table:new();
ut1:insert("first_string");
ut1:insert("first_string");
ut1:insert("second_string");
-- ut1 Contents: "first_string", "second_string"
Example - Adding two unique tables together:
Only unique entires are added to both tables, and upon adding both tables again only unique entires remain in the final table
local ut1 = unique_table:new();
ut1:insert("first_string");
ut1:insert("first_string");
ut1:insert("second_string");
local ut2 = unique_table:new();
ut2:insert("second_string");
ut2:insert("third_string");
local ut3 = ut1 + ut2;
-- ut3 Contents: "first_string", "second_string", "third_string"
Example - Subracting one unique table from another:
The entries from the second table are removed from the first and the result is a new unique table
local ut1 = unique_table:new();
ut1:insert("first_string");
ut1:insert("second_string");
local ut2 = unique_table:new();
ut2:insert("second_string");
local ut3 = ut1 - ut2;
-- ut3 Contents: "first_string"
Example - Extracting matching items:
The resulting unique table from the multiplication operator only contains items that are in both of the unique tables supplied
local ut1 = unique_table:new();
ut1:insert("first_string");
ut1:insert("second_string");
local ut2 = unique_table:new();
ut2:insert("second_string");
ut2:insert("third_string");
local ut3 = ut1 * ut2;
-- ut3 Contents: "second_string"
Example - Extracting unique items amongst multiple unique tables:
The resulting unique table from the division operator only contains items that are not in both of the unique tables supplied
local ut1 = unique_table:new();
ut1:insert("first_string");
ut1:insert("second_string");
local ut2 = unique_table:new();
ut2:insert("second_string");
ut2:insert("third_string");
local ut3 = ut1 / ut2;
-- ut3 Contents: "first_string", "third_string"
Example - Creating a unique table from existing data:
Here two list interfaces are taken from the model, turned into unique tables and multiplied to get only the items in both lists
local faction_obj = cm:model():world():faction_by_key(faction_key);
local non_aggression_pacts = unique_table:faction_list_to_unique_table(faction_obj:factions_non_aggression_pact_with());
local trading_partners = unique_table:faction_list_to_unique_table(faction_obj:factions_trading_with());
local trading_partners_with_nap_pacts = non_aggression_pacts * trading_partners;
-- Result: The final unique table contains only factions with whom the player has both a non-aggression pact and a trade treaty
-
unique_table:new([tableo]) -
Creates a new unique table object
Parameters:
1
tableoptional, default value=nil
Pass an object to the new function to use that instance of the object as this new one
Returns:
unique_tablethe new unique table object
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 111
-
unique_table:insert(objectitem) -
Adds a new item into the unique table, but only if that item does not already exist
Parameters:
1
objectThe item to add to the table
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 180
-
unique_table:remove(objectitem) -
Removes the given item from the table
Parameters:
1
objectThe item to remove from the table
Returns:
nil
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 190
-
unique_table:contains(objectitem) -
Returns true if the given item exists in the table
Parameters:
1
objectThe item to check
Returns:
booleantrue if the item exists
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 203
-
unique_table:index_of(objectitem) -
Returns the index of the given item in the table, or 0 if it doesn't exist
Parameters:
1
objectThe item to check
Returns:
numberindex of the given item
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 211
-
unique_table:to_table() -
Returns the unique table object as a normal table
Returns:
tablethe unique table as a normal table
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 224
-
unique_table:table_to_unique_table() -
Returns the table as a new unique table object
Returns:
unique_tablethe table as a new unique table
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 232
-
unique_table:faction_list_to_unique_table(objectfaction_list, booleancqi_list) -
Returns a new unique table containing the items from the given faction list
Parameters:
1
objectThe faction list to use
2
booleanPass true to make the table use cqi's instead of faction objects
Returns:
unique_tablethe new unique table
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 246
-
unique_table:character_list_to_unique_table(objectcharacter_list) -
Returns a new unique table containing the items from the given character list
Parameters:
1
objectThe character list to use
Returns:
unique_tablethe new unique table
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 266
-
unique_table:region_list_to_unique_table(objectregion_list) -
Returns a new unique table containing the items from the given region list
Parameters:
1
objectThe region list to use
Returns:
unique_tablethe new unique table
defined in ../../warhammer/working_data/script/_lib/lib_unique_table.lua, line 280