Changing variable inside damage formula before damage calculation, that depends on Actor/Enemy ID using it?

Hi,

I have following problem and can’t find a solution for it. My goal is to change a variable inside the damage formula of a skill before the damage calculations happens. The variable should change depending who’s using the skill. Either party member or enemy.

Something like this:
(a.atk * v[1] - b.def * 2)

v[1] gets the result of checking party member or enemy ID who's using the skill.

Example:
Enemy ID uses that skill. Its ID in the database is 8. Before damage calculations happens, there is a conditional branch in battle that checks this ID and changes v[1] into a specific value assigned to that ID. Something like:

Enemy ID = 8
If Enemy ID = 8 => v[1] value changes to 3

The same should work for party members.

I tried to find script calls for it but nothing seems to work. Like BattleManager.actor().actorId() === 1 but this gives in error.

Is there any way doing this without plugins? This is my preferred way if its possible.

Huge thanks in advance.
You may find this thread useful:

I tried to find script calls for it but nothing seems to work. Like BattleManager.actor().actorId() === 1 but this gives in error.
There's no need for that, the damage formula box provides a and b.

Can the same skill be used by both actors and enemies? If yes, the code will be slightly more complex to make sure you check the correct property (enemy ID or actor ID). The link I provided above lists the isActor() function.

Is there any way doing this without plugins? This is my preferred way if its possible.
Why? Everything you type into the damage formula is already JavaScript, if this is a complex set of logical checks it will be much more readable to stick it into a plugin file and just call the function from the damage formula.

You don't really give enough specifics for one to write code for you - can the variable be different values for different IDs? Only one value if it's a specific ID?

Just as an example of setting the variable to 3 if it's enemy ID 8 or to 5 if it's actor ID 2. Otherwise it stays at 1 as a neutral modifier:
Code:
if (a.isActor() && a.actorId()==2) v[1]=5; else if (!a.isActor() && a.enemyId()==8) v[1]=3; else v[1]=1; a.atk * v[1] - b.def * 2
This already helped me so much. Thanks ALOT. Your answer opened new doors for me.

„Can the same skill be used by both actors and enemies? If yes, the code will be slightly more complex to make sure you check the correct property (enemy ID or actor ID). The link I provided above lists the isActor() function.“​

Yes. This is my intention.

„You don't really give enough specifics for one to write code for you - can the variable be different values for different IDs? Only one value if it's a specific ID?“​

Sorry my bad. I simplified the problem it too much and focused just on that one variable part cause there I was stuck. I wanted to add 2 elements for skills and make element rates stack. One elementRate is already fix cause of the skill element itself. Can be hardcoded in the formula. Other elementRate depends on users element. That’s how I came with my formula idea after googling the non variable version of it. If there is anything better than I'm happy for alternatives.

Question about the enemy ID check:
Is there a reason why you used !a.isActor() instead of a.isEnemy() beside habit/best practice?

Another question on how to make the formula more efficient/universal:
What’s the best way to group IDs? Let’s say the IDs are already sorted. If actorId() is between 1-20 and enemyId() between 5-30. First I have the check variable range version:

JavaScript:
if (a.isActor() && a.actorId() >= 1 && if a.actorId() <= 20) v[1]=3;
else if (!a.isActor() && a.enemyId() >= 5 && if a.actorId() <= 30) v[1]=3;
else v[1]=1;
a.atk * v[1] - b.def * 2

And if they are not sorted cause of adding new actors or enemies out of order in later development. Would this approach still be the best or maybe using arrays with ID values inside? What about this. I’m still learning so I don’t know if it. Can’t figure out how to display an item of an array to test it. Nothing I tried seems to work or give error so I'm little sceptical.

First step: initialize variables and arrays at start of the game cause they are constants

JavaScript:
$gameVariables.setValue(1, [1,6,12,30]);
$gameVariables.setValue(2, [1,2,3,4,5,11]);

Second step: Damage Formula

JavaScript:
var actor_group_1 = var[1];
var enemy_group_1 = var[2];

