Script call to check if in battle screen

Hey everyone. 👋

So, I'm trying to run a condition that checks if the player is in the battle screen, but I don't think you can do this unless you a bit of script. I've had a brief look at the script call list, and I found this:

BattleManager.setup(troopId, true/false, true/false);
$gamePlayer.makeEncounterCount();
SceneManager.push(Scene_Battle);

I don't really know what any of that means in spite of the instructions being along side of it... but it's fun to speculate. I'm going to guess that if I wrote something like:

if SceneManager.push(SceneBattle) true
... that's... kinda... what I might want?
script call via conditional branch
Code:
SceneManager._scene instanceof Scene_Battle;

Spoiler: EXPLANATION
Code:
SceneManager._scene
this line of code can be used anywhere to return the active scene.

Code:
instanceof
this is a built-in method which will compare the statement on the left (the active scene as defined above), to the statement on the right (described below).

Code:
Scene_Battle
this line of code is telling instanceof what we want to compare the statement on the left to; any object can be specified here (this time, we are looking for an instance of the battle scene as defined above).
Right, thank you very much @dbchest . Could you go on to explain why:

if SceneManager._scene instanceof Scene_Battle a.addState(10);

is not a valid syntax?
syntax can sometimes depend heavily on where you're writing the code. for example, if you are using a script call via the event command list within an event, you do not need to write 'if'; since you are writing your code within a conditional branch, the 'if' is already taken care of for us and we need only write our condition (SceneManager._scene instanceof Scene_Battle). following this, you would continue to use event commands to apply the state to the actor you want within the conditional branch.

where exactly are you writing this code? are you using the script event command on page three? or are you using the conditional branch event command on page one, coding within the script command there?
In the damage formula box, actually.
i am not sure if you can evaluate custom scripts within the damage formula box or not; i have little experience with that particular aspect of the engine, so i will have to let someone with more knowledge chime in at this point. apologies.
No worries, thanks anyway. It's explained the calls a bit to me, at least.
You want to check if your party is battling, right? You can use $gameParty.inBattle(). It will return true if your party is in battle, or false if not. Use it in formula to check for conditions.
For example, i want my Heal to heal double the amount in battle so here is the formula:

Code:
$gameParty.inBattle() ? a.mat * 2 : a.mat
Wooooo! Thanks @JamesRyan Just what I'm after. For future reference - I've looked at the script calls list but it doesn't seem to feature a lot of things that can go in the damage formula box. Do you know if there's anywhere I can find this info?

EDIT: Still having trouble to get it to work with my particular formula. I want to add a state to the target if the condition is true, right?

So...

$gameParty.inBattle() ? b.addState(12)

would be what I would assume, but it's not working. What's the reasoning here?
If you use this statement, please follow this structure:
Code:
condition ? true : false
Both actions for true and false must be there to make it work. If you want to execute something only when the condition is true, you can use the usual "If' statement.

I have googled and learnt some of these since i use Yanfly's Buffs & States Core in lunatic mode a lot. But i think you might want to take a look at his Tips & Tricks to know some more about this, since he uses a lot of JavaScript codes there:

And some here for damage formula: https://rpgmakermv.co/threads/damage-formulas-101-mv-edition.2172/
Yes! Thank you very much. I was just about to make an edit saying I'd figured it out. I wasn't offering an alternative.

BUT... after some revision, I realized I didn't actually need to run a check for the player being in the battlescreen. Instead, what I need is a check for a State.

So, I tried the following formula:

if (b.isStateAffected(10)) b.addState(12)

Doesn't work, get the error "unexpected identifier". But this is directly from the thread @JamesRyan linked to. I'm having an awful lot of trouble getting this addState command to work.
How about this:
Code:
if (b.isStateAffected(10)){ b.addState(12) }
or
Code:
if (b.isStateAffected(10)) b.addState(12);

If you know more about some JavaScript codes in MV, you can even use Yanfly's Damage and Skill Core plugins to write more complex formulas in note boxes without any limitation.
@JamesRyan Hmmm, that doesn't seem to be working. The game still crashes with the message:

"Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': float parameter 3 is non-finite"

This is usually an error that comes up when a formula is incorrect, but it's come up every single time I've used 'addState' in the damage formula box. Which is odd, because it's definitely something you can do. This error may also occur when there is no reference to the damage type in the formula box, but even when I set it to HP damage and use something like a.atk * 0 + b.addstate(12) it gives me the same error.

So yeah, I don't really know what's going on.
That's strange. I tested this in my project
Code:
if (b.isStateAffected(10)) b.addState(12);
It looked weird because of "NNN" damage pop up, but it still worked and added state 12 to the target affected by state 10 without any issues.

Btw, you cannot use "a.atk * 0 + b.addstate(12)". Either a.atk or b.addState(12).
Perhaps I can try adding damage to the formula because "NNN" is causing me issues?
I don't think it is the problem, because there was no issues in my tested project. Actually, there is another more comfortable way to both deal damage and add state to the target by using a plugin. As i mentioned above, if you use Yanfly's Skill Core plugin (http://yanfly.moe/2015/10/13/yep-8-skill-core/), you can just simply put damage formula in the formula box, while adding state to the target via skill's note tag box like this:
Code:
<Post-Damage Eval>
if (target.isStateAffected(10)){
target.addState(12);}
</Post-Damage Eval>
The problem is @JamesRyan , it's an item and not a skill.

Here's the idea:

If an actor has the 'Sleep' state cast on them, for example, the item in question is supposed to remove Sleep. After it removes Sleep, it is supposed to add another status that gives them 100% resistance to Sleep.

However, I need a check that makes sure the actor had sleep on them in the first place - otherwise the player could just spam this item for sleep resistance when there was never the 'Sleep' state on the actor in the first place, see?
Don't worry. That note tag in the Skill Core plugin also works with items so you will have no problem using it. As the codes said above, it only added new state if your target already had state 10. You can give it a try to see what it can do.
I think the problem is that if I remove Sleep in the item traits, the post-damage eval is calculated afterward, isn't it?

So if I were going to remove State 10, I would have to do it in the eval AFTER I add State 12, right?

<Post-Damage Eval>

if (target.isStateAffected(10)){

target.addState(12);}

target.removeState(10)

</Post-Damage Eval>

Would that work?
It should work. But your codes will cause state 10 to be removed even if target is not affected by itself. So if you only want to remove state 10 only if target is already affected by it and after state 12 is added, make a little change:
Code:
<Post-Damage Eval>
if (target.isStateAffected(10)){
target.addState(12);
target.removeState(10);}
</Post-Damage Eval>

Btw, never forget to add a semi-colon at the end of the line.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-to-check-if-in-battle-screen.85069/