What's wrong with this formula?

Hello, I need help with a formula of mine. I don't why it's not working as intended, here's a screenshot of the code:

bleedingformula.jpg


It isn't fully completed yet, but basically the event should do this:

(fixed bleeding variable /100)*((luck of character*4) - (luck of enemy * 2))

after that, it should subtract a random number from 1 to 100 to generate some sort of dice roll.

The problem is, no matter what values I choose, even if the luck of my character is at the maximum value. I do obtain always a negative number in return.
Are there ways of debugging, to see the course of the variables? Unfortunelately it's not possible to see it while in battle.

Maybe someone sees the flaw here. I have no possible idea...
May I ask why this isn't in a damage formula?.

Though to answer you're other questions it's probably negative because you have 15/100 inputed into variables, and since variables round decimals to the nearest integer, it becomes 0. So you're whole formula can be re-written as 0*((luck of character*4) - (luck of enemy * 2)) which is 0 and then it's subtracting otherwhole numbers so the answer anywhere between -1 to -100
@Zarsla the multiplication happens after the substraction, so it could only be zero, not negative if that was the only problem.

@Klunky2 Best guess at the moment is that the numbers for luck are wrong.
Can you give us which numbers you're using? Keep in mind that the enemy luck depends on the troop in the battle when a skill calls that process.

Additionally, if you have such a problem the fastest solution is usually to add several show text commands to display the variable value at any given time. Especially I suggest adding a show text for variable 4 after that had been loaded to see if it loads the value of the correct enemy.
May I ask why this isn't in a damage formula?.

Though to answer you're other questions it's probably negative because you have 15/100 inputed into variables, and since variables round decimals to the nearest integer, it becomes 0. So you're whole formula can be re-written as 0*((luck of character*4) - (luck of enemy * 2)) which is 0 and then it's subtracting otherwhole numbers so the answer anywhere between -1 to -100

It isn't a damage formula because the effect doesn't do any damage. As far as I know in damage formulas you can only return one value and it's not possible to set values for variables, you can only use them. The common event is triggered after doing a normal attack. After that I want to trigger a specific condition based on the probability which is calculated by the formula.

Are there any options to save decimal values in variables or am I pretty lost here?

edit: @ Andar

Messages are a pretty good Idea, I will try it and tell you the results afterward.
@Zarsla the multiplication happens after the substraction, so it could only be zero, not negative if that was the only problem.

Ok I tried it out, it's right that the bleeding variable rounds down to zero, the calculation of luck seems right. The reason why it throws out negative numbers instead of null lies in the substraction with the random constant from 1 to 100 at the end of the event.

