Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Rounding a variable


Redyz
 Share

Recommended Posts

Hello, this is my first post around here, but I've been lurking for almost a year already, so I'd like to say Hi to everyone. (Well, at least everyone that browses this section)

Anyways, enough of this already, on to the main question.

I want to change the OnAttack, but instead of just adding +5 or -5 like someone [mentioned](http://www.touchofdeathforums.com/smf/index.php?topic=55259.0), I would like it to add +/- 5%.

So I did this algorithm:

  Damage = Rand(Int((GetPlayerSTR(Index)) - (Int((GetPlayerSTR(Index)) * 0.05))), Int((GetPlayerSTR(Index)) + ((GetPlayerSTR(Index)) * 0.05)))

(The code might be messy, I'm kinda tired of looking at all the parantheses)

Obviously sometimes the value returned is something like 2.3 or so, and so It doesnt show (E.G Doesn't affect the NPC)

So, for it to show, It needs to be rounded, and that is my question.

Short version: Need to round the value of a variable, what do

Thank you.
Link to comment
Share on other sites

Well, you would need to first split the variable, then use Left on the second array, watch.
```
ADamage = Split(Rand(Int((GetPlayerSTR(Index)) - (Int((GetPlayerSTR(Index)) * 0.05))), Int((GetPlayerSTR(Index)) + ((GetPlayerSTR(Index)) * 0.05))), ".")
Round = Left(Damage(1), 1)
If CInt(Round) > 4 Then
    Round = 1
ElseIf CInt(Round) < 5 Then
    Round = 0
End If
Damage = ADamage(0) + Round

```
Link to comment
Share on other sites

I'm not familiar with arrays, but anyways, I tried understanding your code, basicly what it does is it checks for the value after the ".", and if it's over 5 or under 4.. Etc (Rounding, I believe?)

I tried implementing it in my OnAttack.ess File (I forgot to mention I was using ES, Might be different?) but now it just doesn't affect the NPC in any way, It just never seems to do any damage.

Here's my Sub OnAttack, I might be doing something wrong.

```
Sub OnAttack(Index, Damage)

ADamage = Split(Rand(Int((GetPlayerSTR(Index)) - (Int((GetPlayerSTR(Index)) * 0.05))), Int((GetPlayerSTR(Index)) + ((GetPlayerSTR(Index)) * 0.05))), ".")
Round = Left(Damage(1), 1)
If CInt(Round) > 4 Then
    Round = 1
ElseIf CInt(Round) < 5 Then
    Round = 0
End If
Damage = ADamage  + Round

If Damage < 1 Then
Exit Sub
End If

If GetPlayerTarget(Index) > 0 Then
Call DamageNPC(Index, GetPlayerTargetNPC(Index), Damage)

Exit Sub
End If
End Sub

```
Link to comment
Share on other sites

And after a lot of trial and error, I've finally managed to fix this, thanks a ton guys.

I believe a random attacking script that uses percentage would be a good addition to SE.

Anyways, you guys can manually replace your Sub OnAttack by this

```
Sub OnAttack(Index, Damage)

Damage = Cint(Rand(Int(GetPlayerSTR(Index) - GetPlayerSTR(Index) * 0.20), Int(GetPlayerSTR(Index) + GetPlayerSTR(Index) * 0.20)))
If Damage < 1 Then
Exit Sub
End If

If GetPlayerTarget(Index) > 0 Then
Call DamagePlayer(Index, GetPlayerTarget(Index), Damage)

Exit Sub
End If

If GetPlayerTargetNPC(Index) > 0 Then
Call DamageNPC(Index, GetPlayerTargetNPC(Index), Damage)

Exit Sub
End If
End Sub

```
0.20 = 20% So this means +/- 20%

Thanks Soul and Admiral Refuge, and Lol, Sorry If it sounds like Im giving this script, It's just that I had trouble finding a random attack script using the search bar, So I figured that posting this one might be helpful to some.

~Redyz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...