#Pulling and Finding the Sum Of Values in an Array.

1 messages · Page 1 of 1 (latest)

cunning oracle
#

I need a way to take values between 2 indexes in an array and find the sum of those values. I tried using a traditional for loop but the way my code is structured it doesn't work. Does anyone know how I could do this?

agile fiberBOT
#

"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?

twilit zodiac
#

Also this isn't a #1006674323458764851 question

cunning oracle
twilit zodiac
#

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

cunning oracle
#

I can send the code.

twilit zodiac
#

Ew you don't need to use Sum method here, that's unnecessarily slow

cunning oracle
#

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
twilit zodiac
#

Using a custom for loop is always faster

#

Show me what you have currently, for summing

cunning oracle
#

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

twilit zodiac
#

Also as a protip

cunning oracle
#

Using a for loop it infinitely increased

twilit zodiac
cunning oracle
#

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

twilit zodiac
#

Ah I see

cunning oracle
#

Thanks for responding btw

twilit zodiac
#

Yeah, revert back to returning. It makes more sense for a method like this to return the result

cunning oracle
#

ok

twilit zodiac
cunning oracle
#

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--)

twilit zodiac
#

well no wonder it was failing

cunning oracle
#

yeah i kinda suck at for loops

twilit zodiac
#

you're using a reverse loop but your condition is checking <= 0 not >= 0

cunning oracle
#

Oh yeah but when i tried that i was not incrementing

#

decrementing

twilit zodiac
#

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?

cunning oracle
#

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.

twilit zodiac
#

I see

cunning oracle
#

I'm going for a dark souls like leveling system for my combat parkour game

twilit zodiac
#

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?

cunning oracle
#

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.

twilit zodiac
#

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

cunning oracle
#

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

twilit zodiac
#

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

cunning oracle
#

Yeah I see that too but it seems to be working

#

after the fix

twilit zodiac
#

cool

cunning oracle