Are the any options the use floats or something like that? A little bit scripting would be know problem if I knew the commands.
May I recommend using formula effects by Himeworks to help with this calculation.
Place this note tag in the skill you want the calculation to take place.
<formula effect>
$gameVariables.set (2, x);
var varry = $gameVariables.value (2);
var formu = ((varry/100) *((a.luk * 4) - (b.luk *2)) + 1 + Math.randomInt (100);
if ( formu >== 0){
$gameSwitches.setValue(9, true);
}
</formula effect>

the x in the set variable should be any number you want.
@Zarsla although your approach was fine (apart from adding the random number rather than subtracting), there's some syntax errors which are going to cause the game to crash.

As mentioned, use the formula effects plugin and use the following (You don't need to use any of the game variables):

<formula effect>
var x = FixedValue; // Replace with the fixed value you want to use. 15 in the screenshot you posted.
x /= 100;
var numTest = x * (a.luk * 4 - b.luk * 2);
var randNum = 1 + Math.random() * 100;
randNum = Math.round(randNum).clamp(1, 100);
numTest -= randNum;
if (numTest >= 0) $gameSwitches.setValue(9, true);
</formula effect>
Hey thanks for the code that's really helpful to understand the use and syntax of javascript.
But something doesn't seem right, I'm getting an unexpected token error after using this code with the normal attack skill.
Do I forget something?

That's how it looks like, I put it on the note tab, just as it needs to be.

Code:
<formula effect>
$gameVariables.set (2, 15);
var varry = $gameVariables.value(2);
var formu = ((varry/100) *((a.luk * 4) - (b.luk *2)) - Math.randomInt(100);
if (formu >= 0){
$gameSwitches.setValue(9, true);
}
</formula effect>

Also one additional question: does the formula calculates even when the attack doesn't hit?
Hey thanks for the code that's really helpful to understand the use and syntax of javascript.
But something doesn't seem right, I'm getting an unexpected token error after using this code with the normal attack skill.
Do I forget something?

That's how it looks like, I put it on the note tab, just as it needs to be.

Code:
<formula effect>
$gameVariables.set (2, 15);
var varry = $gameVariables.value(2);
var formu = ((varry/100) *((a.luk * 4) - (b.luk *2)) - Math.randomInt(100);
if (formu >= 0){
$gameSwitches.setValue(9, true);
}
</formula effect>

Also one additional question: does the formula calculates even when the attack doesn't hit?

As mentioned in my post above, the formula has some syntax problems which are giving the error.
Use the one I provided which should fix the problems.

And judging from the description of the plugin and its usage examples, I would take a guess and say it does calculate even if the attack doesn't hit.
Hey, great thanks, that's a bit more ordered and seems to work, on several cases. But I'm not to sure. I have a final percantage of 17 but the bleeding condition happens quite to rarely. I try to set a non-temporary variable just after the numTest to check if the calculation is as I inteded:
$gameVariables.set (2, numTest);
but unfortunelately after that, the program crashs again.
$gameVariables.set is not the appropriate function.

Use $gameVariables.setValue(2, numTest);
Also, could you describe what exactly you're trying to achieve with the effect? Since as it currently is, it activates a switch which is not turned off later. So, this effect will basically be successful only once (as once it is successful, the switch will be turned on and remain on, so any checks afterwards become meaningless). What are you trying to achieve by turning on the switch?
Ok curiously enough, after I set the variable the value is negative, even after a successful bleeding effect and escaping afterwards. Thats very weird, but it seems to funktionate more or less. I mean, math doesn't lie am I right? Unless any maker-specific processes messing with the numbers.

Nonetheless, thank you Naveed & Zarsla. I'm a huge step ahead and pretty satisfied with this approach.

Also, could you describe what exactly you're trying to achieve with the effect? Since as it currently is, it activates a switch which is not turned off later. So, this effect will basically be successful only once (as once it is successful, the switch will be turned on and remain on, so any checks afterwards become meaningless). What are you trying to achieve by turning on the switch?

Well it's a bit complicated, the formula isn't the only condition to trigger the bleeding effect, the enemy needs less than 10% of his total hp left. Only then you should have the possibility (after every successful hit) to inflict the bleeding status. The actual validation lies in the condition of the bleeding effect itself. (via another plug-in)

Usually it's intended to be a low probabilty, but dependend on the enemies- and own luck I can variate a bit. Also there should be specific weapon types like daggers, which highers the chance to inflict the status. And exactly this seems to be my next mammoth task. xd

I hope I'm able so solve that problem for myself this time, instead of annoying you people off. xD
So what I got from your explanation is, you want it so if the enemy has less than 10% hp, your attacks (not sure if you want all attacks or just basic attacks) run the luck check that you described above, and if that passes, the enemy has the bleed state inflicted? And if the actor has a dagger equipped, the rate increases (not sure if you want an additive increase or a multiplicative increase)? If so that is easily doable, but still not sure what the switch is for.
So what I got from your explanation is, you want it so if the enemy has less than 10% hp, your attacks (not sure if you want all attacks or just basic attacks) run the luck check that you described above, and if that passes, the enemy has the bleed state inflicted? And if the actor has a dagger equipped, the rate increases (not sure if you want an additive increase or a multiplicative increase)? If so that is easily doable, but still not sure what the switch is for.

The switch checks simply if the probability check was a success.

In the Condition tab under the bleeding effect I write the following:

<Passive Condition: Switch 9 ON>
<Custom passive Condition>
if (user.hp / user.mhp < 1){
condition = true;
} else {
condition = false;
}
</Custom passive Condition>

Well ok, I realise at the moment, it's the oppossite, but it does the same anyway.
I see what you're trying to do but that isn't the way to do it. The way it's set up, it'll cause bleed on all enemies below the health percentage, not just the target.

What you should do instead is, use Yanfly's Skill Core plugin (assuming you want the effect to be on the basic attack only) and use the following notetag on the attack skill:

<Post-Damage Eval>
var hpPercent = 0.1; // The hp percent of the enemy below which you want the effect to activate. 0.1 is 10%.
if (target.hpRate() <= hpPercent) {
var x = FixedValue; // Replace with the fixed value you want to use. 15 in the screenshot you posted.
x /= 100;
var numTest = x * (a.luk * 4 - b.luk * 2);
var randNum = 1 + Math.random() * 100;
randNum = Math.round(randNum).clamp(1, 100);
numTest -= randNum;
if (numTest >= 0) target.addState(BleedStateId); // Replace with the id of the bleed state.
}
</Post-Damage Eval>

Original thread: https://forums.rpgmakerweb.com/threads/whats-wrong-with-this-formula.80654/