My current set up is to generate upgrade values for my game as manually setting them would take ages so i used a table and a for loop to generate values based off of number factors that increase the value each time. Is this the best way to set tables values or am missing a useful function or different method?
Personally, I wouldn’t use a dictionary at all unless absolutely necessary. What I do instead is create a function that returns a value for the next upgrade and I only call it when I need my code to recognise a new cost for any reason, from security checks to updating Guis.
Other than that idea of changing the way you generate your values, yes, your code looks fine as it is. One thing I’d recommend is to store some of your indexes into variables so that you aren’t constantly performing table lookups and writing the same code over again.
for i = 2, 15 do
local PetStorage = Public.UpgradesTable.PetStorage
local PreviousPet = PetStorage[i-1]
table.insert(PetStorage, {
Cost = PreviousPet.Cost * UpgradePetStorageCostFactor;
newAmount = PreviousPet.newAmount * UpgradePetStorageAmountFactor;
XP = PreviousPet.XP * UpgradePetStorageXPFactor;
}
-- and so on
Out of curiosity, is there a reason why you starting the loop from two and going to 15 when you’re just subtracting one from the value of I to get the index? It seems like a smarter option would be to go from one to 14 as it would have the same effect.
From a quick look at the code, it seems that the first indice is set as the defaults for the value. That is then used as a reference point for the next upgrade where table.insert creates the next indice based off of values from the previous one, which is why the loop begins at 2 and not 1 and goes to 15 not 14.
The effects between your suggestion and what the code is doing would not have the same effect, because then the indice of [i-1] where i = 1 would be nil and thus that provides no data to work off of when the next indice is being created and inserted into the table.
Again, why in cases like this I’d prefer writing a function that calculates the cost arbitrarily instead of using a dictionary and then storing that value somewhere for later use.