MZ Message during skill

I'm not sure if this thread belongs here or somewhere else, so sorry if it has to be moved.

I have a skill which 'ejects' someone from the battle permanently. I originally gave it to an enemy, but would now like to be able to give it to an ally as well. However, I obviously don't want this to be used on a boss.

I thought that I would use switch 167 as a conditional in the damage formula box. This is my "this is a boss battle" switch and is used for several things I want to happen automatically in such encounters. The structure I thought to use was
Is switch 167 ON?
if 'yes' have a message appear saying the enemy is too strong for this skill
if 'no' then the damage formula comes into play.

What I have at the moment is:
Code:
if ($gameSwitches.value(167)===true { $gameMessage.push‘The enemy is too powerful for this skill’} else {(b.addState(1); if ($gameParty.isAllDead()) BattleManager._escapeType = 'lose'; b.hide(); 0 )}

I have also tried $gameMessage.add'The enemy....

The message does not appear with either add or push.
The message does not appear whether I use either single or double quote marks
The skill continues but does not eject the enemy anyway.

I have tried different combinations of brackets, but if that's the problem I didn't hit on the right combination.

Could someone give the real code for getting the message to appear and (if and only if you have the time) explain to me why my brackets are wrong (if they are) so that I don't repeat the same error next time.

Thank you
Have you tried to put it in ()?
Unlike Ruby, in JS you have to explicitly call the function. The () indicates that you call that function ("add" is a function from $gameMessage).
So, it should be like $gameMessage.add("text here")

Whether or not "add" is an actual function name from Game_Message, I don't remember.

Also, the bracket in the IF statement should be like
Code:
if ($gameSwitches.value(167)===true) {  ....  }
You missed the ")" at the end of the condition.
@TheoAllen
Thank you very much for this. I've just had a phone call so I shall have to rush away now, but I'll test this as soon as I can.
Also, I think VisuStella finally added selection criteria to the Battle Core.

So you may prefer the style of wasting your turn trying to use the skill and getting the message that the boss is invalid; but you could instead make the user unable to select the boss as a target in the first place.
@TheoAllen Sadly even with the () no message appears.

@ATT_Turan You may just have saved my sanity.
One thing you were missing is the closing bracket for the if statement. And while not technically necessary, I highly recommend adding newlines between statements, it make code a lot more readable. A version with some fixes:

JavaScript:
if ($gameSwitches.value(167)===true) {
    $gameMessage.push('The enemy is too powerful for this skill');
} else {
    b.addState(1);
    if ($gameParty.isAllDead()) BattleManager._escapeType = 'lose';
    b.hide();
    //0 )
}

I'm not sure what exactly did you want to do, so this might not be fully correct either, but at least it will run. I definitely don't know what the zero at the end was for, so I've put it in a comment.

In general I'd definitely recommend doing some simple JavaScript tutorials to get a hang of how syntax work - that will definitely save you time in the long run.

EDIT: Just noticed that those were not normal brackets, so I also fixed that. The only type that will work is either ' or " (or `, but those are a bit special).
@Mac15001900 Thanks for this.
The code was all in one line because, as I mentioned, this is in the damage formula box which doesn't take line breaks.
The zero is there because it needs to end with a number.
You threw me for a while with the reference to non standard brackets, and then I realised that you meant quote marks. In the editor they are normal ' quote marks, but in copy/pasting it here they were changed to the other one which I can't type because I don't have it on my keyboard.

Unfortunately it still doesn't work. The message does not appear when the switch is ON, and when the skill runs the 'eject' bit doesn't happen. For the sake of clarity, without the part trying to implement the message, the formula works perfectly, as indeed one would expect with code provided by cathyril.
Ah, I see. I don't have combat in my game, so I'm not very familiar with those, but I think I see how it's supposed to work. I'm guessing it just runs eval on whatever you give it; that means the 0 would need to come after everything else, not inside brackets. In more technical terms, the last statement will be the damage dealt. Also, do you have some plugin that adds $gameMessage.push? Because that doesn't seem to be a thing in MZ - it should probably be $gameMessage.add (as you mentioned in your first post). So like this:
JavaScript:
if ($gameSwitches.value(167)===true) {
    $gameMessage.add('The enemy is too powerful for this skill');
} else {
    b.addState(1);
    if ($gameParty.isAllDead()) BattleManager._escapeType = 'lose';
    b.hide();
}
0
Or compressed to one line:
if ($gameSwitches.value(167)===true) { $gameMessage.add('The enemy is too powerful for this skill');} else { b.addState(1); if ($gameParty.isAllDead()) BattleManager._escapeType = 'lose'; b.hide(); } 0
@Mac15001900 That is definitely an improvement insofar as the message came up when the switch was on. Unfortunately it appeared after the skill had run and for the life of me I cannot see how that could be.

I think what I will have to do is to do the whole thing with a common event using a conditional with a Gab message if the switch is ON and a force action if it's OFF. Which I had wanted to avoid doing because it means I still don't have a simple, reliable way of getting messages incorporated into skills.

Thank you for taking the time and trouble to help with this query. It is appreciated.
Unfortunately it appeared after the skill had run and for the life of me I cannot see how that could be.
Do you mean the part inside else executed, or just that the skill ran and did nothing? Because the latter is unavoidable - this is just the damage formula after all, I'm guessing it's executed after the skill is used. You'd probably need a plugin if you want the damage formula to be able to cancel a skill.
@Mac15001900 My understanding of the logic of the damage formula is that if the first part (the IF statement) is true, then the ELSE part never runs. It's a true conditional. Therefore, if the skill is only in the Else, it should not run.

But it's been a long day. In my time zone it's when I need to stop if I'm to be fit for work in the morning.. I'll therefore have to leave this until tomorrow evening.
@Mac15001900 My understanding of the logic of the damage formula is that if the first part (the IF statement) is true, then the ELSE part never runs. It's a true conditional. Therefore, if the skill is only in the Else, it should not run.
You and Mac were talking about two different things. When you do the $gameMessage.add(), it's going to go into a queue for the next time a message window can be displayed, which is typically not in the middle of a skill.

The damage formula is not a good place for this kind of stuff to be done - it would be better done via VS skill notetags or an action sequence (which should be able to use the Show Text event command).
@Mac15001900 My understanding of the logic of the damage formula is that if the first part (the IF statement) is true, then the ELSE part never runs. It's a true conditional. Therefore, if the skill is only in the Else, it should not run.
As ATT_Turan mentioned, that's indeed not what I meant. I meant that whatever you put in that formula is only going to happen after the skill is used, no matter what that it. To achieve what I'm assuming you want to you will have to do it differently.

Original thread: https://forums.rpgmakerweb.com/threads/message-during-skill.154823/