Skip to content

tableHelper.lua utility file

Bui edited this page Aug 20, 2015 · 2 revisions

Table of Contents

tableHelper.lua

Category: Utility Lua-Based Scripts

transform_type_kvp

Type: number

Notes: Used as an argument to the transform function.

transform_type_array

Type: number

Notes: Used as an argument to the transform function.

append

Return(s):

  • N/A
Argument(s):
  • tbl: table
  • elem: any
Notes: Appends the specified element to the end of the table.

prepend

Return(s):

  • N/A
Argument(s):
  • tbl: table
  • elem: any
Notes: Inserts the specified element into the beginning of the table.

selectElement

Return(s):

  • element: any
Argument(s):
  • tbl: table
Notes: Chooses a random element from the specified table and returns it.

merge

Return(s):

  • tbl: table
Argument(s):
  • Varargs - listOfTables: any
Notes: Flattens all arguments into a single table. If the code comes across an argument in the function call that isn't a table, it just adds it to the finalized table anyway.

Usage:

 items = merge(commonEquips, warriorEquips, magicianEquips, bowmanEquips, thiefEquips, pirateEquips, arrows, scrolls, skills);
 items = merge(commonEquips, warriorEquips, magicianEquips, bowmanEquips, thiefEquips, pirateEquips);

slice

Return(s):

  • tbl: table
Argument(s):
  • tbl: table
  • Optional - startIndex: number
  • Optional - endIndex: number
Notes: Copies a portion of the specified table and returns a new table consisting only of that portion.

transform

Return(s):

  • tbl: table
Argument(s):
  • tbl: table
  • transformType: number
  • func: function
    • Return(s): value: any
    • Argument(s): index: number, key: any, value: any
Notes: Transforms an entire table using the specified predicate.

Usage:

 -- This is in the context of building up a list of players to ban from an expedition
 signedUp = getSignupList();
 -- This drops one element off signedUp (in particular, the expedition leader)
 -- After preprocessing the array, it transforms the elements into a choice data array that can be used directly with choiceRef
 choices = transform(slice(signedUp, 2), transform_type_array, function(idx, key, value)
 	return makeChoiceData((key + 1) .. " : " .. value, {value});
 end);

extend

Return(s):

  • tbl: table
Argument(s):
  • Varargs - listOfTables: any
Notes: Takes all of the keys from the specified tables and lumps them into a single table. The behavior if the same key is found is to take the "most recent" (as defined by the order of the arguments) value. That is, it will overwrite the existing value. Tables are queried in order from left argument to right argument.

Usage:

 function getOrDefault(props)
 	return extend({
 		["min"] = 0,
 		["max"] = 100,
 	}, props);
 end
 tbl1 = getOrDefault(); -- min 0, max 100
 tbl2 = getOrDefault({ ["max"] = 200 }); -- min 0, max 200

findValue

Return(s):

  • found: boolean
  • key: any
Argument(s):
  • tbl: table
  • needle: any
Notes: Searches the top level of a table in order to find a particular value. If needle is a function, it is executed for every element and returns a boolean indicating true if there was a match and false if not. Otherwise, it simply compares values until it finds the appropriate key associated to a value and returns it.

Usage:

 function findPlayerByName(list, playerName)
 	return findValue(list, function(value)
 		return value[1] == playerName;
 	end);
 end

performAction

Return(s):

  • N/A
Argument(s):
  • tbl: table
  • action: function
    • Return(s): N/A
    • Argument(s): key: any, value: any
Notes: Executes a function over an entire table.

Usage:

 local signups = getFullSignupList();
 performAction(signups, function(i, playerPair)
 	local name, id = playerPair[1], playerPair[2];
 	if #name > 0 and setPlayer(id) then
 		showMessage("The leader of the squad has entered the map. Please enter the map before time runs out on the squad.", msg_red);
 		revertPlayer();
 	end
 end);
Clone this wiki locally