if (a.isActor() && actor_group_1.includes(a.actorId()) v[3]=3;
else if (!a.isActor() && actor_group_1.includes(a.enemyId()) v[3]=3;
else v[3]=1;
a.atk * v[3] - b.def * 2
I wanted to add 2 elements for skills and make element rates stack. One elementRate is already fix cause of the skill element itself. Can be hardcoded in the formula. Other elementRate depends on users element.
You might consider simplifying your life and using something like the VisuStella Elements and Status Menu Core, which provides handling for multi-element attacks.

Is there a reason why you used !a.isActor() instead of a.isEnemy() beside habit/best practice?
No, just to be visually consistent. There's no difference.

What’s the best way to group IDs? Let’s say the IDs are already sorted. If actorId() is between 1-20 and enemyId() between 5-30. First I have the check variable range version:

JavaScript:
if (a.isActor() && a.actorId() >= 1 && if a.actorId() <= 20) v[1]=3;
This is invalid syntax. You can't stick the word "if" inside of another conditional - please see what I posted before for a correct example.

But yes, you'd just look for it to be >=1 and <= 20.

Is there an actual purpose? Are there other actors, after ID 20, that have a different or no element? If it's the same for all actors, there's no reason to be looking at their ID at all.

Also, the variable is going to be the same value for enemies and actors? Then there's no need for two separate conditionals, you just use OR.

First step:
This is the kind of situation where I was saying it would be easier to move this into a plugin, rather than taking up game variables and events and trying to cram all this code into all of your damage formulae. It would be much more efficient to use array methods or a switch statement than to have all of this code in the damage formula.

JavaScript:
var actor_group_1 = var[1];
var enemy_group_1 = var[2];
Where did this var come from? That's going to produce a syntax error for an undefined variable and cause your damage formula to crash out.

A much simpler approach (if you don't want to just use a multi-element plugin) would be to use notetags. I don't know exactly what this skill is for, but let's say you're making it be more powerful if the battler is the fire element.

So you put the <fire> notetag into the actor or enemy, then do this in your damage formula:
Code:
if (a.isActor() && $dataActors[a.actorId()].meta.fire || !a.isActor() && $dataEnemies[a.enemyId()].meta.fire) v[3]=3;
You might consider simplifying your life and using something like the VisuStella Elements and Status Menu Core, which provides handling for multi-element attacks.
It's my first project and I aim for doing everything without plugins for now until I know enough. This damage formula is very helpful for me learning and understanding stuff. I just use small QOL plugins or if it's something that doesn't worsen my learning experience.

But is there a free multi-element plugin that does easily what I'm trying to do? VisuStella let's you apply multiple elements to a skill in a <Multi-Element: > tag but I can't see how it will handle variable element like I try to do. Is there a detailed documentation after buying it, that shows how to do it?

This is invalid syntax. You can't stick the word "if" inside of another conditional - please see what I posted before for a correct example.
Oh. Didn't notice. Thanks for pointig this out. So removing the if makes this formula usable? If I add an enemey/actor out of order then I could do something like this?

JavaScript:
if (
a.isActor() && a.actorId() >= 1 && a.actorId() <= 20 ||
a.isActor() && a.actorId() >= 36 && a.actorId() <= 40 ||
a.isActor() && a.actorId() == 71 ||
!a.isActor() && a.enemyId() >= 1 && a.enemyId() <= 20 ||
!a.isActor() && a.enemyId() >= 36 && a.enemyId() <= 40 ||
!a.isActor() && a.enemyId() == 71 ||
) v[1]=3;

else v[1]=1;
a.atk * v[1] - b.def * 2

Is there an actual purpose? Are there other actors, after ID 20, that have a different or no element? If it's the same for all actors, there's no reason to be looking at their ID at all.

Also, the variable is going to be the same value for enemies and actors? Then there's no need for two separate conditionals, you just use OR.
Actors and Enemies have different elements and adds up with the used skill's element. If I add new enemies then it's likely that the new enemy has different element the previous ones. Otherwise I could try to sort them from the start but for testing it will be unsorted I guess.

Maybe it makes it clearer mentioning that the own element has different variation than possible skill elements. So something like Fire*Fire isn't possible. It's like in Digimon.

I suppose that the variable can be the same for Enemies and Actors cause they are using the same skills and are influenced by same weakness handling system. Actor and Enemy are interchangeable in this regard.
Where did this var come from? That's going to produce a syntax error for an undefined variable and cause your damage formula to crash out.
Understand. Must looked up something wrong thinking that I can temporary define new var inside the damage formula.
A much simpler approach (if you don't want to just use a multi-element plugin) would be to use notetags. I don't know exactly what this skill is for, but let's say you're making it be more powerful if the battler is the fire element.

So you put the <fire> notetag into the actor or enemy, then do this in your damage formula:
Code:
if (a.isActor() && $dataActors[a.actorId()].meta.fire || !a.isActor() && $dataEnemies[a.enemyId()].meta.fire) v[3]=3;
The plugin is unknown to me but does it do what I try to accomplish? The skills I plan do more or less damage to the enemy if user's own element is strong/weak to enemy's element. Otherwise its neutral.

Does your plugin enhanced code mean if actor or enemy is assigned with <fire> notetag inside their respective database entry then v[3]=3?
Understand. Must looked up something wrong thinking that I can temporary define new var inside the damage formula.
You can define new variables, but then you're looking at var[1] and var[2] and that doesn't mean anything. You haven't declared any array named var (and you can't, it's a reserved word).

Does your plugin enhanced code
You might be confused, I didn't give any code that has anything to do with a plugin.

if actor or enemy is assigned with <fire> notetag inside their respective database entry then v[3]=3?
Yes, that's what I said in post #4:
A much simpler approach (if you don't want to just use a multi-element plugin) would be to use notetags.

So you put the <fire> notetag into the actor or enemy, then do this in your damage formula:
You might be confused, I didn't give any code that has anything to do with a plugin.
It's something I only saw with plugins yet so that's why I'm confused. I did fast testing and it seems to work and would make the formula much easier. Much appreciated new technique I discovered.

How does the tag system work tho? Is it some kind of non-numeral placeholder string that has no effect until the conditions are met somewhere to do something? I tried out stuff like putting random notetag or using a whatever meta and didn't get any error. Would be good to know how to use it with examples. I really can't find anything about meta tags.
How does the tag system work tho? I really can't find anything about meta tags.

First result from Googling "rpg maker meta notetag" 😉

That should explain it sufficiently - if not, there are other threads, just mostly getting into how to use it for writing parsers in plugins.
Is there a way to check the Actor's Class in the damage formula? I looked in the Damage Formula 101 and Script Call Spreadsheet and the best I could find and think of was this:

- $dataActors[a.actorId()].classId === 12
- a.actorId().isClass(12)
- a.isActor() && a.actorId().isClass($dataClasses[12])

Nothing works. Either this doesn't change the Formula for the Actor at all or the damage output becomes 0. Wanted to achieve something like the meta.fire solution but it seems like it's not that easy with Classes if possible at all.

How can I solve this? I will use a Plugin in my final result but for learning purpose I'm really curious how it works and what my error is.
- $dataActors[a.actorId()].classId === 12
This should work. It might not be correct, because if you have any class changing in your game your actor might not be the same class as their starting class in the database. But it should work.

- a.actorId().isClass(12)
This is incorrect because isClass() takes a Class object as an argument, not an integer. Per the example in the script call spreadsheet:
1693250753934.png


Also, see below about actorId().

- a.isActor() && a.actorId().isClass($dataClasses[12])
This one...just makes no sense. actorId() returns an integer, the ID of the actor. Numeric values don't have any isClass() method.

So your second method should work if you fix the argument you're passing into it. Note that if the skill can possibly be used by enemies, you should include the isActor() check like you do in your third line.
This should work. It might not be correct, because if you have any class changing in your game your actor might not be the same class as their starting class in the database. But it should work.
It really worked if the starting class equals the one in the formula like you said. I was doing a Class Change before battles all time. For the enemies everything is fine so far cause I can manage everything inside their Notes and metas.

So does this only work for the starting class or is there a solution for this problem?
...I told you the solution in my prior post.

So your second method should work if you fix the argument you're passing into it. Note that if the skill can possibly be used by enemies, you should include the isActor() check like you do in your third line.
How does passing the argument work here to make it work like I want? My programming knowledge is still limited. I tried this and other variants of it but nothing works.

- $gameParty.members()[a.actorId()].isClass($dataClasses[12])
- $gameParty.members()[10].isClass($dataClasses[12])

Even putting Actor ID directly in it or did I misunderstand something?
Okay, I didn't even notice until now that it was extra wrong because, again, you have this extra actorId() thing. I dunno where you got your mini obsession with that 😉

The correct check is a.isClass($dataClasses[12])

Original thread: https://forums.rpgmakerweb.com/threads/changing-variable-inside-damage-formula-before-damage-calculation-that-depends-on-actor-enemy-id-using-it.160362/