Selecting Targets when using Common Event in battle

So I have a skill, Dominate, which runs a common event to check for one of two states.


The states are :is predator, and is prey. They are passive states utilizing the YEP Passive States plug in.


So here's my situation: See that If: #? bit? How can I make it so that this common event will effect whichever enemy is targeted? See sometimes it won't be the enemy in the #1 position that I want to target, sometimes it will be the enemy in the #2 position. Does anyone know how I can control this? 

Screen Shot 2016-10-30 at 7.54.20 PM.png
you'll need one conditional branch for each possible enemy


if #1 is affected


if #2 is affected


if #3 is affected


and so on.
Hello @Volimance!


A purely evented solution you can try is to give the enemy target another invisible state. Let's call it "target".


You then in your common event check wich enemy has the state target and run your conditionnals on him only.


It's quite a lot of conditionnals but I don't see any other way.


For a fully automated solution you'll have to use javascript, because the editor indeed lacks a way to get the current target of an action directly. (And to add/check/remove a state by its id too.)


The script snippet spoiler'd below is a way to do that.

Spoiler
var targetIndex = BattleManager._action._targetIndex //get the target of the current action
var target = $gameTroop.members()[targetIndex]; //get the enemy object from the index
var predatorSID = 5; var predatorSR = 7; //change here predator state ID and predator state Result to what you need
var preySID = 6; var preySR = 8; //same but with prey
if (target.isStateAffected(predatorSID)) { //if target is a predator
target.addState(predatorSR); //add the state of ID predator result
} else if (target.isStateAffected(preySID)) { //same with prey
target.addState(preySR);
}


This is meant to be used for single target spells, in a common event called by using a skill or item in battle only.


It won't work for multiple target spells and will crash your game if used outside battle.


I don't know if it works with yanfly passive states, but it does in my test project.
you'll need one conditional branch for each possible enemy


if #1 is affected


if #2 is affected


if #3 is affected


and so on.

Gah I was afraid this would be the response. Thank your for answering!
Hello @Volimance!


A purely evented solution you can try is to give the enemy target another invisible state. Let's call it "target".


You then in your common event check wich enemy has the state target and run your conditionnals on him only.


It's quite a lot of conditionnals but I don't see any other way.


For a fully automated solution you'll have to use javascript, because the editor indeed lacks a way to get the current target of an action directly. (And to add/check/remove a state by its id too.)


The script snippet spoiler'd below is a way to do that.

Spoiler
var targetIndex = BattleManager._action._targetIndex //get the target of the current action
var target = $gameTroop.members()[targetIndex]; //get the enemy object from the index
var predatorSID = 5; var predatorSR = 7; //change here predator state ID and predator state Result to what you need
var preySID = 6; var preySR = 8; //same but with prey
if (target.isStateAffected(predatorSID)) { //if target is a predator
target.addState(predatorSR); //add the state of ID predator result
} else if (target.isStateAffected(preySID)) { //same with prey
target.addState(preySR);
}


This is meant to be used for single target spells, in a common event called by using a skill or item in battle only.


It won't work for multiple target spells and will crash your game if used outside battle.


I don't know if it works with yanfly passive states, but it does in my test project.



If I am understanding the script correctly then for the variables enumerated above (5, 6, 7, 8) I would use the IDs of the states as the occur in my project, correct?

Then once I've updated the script with the locally accurate state IDs, I'd then make a common event, click that diamond, turn to the third page and click on the script... button and paste the whole mess into that window, hit okay. I'd then link the skill to the common event as normal.

Also, given your precaution, I'd make certain to set the skill to only be accessible from the battle menu to prevent a game crash.

Is my understanding close to correct?

Thank you so much for taking the time to help me with this!!!
Yes, you need to change predatorSID to the id of the predator state (I used 5 randomly), predatorSR to the id of the state you want to inflict if the target is a predator (I used 7 randomly).


Then do the same for the prey part.


Then you indeed paste this in the script event command, in a common event and link it to a skill via normal.


Try it and tell me if everything goes alright.


If it doesn't provide screenshot of the setup (skill and common event) and the result, and I'll help you as best I can.
@Astfgl66 WooHoo! I got it to work exactly as I wanted it! You sir or madam, get yourself a nice fat credit in the game! I'm gonna assume you don't mind if I edit the code and reuse it other similar situations. There is a corresponding skill "Calm" which works great on prey, but really upsets predators.

Also, if you are really bored, is there a way to add a text popup for when the skill is used in the wrong way?


(oh boy oh boy oh boy, vols is really happy!!!!)
Use it to your heart's content: modify it, repost it, read it out loud outside at a full Moon if you want ^^.


As for your request, how about we turn a switch on if it's a predator, off if it's a prey. Then you can make a conditionnal for that switch and display the message you want or do anything really. I can write code for a text message in javascript if it's needed, but it's much less intuitive to modify after.


If that works for you here is an updated version:


var targetIndex = BattleManager._action._targetIndex
var target = $gameTroop.members()[targetIndex]; var sID = 1 //change sID to the id of the switch you want to modify
var predatorSID = 5; var predatorSR = 7;
var preySID = 6; var preySR = 8;
if (target.isStateAffected(predatorSID)) {
target.addState(predatorSR); $gameSwitches.setValue(sID,true); //turn switch (sID) true if predator
} else if (target.isStateAffected(preySID)) {
target.addState(preySR); $gameSwitches.setValue(sID,false); //turn switch (sID) false if prey
}


End result of that: switch 1 will be on if the target was a predator, off if it was a prey.


If you copy paste that, don't forget to re change the state IDs again ^^.


Glad to help.
Okay this works. I'm fairly confident I can make it do what I want it to do!


Braining hurts a little, but I try!
Use it to your heart's content: modify it, repost it, read it out loud outside at a full Moon if you want ^^.


As for your request, how about we turn a switch on if it's a predator, off if it's a prey. Then you can make a conditionnal for that switch and display the message you want or do anything really. I can write code for a text message in javascript if it's needed, but it's much less intuitive to modify after.


If that works for you here is an updated version:



var targetIndex = BattleManager._action._targetIndex
var target = $gameTroop.members()[targetIndex]; var sID = 1 //change sID to the id of the switch you want to modify
var predatorSID = 5; var predatorSR = 7;
var preySID = 6; var preySR = 8;
if (target.isStateAffected(predatorSID)) {
target.addState(predatorSR); $gameSwitches.setValue(sID,true); //turn switch (sID) true if predator
} else if (target.isStateAffected(preySID)) {
target.addState(preySR); $gameSwitches.setValue(sID,false); //turn switch (sID) false if prey
}


End result of that: switch 1 will be on if the target was a predator, off if it was a prey.


If you copy paste that, don't forget to re change the state IDs again ^^.


Glad to help.

Worked like a charm, super easy to implement good feedback for both proper and improper usage!  Thank you!
Glad everything's working properly. Thank you for keeping me updated.


Good luck and happy making!

Original thread: https://forums.rpgmakerweb.com/threads/selecting-targets-when-using-common-event-in-battle.70374/