How would I make a decimal variable round to a whole number?

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
I want to clarify as much as possible that I do NOT believe this is a script-related issue. Basically, I use a script written by a member here named @Heirukichi. To my understanding, all that this script does is store damage a character has taken into a variable, when that character is under a specific state, instead of inflicting it to their HP. To make the skill interesting, I created a common event to subtract HP from the user when the state is first activated, based on the current variable of stocked damage (So you would take the amount of damage you've saved up from the last use of Damage Shield before the variable will be reset.) Whenever this happens though, the amount of damage you take is never a whole number, but a really long variable. This does some REALLY weird things to the HP number display in the menu. So my question is, what is the simplest method to the round the variable Damage Shield to a whole number? I appreciate any help, and thank you in advance.ThisiswhatImean.png
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
@Solr That would be 'round()'. So, 'number_variable.round()'. That function will round the number up to the nearest whole number.
 

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
this.png
@Solr That would be 'round()'. So, 'number_variable.round()'. That function will round the number up to the nearest whole number.
I think I entered something wrong, it gave me a script error. Did I write this correctly?
 
Last edited:

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
You have to use round without arguments.

Example:
Code:
my_var = 29.154893726
my_rounded_var = my_var.round
puta my_rounded_var # This prints 29
If you want the actual value to be rounded to the highest integer that is less than the value itself you can use to_i instead.

Code:
my_var = 35.9786124
my_int_var = my_var.to_i # this sets it to 35
my_rounded_var = my_var.to_i # this sets it to 36
EDIT:
If you want to read about how round works in depth you can take a look at the documentation (although there is not much more to say about it, except for multiples of ten).
 
Last edited:

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
@Heirukichi Oh hey, it's Heirukichi in person! I love your scripting work, I'll be sure to credit you. I clicked on that link and I think I kind of understand, but I think I only kind of "get" how to round up or down solid integers. How would I apply that for a variable, like how would that actually look? I keep getting script call errors. I'm really inept when it comes to ruby script you see. In the screenshot 27 is my variable.
again.png

You have to use round without arguments.

Example:
Code:
my_var = 29.154893726
my_rounded_var = my_var.round
puta my_rounded_var # This prints 29
If you want the actual value to be rounded to the highest integer that is less than the value itself you can use to_i instead.

Code:
my_var = 35.9786124
my_int_var = my_var.to_i # this sets it to 35
my_rounded_var = my_var.to_i # this sets it to 36
EDIT:
If you want to read about how round works in depth you can take a look at the documentation (although there is not much more to say about it, except for multiples of ten).
Sorry about my previous post I just now figured out how to properly write it. This isn't really an issue, but something kind of strange is happening though. Whenever the common event that inflicts damage shield is triggered immediately after battle, the script call to round the number is ignored completely for some reason. Like no error window or anything, it's just ignored and the character takes a really long decimal worth of damage. Whenever I trigger it in an event separate from battle though, it works just fine. Weird huh? Thank you for all your help!

@Heirukichi
Aw man, I just realized something. I think this is because of an incompatibility with another battle script, but for whatever reason, the variable that I use to store damage in from the damage variable shield script always seems to be either 48, 50, or 52, no matter how damage the character actually takes. I don't think I will be able to use your script to it's full potential, so I'm not going to use it. Thanks so much though, I think you are really talented and this script gave me a lot of inspiration for ideas and game mechanics.
 
Last edited by a moderator:

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,867
Reaction score
5,240
First Language
Dutch
Primarily Uses
RMXP

Solr, please avoid double posting, as it is against the forum rules. You can use the "Edit" function on your posts to add additional information you've forgotten or respond to multiple people. You can review our forum rules here. Thank you.


Please use the 'Edit' button in the future to add on to your post.
I've merged your triple post together.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
@Solr Well, first of all, if you want to round variable 27 you should use this:
Code:
$game_variables[27] = $game_variables[27].to_i
That said, if it somehow interacts with another script you are using, why don't you simply change the id of the variable used? It really sounds like something is overwriting its value to me. Of course, I cannot be entirely sure as you did not even mention which one of my scripts you are using so I cannot be sure, but if the value is always the same, changes of another script overwriting it are high.
 

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
@Solr Well, first of all, if you want to round variable 27 you should use this:
Code:
$game_variables[27] = $game_variables[27].to_i
That said, if it somehow interacts with another script you are using, why don't you simply change the id of the variable used? It really sounds like something is overwriting its value to me. Of course, I cannot be entirely sure as you did not even mention which one of my scripts you are using so I cannot be sure, but if the value is always the same, changes of another script overwriting it are high.
Ok so, I used the script call of rounding variable 27 like you posted, but I got a "No method" error window. I tried it again with the other method of rounding variables, and I changed the variable from 27 to 50, and it still gives me the same three values every time. This is the script of yours that I have been using. I'm not expert with this language at all, but I'm pretty sure the issue is being caused by a different battle script I am using.

class Game_Battler < Game_BattlerBase
#--------------------------------------------------------------------------
# * Calculate Damage
#--------------------------------------------------------------------------
def make_damage_value(user, item)
value = item.damage.eval(user, self, $game_variables)
value *= item_element_rate(user, item)
value *= pdr if item.physical?
value *= mdr if item.magical?
value *= rec if item.damage.recover?
value = apply_critical(value) if @result.critical
value = apply_variance(value, item.damage.variance)
value = apply_guard(value)
if state?(29)
$game_variables[27] = value
value = 0
end
@result.make_damage(value.to_i, item)
end
end
@slimmmeiske2 Thank you for correcting me, I did not mean to disrupt the forums in anyway and I will pay more attention to my posting from now on.
 
Last edited:

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
The only reason you can get a "no method" error is when your variable is not a numeric value. however, when that happens, the error itself tells you which kind of instance your variable is. If you could post a screenshot of your error message, that would be very helpful. Alternatively, you should have no problems doing this:
Code:
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Calculate Damage
  #--------------------------------------------------------------------------
  def make_damage_value(user, item)
    value = item.damage.eval(user, self, $game_variables)
    value *= item_element_rate(user, item)
    value *= pdr if item.physical?
    value *= mdr if item.magical?
    value *= rec if item.damage.recover?
    value = apply_critical(value) if @result.critical
    value = apply_variance(value, item.damage.variance)
    value = apply_guard(value)
    if state?(29)
      $game_variables[27] = value.to_i
      value = 0
    end
    @result.make_damage(value.to_i, item)
  end # Calculate Damage
end # end of Game_Battler class
Keep in mind that if that variable does not contain a numeric value, it is possible that something is changing it without you knowing about it.
 

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
The only reason you can get a "no method" error is when your variable is not a numeric value. however, when that happens, the error itself tells you which kind of instance your variable is. If you could post a screenshot of your error message, that would be very helpful. Alternatively, you should have no problems doing this:
Code:
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Calculate Damage
  #--------------------------------------------------------------------------
  def make_damage_value(user, item)
    value = item.damage.eval(user, self, $game_variables)
    value *= item_element_rate(user, item)
    value *= pdr if item.physical?
    value *= mdr if item.magical?
    value *= rec if item.damage.recover?
    value = apply_critical(value) if @result.critical
    value = apply_variance(value, item.damage.variance)
    value = apply_guard(value)
    if state?(29)
      $game_variables[27] = value.to_i
      value = 0
    end
    @result.make_damage(value.to_i, item)
  end # Calculate Damage
end # end of Game_Battler class
Keep in mind that if that variable does not contain a numeric value, it is possible that something is changing it without you knowing about it.
Well, here is the error message.
errormessage.png
Also thank you so much for the updated script! Everything works perfectly and the damage taken is now always a whole number. The second problem still remains, but I think I know what is causing it. I am so dumb for not realizing it sooner hahaha. The reason the damage taken is seemingly always 48, 50, or 52 is because that's roughly the amount of damage the test enemy will do to the test actor in a single use of the one skill I gave it. I'm not sure if your script is designed like this purposely, but the stored damage is not accumulative. I have a number of scripts that make alterations to the way damage is calculated during a battle, so they could also be affecting your script. It would be extremely unreasonable to ask you to go out of your way to the modify your script anymore for my sake. I like this script very much, so I am still going to use it. I will just nerf the state so it disappears either after the actor has taken a certain amount of damage, or after a single turn. Thank you for everything, Heirukichi. :)
 
