One more notice about this.
In getter Function I was simply using
And like in the AddItem situation:
Code: Select all
Local Items = Cart.GetItems()
InsertItem(Items, Item)
This works and might be exactly how you want to do it even.
Point being, that although "Items" in AddItem part is Local, meaning it gets destroyed after you leave that function, it however works since Local Items is actually a reference to the Original Cart.Items, which means that whatever you do to that Local Items, will also happen to Cart.Items (or technically speaking, there is only one copy of Cart.Items, and since Local Items is a reference to Cart.Items, it means that it changes Cart.Items only, but since Local Items is like another name how to use Cart.Items, you can see everything with Local Items too).
This might be what you want, and then you don't need a separate Setter function at all for the Cart.Items.
But, there can be situations where you might want to get the Cart.Items as a Local copy, so that you can change the Local copy into something different, without affecting the original Cart.Items.
For example, maybe you have different ways to Sort the items, in that case you maybe want a temporal copy of Items that you can sort into any order you want, while original Cart.Items remains unsorted.
To do this, you have two options:
1.
You use:
Code: Select all
Local CartItems = Cart.GetItems()
Local Items = CopyTable(CartItems)
Or, if you want to make more sure to not accidentally mess things, you change the Getter Function into following:
2.
Code: Select all
Cart.GetItems()
Return( CopyTable(Cart.Items) )
EndFunction
In which case the returned Items is an independent copy of that table instead of reference to the cart.Items table.
But if you use this approach, which helps you avoid accidentally changing Cart.Items, you then need to use Setter Function when you do want to make changes, like in Cart.InsertItem, after you have inserted the item, you need to use
Which requires a Setter Function:
Code: Select all
Cart.SetItems(Items)
Cart.Items = Items
EndFunction