#Pulling and Finding the Sum Of Values in an Array.
1 messages · Page 1 of 1 (latest)
"It's not working" is not helpful
In order for a question to be answered, it must specify what exactly is wrong. Stating simply that "it doesn't work" is not sufficient.
Source: https://idownvotedbecau.se/itsnotworking
Please elaborate on your question by including all relevant details. What do you think is the problem? Have you tried to fix it? If you have, why didn't that work?
Also this isn't a #1006674323458764851 question
I need to do it in the context of Unity so I need to get around using for loops.
Summing the values in an array doesn't care about context
You do it in Unity the same way you do it in any other environment
At any rate, ^
I'm more looking for alternitive methods. Like the .Sum(); command where I can define a starting and ending index.
I can send the code.
Ew you don't need to use Sum method here, that's unnecessarily slow
public int[] costPerLevel = { 200, 250, 300, 350, 475, 600, 650, 750, 850, 950, 1000, 1100, 1200, 1500, 2500, 3500, 4500, 5000, 6000, 7000, 8000, 9500, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 22500, 25000, 27500, 30000};
public void CalculateRequiredXp()
{
if (Mathf.Abs(desiredHealthLvl + desiredSpeedLvl + desiredAttackLvl + desiredUtilityLvl - 3) != level)
{
requiredXp =
}
else
{
requiredXp = 0;
} I
Using a custom for loop is always faster
Show me what you have currently, for summing
i need to have the required xP increase depending on what level you are upgrading to
but the way i did it made it cheaper to spend xp, hit apply over and over rather than applying all the levels at once
Also as a protip
Using a for loop it infinitely increased
It looks like here you are storing the result in a requiredXp variable, when that seems wasteful. The method should return the result instead
oh yeah I did have that initially but changed it
im still kinda new and was trying to make it easier to visualize in my mind
but was gonna revert back to it
Ah I see
Thanks for responding btw
Yeah, revert back to returning. It makes more sense for a method like this to return the result
ok
Anyways, sounds like you possibly had an infinite loop which means your condition was wrong. How did your loop look?
I can't remember exactly, but it was taking the level value you wanted to upgrade to and adding all of the values that it cost to get to that level until the level and the var that was = to the desired level <=0
it was like
for (int i = (desiredHealthLvl + desiredSpeedLvl + desiredAttackLvl + desiredUtilityLvl - 3); i <= 0; i--)
well no wonder it was failing
yeah i kinda suck at for loops
you're using a reverse loop but your condition is checking <= 0 not >= 0
because you haven't told it to increment, you have i-- not i++
decrementing is the word
(desiredHealthLvl + desiredSpeedLvl + desiredAttackLvl + desiredUtilityLvl - 3)
can I ask what this is doing? or rather, why you're doing that?
Does the required level XP need to sum all the other levels?
yeah that is the equation to find the desired level. I'm going to set up a desired level method after I figure this out. I want the required xp to sum up the cost of all of the levels you are upgrading.
I see
I'm going for a dark souls like leveling system for my combat parkour game
So if that final result is 1, you want it to just take the first value (200).
If the result is 2, take the first two (200 + 250)
etc. etc. am I right in thinking that?
yeah so if you want to level up to level 5 from level 2, you have to add up the cost to level up to level 3, 4, and 5.
gotcha
well that is relatively straight forward
initialise the iterator (i) to the initial level - 1, condition i < desiredLevel, and just tally up the elements inside the loop
var total = 0;
for (var index = initial - 1; index < desired; index++)
total += array[index];
return total;
basically
maybe some other checks to make sure desired > initial - 1 (so that you don't have an infinite loop), and also that initial - 1 doesn't go below 0 (causing IndexOutOfRangeException)
but that is the general idea anyway
Yeah im new to this so i only know how do certain things well and have not ventured into to something like this. I just taught myself over a month with like no tutorials so this helped a lot. Ill try it out.
Yeah I think i know what you mean
Thanks so much
you're welcome
that's untested code btw - take with a grain of salt. I might've made a typo
ah yep I did make a typo
you don't start with initial - 1, because you presumably don't want to include the current level's XP in the sum because... you're already that level
Starting at level 2 (200xp), to reach level 5 (500xp), that should give 300 + 400 + 500 (ignoring the current level's 200xp because you already have that level)
which checks out
cool
Just did it. It works 100% after one or 2 changes. Thanks a ton.