Last edited:

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
From the error you get it looks like you made some mistake in the script call, mostly something like this:
Code:
$game_variables[27] = [27].to_i

Also, the script itself only stores the last damage dealt. If you want to store more you have to use this:
Code:
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Calculate Damage
  #--------------------------------------------------------------------------
  def make_damage_value(user, item)
   value = item.damage.eval(user, self, $game_variables)
   value *= item_element_rate(user, item)
   value *= pdr if item.physical?
   value *= mdr if item.magical?
   value *= rec if item.damage.recover?
   value = apply_critical(value) if @result.critical
   value = apply_variance(value, item.damage.variance)
   value = apply_guard(value)
   if state?(29)
     $game_variables[27] += value.to_i # This line changed
     value = 0
   end
   @result.make_damage(value.to_i, item)
  end # Calculate Damage
end # end of Game_Battler class
However, if you do, you have to reset that value when it is supposed to start from 0 once again (e.g. when starting a new battle or when the said state is no longer active). That is something you have to do manually by setting your variable 27 to 0. Whether or not you do this with event commands is up to you, just know that using script calls for this might be better as you can use that in the damage formula as well.
 

Solr

Living Weapon
Veteran
Joined
Apr 20, 2019
Messages
46
Reaction score
7
First Language
English
Primarily Uses
RMVXA
From the error you get it looks like you made some mistake in the script call, mostly something like this:
Code:
$game_variables[27] = [27].to_i

Also, the script itself only stores the last damage dealt. If you want to store more you have to use this:
Code:
class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * Calculate Damage
  #--------------------------------------------------------------------------
  def make_damage_value(user, item)
   value = item.damage.eval(user, self, $game_variables)
   value *= item_element_rate(user, item)
   value *= pdr if item.physical?
   value *= mdr if item.magical?
   value *= rec if item.damage.recover?
   value = apply_critical(value) if @result.critical
   value = apply_variance(value, item.damage.variance)
   value = apply_guard(value)
   if state?(29)
     $game_variables[27] += value.to_i # This line changed
     value = 0
   end
   @result.make_damage(value.to_i, item)
  end # Calculate Damage
end # end of Game_Battler class
However, if you do, you have to reset that value when it is supposed to start from 0 once again (e.g. when starting a new battle or when the said state is no longer active). That is something you have to do manually by setting your variable 27 to 0. Whether or not you do this with event commands is up to you, just know that using script calls for this might be better as you can use that in the damage formula as well.
Ah thank you so much, I am not worthy of your scripting genius and generosity. This does exactly what I want it to do! The possibilities are endless. A special Colosseum type event where you fight monsters and earn rare items, but you only keep the items if you can survive the accumulative damage that is inflicted onto you at the end. A "resolve" type skill that allows a user to power through damage momentarily and have it inflicted to them later either as slow fractions or all at once. It's all possible now thanks to you. I can't thank you enough :) I'll be sure to mind manually reset thing. It's no issue at all.

perfect.png
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,454
Members
137,821
Latest member
Capterson
Top