# How to do division?

**URL:** <https://forums.unrealengine.com/t/how-to-do-division/1715973>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [February 27, 2024, 10:10am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973 "2024-02-27T10:10:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![scoobs3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scoobs3d/32/1182254_2.png) [@scoobs3d](https://forums.unrealengine.com/u/scoobs3d)\
**Post date:** [February 27, 2024, 10:10am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/1 "2024-02-27T10:10:09Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![JamieDeGamie](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JamieDeGamie](https://forums.unrealengine.com/u/JamieDeGamie)\
**Post date:** [February 27, 2024, 10:49pm UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/2 "2024-02-27T22:49:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![scoobs3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scoobs3d/32/1182254_2.png) [@scoobs3d](https://forums.unrealengine.com/u/scoobs3d)\
**Post date:** [February 27, 2024, 10:55pm UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/3 "2024-02-27T22:55:14Z")

</div>

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)

```

---

<div class="post-metadata">

**Author:** ![LogisonDK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LogisonDK](https://forums.unrealengine.com/u/LogisonDK)\
**Post date:** [February 28, 2024, 1:38am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/4 "2024-02-28T01:38:51Z")

</div>

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:

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![scoobs3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scoobs3d/32/1182254_2.png) [@scoobs3d](https://forums.unrealengine.com/u/scoobs3d)\
**Post date:** [February 28, 2024, 2:01am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/5 "2024-02-28T02:01:43Z")

</div>

(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.

```auto
    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}"))

```

---

<div class="post-metadata">

**Author:** ![LogisonDK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LogisonDK](https://forums.unrealengine.com/u/LogisonDK)\
**Post date:** [February 28, 2024, 3:25am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/6 "2024-02-28T03:25:00Z")

</div>

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.

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

        PercentScore:int = Round[Value1 / Value2]

```

Example:

```auto
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.

---

<div class="post-metadata">

**Author:** ![scoobs3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scoobs3d/32/1182254_2.png) [@scoobs3d](https://forums.unrealengine.com/u/scoobs3d)\
**Post date:** [February 28, 2024, 3:46am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/7 "2024-02-28T03:46:10Z")

</div>

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.

```auto
    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}"))

```

---

<div class="post-metadata">

**Author:** ![LogisonDK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LogisonDK](https://forums.unrealengine.com/u/LogisonDK)\
**Post date:** [February 28, 2024, 4:05am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/8 "2024-02-28T04:05:51Z")

</div>

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

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

```auto
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

---

<div class="post-metadata">

**Author:** ![scoobs3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scoobs3d/32/1182254_2.png) [@scoobs3d](https://forums.unrealengine.com/u/scoobs3d)\
**Post date:** [February 28, 2024, 4:20am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/9 "2024-02-28T04:20:02Z")

</div>

Thank you!! Worked perfectly

---

<div class="post-metadata">

**Author:** ![LogisonDK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LogisonDK](https://forums.unrealengine.com/u/LogisonDK)\
**Post date:** [February 28, 2024, 5:11am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/10 "2024-02-28T05:11:14Z")

</div>

Good to hear 😀

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 29, 2024, 5:12am UTC](https://forums.unrealengine.com/t/how-to-do-division/1715973/11 "2024-03-29T05:12:03Z")

</div>

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