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.
How would I make a decimal variable round to a whole number?
RPG Maker VXAce Support@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?
You have to use round without arguments.
Example:
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.
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).
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).
@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.
@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.
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.
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.
@Solr Well, first of all, if you want to round variable 27 you should use this:
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.
Code:
$game_variables[27] = $game_variables[27].to_i
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.
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:
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.
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.
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. 🙂
From the error you get it looks like you made some mistake in the script call, mostly something like this:
Also, the script itself only stores the last damage dealt. If you want to store more you have to use this:
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.
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.
Original thread: https://forums.rpgmakerweb.com/threads/how-would-i-make-a-decimal-variable-round-to-a-whole-number.113950/