Firetron's ObjectArray
stableDescription
Creates and returns an ObjectArray Class instance. It has less operations than a normal array, but it can store objects instead of just simple types and still be able to save and load (using its own methods). Up to 9 fields/properties are supported on the objects, and you must provide their names.
HaasScript
-- ============================================================================
-- Firetron's ObjectArray
--
-- Creates and returns an ObjectArray Class instance. It has less operations
-- than a normal array, but it can store objects instead of just simple types
-- and still be able to save and load (using its own methods).
-- Up to 9 fields/properties are supported on the objects, and you must
-- provide their names.
--
-- Discord: @FiretronP75
-- ============================================================================
-- ========================================================
-- Variables
-- ========================================================
-- ------------------------------------
-- Definition
-- ------------------------------------
local description
local output
-- ------------------------------------
-- Parameter
-- ------------------------------------
local pFieldNameList
-- ========================================================
-- Command Definition
-- ========================================================
description = 'Creates and returns an ObjectArray Class instance. It has less operations than a normal array, but it can store objects instead of just simple types and still be able to save and load (using its own methods). Up to 9 fields/properties are supported on the objects, and you must provide their names.'
DefineCommand('ObjectArray', description)
-- ========================================================
-- Parameter Definition
-- ========================================================
description = 'Comma separated list of the names of the fields on your object (no spaces around commas) "name,value,isDone" for example.'
pFieldNameList = DefineParameter(StringType, 'fieldNameList', description, true, 'name,value,isDone')
-- ========================================================
-- Class Definition
-- ========================================================
local ObjectArray = function (fieldNameList)
if not fieldNameList then
LogError('ObjectArray\'s fieldNameList must be defined.')
end
if #fieldNameList < 2 then
LogError('ObjectArray\'s fieldNameList must have at least 2 fields. If you only need 1 field then use a normal array.')
end
local vList1 = {}
local vList2 = {}
local vList3 = {}
local vList4 = {}
local vList5 = {}
local vList6 = {}
local vList7 = {}
local vList8 = {}
local vList9 = {}
local nList = fieldNameList
local _Add = function (element)
local newIndex = #vList1 + 1
if nList[1] != nil then vList1[newIndex] = element[nList[1]] end
if nList[2] != nil then vList2[newIndex] = element[nList[2]] end
if nList[3] != nil then vList3[newIndex] = element[nList[3]] end
if nList[4] != nil then vList4[newIndex] = element[nList[4]] end
if nList[5] != nil then vList5[newIndex] = element[nList[5]] end
if nList[6] != nil then vList6[newIndex] = element[nList[6]] end
if nList[7] != nil then vList7[newIndex] = element[nList[7]] end
if nList[8] != nil then vList8[newIndex] = element[nList[8]] end
if nList[9] != nil then vList9[newIndex] = element[nList[9]] end
end
local _Concat = function (objectArray)
local all = objectArray.GetAllValueLists()
if nList[1] != nil then vList1 = ArrayConcat(vList1, all.valueList1) end
if nList[2] != nil then vList2 = ArrayConcat(vList2, all.valueList2) end
if nList[3] != nil then vList3 = ArrayConcat(vList3, all.valueList3) end
if nList[4] != nil then vList4 = ArrayConcat(vList4, all.valueList4) end
if nList[5] != nil then vList5 = ArrayConcat(vList5, all.valueList5) end
if nList[6] != nil then vList6 = ArrayConcat(vList6, all.valueList6) end
if nList[7] != nil then vList7 = ArrayConcat(vList7, all.valueList7) end
if nList[8] != nil then vList8 = ArrayConcat(vList8, all.valueList8) end
if nList[9] != nil then vList9 = ArrayConcat(vList9, all.valueList9) end
end
local _Count = function ()
return Count(vList1)
end
local _Get = function (index)
local element = {}
if nList[1] != nil then element[nList[1]] = ArrayGet(vList1, index) end
if nList[2] != nil then element[nList[2]] = ArrayGet(vList2, index) end
if nList[3] != nil then element[nList[3]] = ArrayGet(vList3, index) end
if nList[4] != nil then element[nList[4]] = ArrayGet(vList4, index) end
if nList[5] != nil then element[nList[5]] = ArrayGet(vList5, index) end
if nList[6] != nil then element[nList[6]] = ArrayGet(vList6, index) end
if nList[7] != nil then element[nList[7]] = ArrayGet(vList7, index) end
if nList[8] != nil then element[nList[8]] = ArrayGet(vList8, index) end
if nList[9] != nil then element[nList[9]] = ArrayGet(vList9, index) end
return element
end
local _GetAllValueLists = function ()
return {
valueList1 = vList1,
valueList2 = vList2,
valueList3 = vList3,
valueList4 = vList4,
valueList5 = vList5,
valueList6 = vList6,
valueList7 = vList7,
valueList8 = vList8,
valueList9 = vList9,
}
end
local _Index = function (index)
local element = {}
if nList[1] != nil then element[nList[1]] = ArrayIndex(vList1, index) end
if nList[2] != nil then element[nList[2]] = ArrayIndex(vList2, index) end
if nList[3] != nil then element[nList[3]] = ArrayIndex(vList3, index) end
if nList[4] != nil then element[nList[4]] = ArrayIndex(vList4, index) end
if nList[5] != nil then element[nList[5]] = ArrayIndex(vList5, index) end
if nList[6] != nil then element[nList[6]] = ArrayIndex(vList6, index) end
if nList[7] != nil then element[nList[7]] = ArrayIndex(vList7, index) end
if nList[8] != nil then element[nList[8]] = ArrayIndex(vList8, index) end
if nList[9] != nil then element[nList[9]] = ArrayIndex(vList9, index) end
return element
end
local _Last = function (offset)
local element = {}
if nList[1] != nil then element[nList[1]] = ArrayLast(vList1, offset) end
if nList[2] != nil then element[nList[2]] = ArrayLast(vList2, offset) end
if nList[3] != nil then element[nList[3]] = ArrayLast(vList3, offset) end
if nList[4] != nil then element[nList[4]] = ArrayLast(vList4, offset) end
if nList[5] != nil then element[nList[5]] = ArrayLast(vList5, offset) end
if nList[6] != nil then element[nList[6]] = ArrayLast(vList6, offset) end
if nList[7] != nil then element[nList[7]] = ArrayLast(vList7, offset) end
if nList[8] != nil then element[nList[8]] = ArrayLast(vList8, offset) end
if nList[9] != nil then element[nList[9]] = ArrayLast(vList9, offset) end
return element
end
local _Load = function (name)
if nList[1] != nil then vList1 = Load(name..'vList1', {}) end
if nList[2] != nil then vList2 = Load(name..'vList2', {}) end
if nList[3] != nil then vList3 = Load(name..'vList3', {}) end
if nList[4] != nil then vList4 = Load(name..'vList4', {}) end
if nList[5] != nil then vList5 = Load(name..'vList5', {}) end
if nList[6] != nil then vList6 = Load(name..'vList6', {}) end
if nList[7] != nil then vList7 = Load(name..'vList7', {}) end
if nList[8] != nil then vList8 = Load(name..'vList8', {}) end
if nList[9] != nil then vList9 = Load(name..'vList9', {}) end
end
local _Pop = function ()
if nList[1] != nil then vList1 = ArrayPop(vList1) end
if nList[2] != nil then vList2 = ArrayPop(vList2) end
if nList[3] != nil then vList3 = ArrayPop(vList3) end
if nList[4] != nil then vList4 = ArrayPop(vList4) end
if nList[5] != nil then vList5 = ArrayPop(vList5) end
if nList[6] != nil then vList6 = ArrayPop(vList6) end
if nList[7] != nil then vList7 = ArrayPop(vList7) end
if nList[8] != nil then vList8 = ArrayPop(vList8) end
if nList[9] != nil then vList9 = ArrayPop(vList9) end
end
local _Push = function (element)
local newIndex = #vList1 + 1
if nList[1] != nil then vList1[newIndex] = element[nList[1]] end
if nList[2] != nil then vList2[newIndex] = element[nList[2]] end
if nList[3] != nil then vList3[newIndex] = element[nList[3]] end
if nList[4] != nil then vList4[newIndex] = element[nList[4]] end
if nList[5] != nil then vList5[newIndex] = element[nList[5]] end
if nList[6] != nil then vList6[newIndex] = element[nList[6]] end
if nList[7] != nil then vList7[newIndex] = element[nList[7]] end
if nList[8] != nil then vList8[newIndex] = element[nList[8]] end
if nList[9] != nil then vList9[newIndex] = element[nList[9]] end
end
local _Remove = function (index)
if nList[1] != nil then vList1 = ArrayRemove(vList1, index) end
if nList[2] != nil then vList2 = ArrayRemove(vList2, index) end
if nList[3] != nil then vList3 = ArrayRemove(vList3, index) end
if nList[4] != nil then vList4 = ArrayRemove(vList4, index) end
if nList[5] != nil then vList5 = ArrayRemove(vList5, index) end
if nList[6] != nil then vList6 = ArrayRemove(vList6, index) end
if nList[7] != nil then vList7 = ArrayRemove(vList7, index) end
if nList[8] != nil then vList8 = ArrayRemove(vList8, index) end
if nList[9] != nil then vList9 = ArrayRemove(vList9, index) end
end
local _Save = function (name)
if nList[1] != nil then Save(name..'vList1', vList1) end
if nList[2] != nil then Save(name..'vList2', vList2) end
if nList[3] != nil then Save(name..'vList3', vList3) end
if nList[4] != nil then Save(name..'vList4', vList4) end
if nList[5] != nil then Save(name..'vList5', vList5) end
if nList[6] != nil then Save(name..'vList6', vList6) end
if nList[7] != nil then Save(name..'vList7', vList7) end
if nList[8] != nil then Save(name..'vList8', vList8) end
if nList[9] != nil then Save(name..'vList9', vList9) end
end
local _Shift = function ()
if nList[1] != nil then vList1 = ArrayShift(vList1) end
if nList[2] != nil then vList2 = ArrayShift(vList2) end
if nList[3] != nil then vList3 = ArrayShift(vList3) end
if nList[4] != nil then vList4 = ArrayShift(vList4) end
if nList[5] != nil then vList5 = ArrayShift(vList5) end
if nList[6] != nil then vList6 = ArrayShift(vList6) end
if nList[7] != nil then vList7 = ArrayShift(vList7) end
if nList[8] != nil then vList8 = ArrayShift(vList8) end
if nList[9] != nil then vList9 = ArrayShift(vList9) end
end
local _Unshift = function (element)
if nList[1] != nil then vList1 = ArrayUnshift(vList1, element[nList[1]]) end
if nList[2] != nil then vList2 = ArrayUnshift(vList2, element[nList[2]]) end
if nList[3] != nil then vList3 = ArrayUnshift(vList3, element[nList[3]]) end
if nList[4] != nil then vList4 = ArrayUnshift(vList4, element[nList[4]]) end
if nList[5] != nil then vList5 = ArrayUnshift(vList5, element[nList[5]]) end
if nList[6] != nil then vList6 = ArrayUnshift(vList6, element[nList[6]]) end
if nList[7] != nil then vList7 = ArrayUnshift(vList7, element[nList[7]]) end
if nList[8] != nil then vList8 = ArrayUnshift(vList8, element[nList[8]]) end
if nList[9] != nil then vList9 = ArrayUnshift(vList9, element[nList[9]]) end
end
return {
Add = _Add,
Concat = _Concat,
Count = _Count,
Get = _Get,
GetAllValueLists = _GetAllValueLists,
Index = _Index,
Last = _Last,
Load = _Load,
Pop = _Pop,
Push = _Push,
Remove = _Remove,
Save = _Save,
Shift = _Shift,
Unshift = _Unshift,
}
end
-- ========================================================
-- Execution
-- ========================================================
local nameList = StringSplit(pFieldNameList, ',')
output = ObjectArray(nameList)
-- ========================================================
-- Output Definitions
-- ========================================================
description = 'ListDynamic where each element is a function.'
DefineOutput(ListDynamicType, output, description)
description = 'Works like ArrayAdd but without the source array parameter and does not return the array.'
DefineOutputIndex( 1, DynamicType, 'Add', description)
description = 'Concatenates another ObjectArray to this one and does not return the array.'
DefineOutputIndex( 2, DynamicType, 'Concat', description)
description = 'Works like Array.Count but without the source array parameter.'
DefineOutputIndex( 3, DynamicType, 'Count', description)
description = 'Works like ArrayGet but without the source array parameter.'
DefineOutputIndex( 4, DynamicType, 'Get', description)
description = 'Returns a ListDynamic containing each of the internal arrays for each field, named valueList1 through valueList9.'
DefineOutputIndex( 5, DynamicType, 'GetAllValueLists', description)
description = 'Works like ArrayIndex but without the source array parameter.'
DefineOutputIndex( 6, DynamicType, 'Index', description)
description = 'Works like ArrayLast but without the source array parameter.'
DefineOutputIndex( 7, DynamicType, 'Last', description)
description = 'Loads into itself from a saved instance.'
DefineOutputIndex( 8, DynamicType, 'Load', description)
description = 'Works like ArrayPop but without the source array parameter and does not return the array.'
DefineOutputIndex( 9, DynamicType, 'Pop', description)
description = 'Works like ArrayAdd but without the source array parameter and does not return the array.'
DefineOutputIndex(10, DynamicType, 'Push', description)
description = 'Works like ArrayRemove but without the source array parameter and does not return the array.'
DefineOutputIndex(11, DynamicType, 'Remove', description)
description = 'Saves this instance.'
DefineOutputIndex(12, DynamicType, 'Save', description)
description = 'Works like ArrayShift but without the source array parameter and does not return the array.'
DefineOutputIndex(13, DynamicType, 'Shift', description)
description = 'Works like ArrayUnshift but without the source array parameter and does not return the array.'
DefineOutputIndex(14, DynamicType, 'Unshift', description)
4 Comments
Sign in to leave a comment.
Fixed some major typos.
Here's a simple test to prove it works:
local objectArray = CC_ObjectArray('name,age')
objectArray.Load('savedObjectArray')
function Main ()
local count = objectArray.Count()
local object = {
age = 20 + count,
name = 'Guy '..count,
}
objectArray.Add(object)
objectArray.Save('savedObjectArray')
end
OptimizedForInterval(60, Main)
Finalize(function ()
local another = CC_ObjectArray('name,age')
local object = {
age = 19,
name = 'Me',
}
another.Add(object)
local all = objectArray.GetAllValueLists()
Log(all.valueList1)
Log(all.valueList2)
another.Concat(objectArray)
local count = another.Count()
Log('Count is '..count)
for i = 1, count do
local object = another.Get(i)
Log(' ')
Log('Age is '..object.age)
Log('Name is '..object.name)
end
end)
Thanks Firetron for your help and scripts here. Could you help me with my script and strategy? Thanks Daniel
Discord is better suited for that discussion.