How to do division?

usually able to figure this stuff out but seems like its a lot harder than it should be. I’m trying to get percentage from two score managers. I successfully am getting their values and display them but cant seem to get a percent value from score / TotalScore. I keep getting errors because its not predefined value since its numbers from score manager.
when using division seems it just only wants predefined numbers.

OnBegin<override>()<suspends>:void=
    Trigger.TriggeredEvent.Subscribe(OnInitial)
    
OnInitial(QAgent:?agent):void=
    if (Agent:=QAgent?):
        ScoreManager1.Activate(Agent)
        score := ScoreManager1.GetCurrentScore(Agent)
        ScoreManager2.Activate(Agent)
        score2 := ScoreManager2.GetCurrentScore(Agent)

        TotalScore := score + score2
        
        SetTriggerText(score, score2, TotalScore)

SetTriggerText(value:int, value2:int, total:int):void=
    ScoreText.SetText(ScoreMessage("{value}"))
    ScoreText2.SetText(ScoreMessage("{value2}"))
    ScoreText3.SetText(ScoreMessage("{total}"))

Where is your division you want to do?
To divide, you need to put it inside of an ‘if’ statement.
To get percentages, I would recommend using the following formula to get the percentage something is of something else:
Smaller part / Full part * 100
For example,
25/75*100 = 33.333

What is your specific question?

Thanks for your response! This is how i was trying to do it. With “PercentScore” but i get error on the “/”

    TotalScore := score + score2
    
    PercentScore:= (score / TotalScore) * 100


    SetTriggerText(score, score2, TotalScore, PercentScore)

Hey, when you divide 2 int you would have to use

Floor() - Rounds down to nearest int
Ceil() - Rounds up to nearest int
Round() - Rounds to nearest int

So you divide like this:

OnInitial(QAgent:?agent):void=
    if(Agent:=QAgent?):
       ScoreManager1.Activate(Agent)
       Score := ScoreManager1.GetCurrentScore(Agent)
       ScoreManager2.Activate(Agent)
       Score2 := ScoreManager2.GetCurrentScore(Agent)
    
       TotalScore := Score + Score2
      
       if(PercentScore := Round[(Score/TotalScore)*100]):
           SetTriggerText(Score, Score2, TotalScore, PercentScore)
       else:
           Err("Failed to calculate percent score") # Stop verse from running to find error in code

You could also make Score and Score2 to a float by multiply with 1.0

OnInitial(QAgent:?agent):void=
    if (Agent:=QAgent?):
        ScoreManager1.Activate(Agent)
        Score := ScoreManager1.GetCurrentScore(Agent) * 1.0
        ScoreManager2.Activate(Agent)
        Score2 := ScoreManager2.GetCurrentScore(Agent) * 1.0

        TotalScore := Score + Score2
        PercentScore := Score/TotalScore*100
        
        SetTriggerText(Score, Score2, TotalScore, PercentScore)
2 Likes

(3rd edit) Thank you been trying some new things and got it fully working but the only thing Im struggling to figure out is conversion. I would like to convert Score,Score2, and TotalScore back to a int after the calculations to send ints in the SetTriggerText instead of floats.

    OnInitial(QAgent:?agent):void=
        if (Agent:=QAgent?):
            ScoreManager1.Activate(Agent)
            Score := ScoreManager1.GetCurrentScore(Agent) * 1.0
            ScoreManager2.Activate(Agent)
            Score2 := ScoreManager2.GetCurrentScore(Agent) * 1.0

            TotalScore := Score + Score2

            if(PercentScore := Round[(Score/TotalScore)*100]):
                SetTriggerText(Score, Score2, TotalScore, PercentScore)
            else:
                Err("Failed to calculate percent score") # Stop verse from running to find error in code

    SetTriggerText(value:float, value2:float, total:float, Percent:int):void=
        
        ScoreText.SetText(ScoreMessage("{value}"))
        ScoreText2.SetText(ScoreMessage("{value2}"))
        ScoreText3.SetText(ScoreMessage("{total}"))
        ScoreText4.SetText(ScoreMessage("{Percent}"))

Idk what happened but the first code did not say a error for me at first. Here is a function Divide where you can divide 2int and get a int back. So you can keep score and score2 as int.

Divide(V1:int, V2:int)<decides><transacts>:int=
        Value1 := V1 * 1.0 
        Value2 := V2 * 1.0

        PercentScore:int = Round[Value1 / Value2]

Example:

Score := ScoreManager1.GetCurrentScore(Agent)
Score2 := ScoreManager2.GetCurrentScore(Agent)
TotalScore := Score + Score2

if(PercentScore := Divide[Score, TotalScore]):
    SetTriggerText(Score, Score2, TotalScore, PercentScore)

I have not tested it though.

1 Like

I implemented what you gave and got no errors but on the ScoreText4 (To display the Percent) i just get 0 and it seems Score2 is getting the same value as Score but TotalScore and Score seem to work fine.

Edit: Just a little mistake on the scores getting the same values (i fixed that) but for the percent i just get 1 or a not correct percent but all other values are good still.

    Divide(V1:int, V2:int)<decides><transacts>:int=
        Value1 := V1 * 1.0 
        Value2 := V2 * 1.0

        PercentScore:int = Round[Value1 / Value2]

    OnBegin<override>()<suspends>:void=
        Trigger.TriggeredEvent.Subscribe(OnInitial)
        
    OnInitial(QAgent:?agent):void=
        if (Agent:=QAgent?):
            ScoreManager1.Activate(Agent)
            Score := ScoreManager1.GetCurrentScore(Agent)
            ScoreManager2.Activate(Agent)
            Score2 := ScoreManager2.GetCurrentScore(Agent)

            TotalScore := Score + Score2

            if(PercentScore := Divide[Score, TotalScore]):
                SetTriggerText(Score, Score2, TotalScore, PercentScore)

    SetTriggerText(value:int, value2:int, total:int, Percent:int):void=
        
        ScoreText.SetText(ScoreMessage("{value}"))
        ScoreText2.SetText(ScoreMessage("{value2}"))
        ScoreText3.SetText(ScoreMessage("{total}"))
        ScoreText4.SetText(ScoreMessage("{Percent}"))

my bad forgot to *100 before making it a int. here is the new version. changed the name to CalculatePercentage

CalculatePercentage(V1:int, V2:int)<decides><transacts>:int=
    Value1 := V1 * 1.0 
    Value2 := V2 * 1.0

    PercentScore := (Value1 / Value2) * 100
    Round[PercentScore]

if you want to have the percentage in float here is the same function but return as a float (you cant have both without changing the name of one of them)

CalculatePercentage(V1:int, V2:int)<decides><transacts>:float=
    Value1 := V1 * 1.0 
    Value2 := V2 * 1.0

    PercentScore := (Value1 / Value2) * 100

i have not tested them

1 Like

Thank you!! Worked perfectly

1 Like

Good to hear :grinning:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.