Is it possible to use contents of a variable as a whole condition in a cond branch

Hey guys,

Id like to use a single variable to be all the conditions in a cond. branch. For example:

Code:
◆Control Variables:#0009 = "$gamePlayer.x >= 9"
◆If:Script:$gameVariables.value(9)
  ◆Play SE:Bell3 (30, 100, 0)
  ◆
:End

Currently when I do it like this, it just keeps passing - I suppose this is because the game things there isnt a real condition. But im wondering if its possible to get rpg maker to acknowledge the contents of the variable as a whole condition.
No, it keeps passing because you have a string as the content of the variable, and strings are not automatically eval'd. Try forcing an eval of the variable, that might work.
No, it keeps passing because you have a string as the content of the variable, and strings are not automatically eval'd. Try forcing an eval of the variable, that might work.
that did the trick, thanks!
To explain what was happening before, you were setting Variable #9 to a string value, so the variable literally contained the value:
"$gamePlayer.x >= 9" as a string.

Then, in the conditional branch, it was checking against that variable. Conditional Branches (otherwise known as If statements) in Javascript evaluate in a Boolean context (i.e. force the value into a true or false). All strings are "truthy" which means they are interpreted as true, except for empty strings which are "falsy" and are interpreted as false.

As you discovered, you can add an eval statement to evaluate the string. However, it's not usually recommended to use eval unless you have to, because there can be powerful side-effects.

A better way, in this example, is to remove the "" marks so it's not a string.

Code:
◆Control Variables:#0009 = $gamePlayer.x >= 9
◆If:Script:$gameVariables.value(9)
  ◆Play SE:Bell3 (30, 100, 0)
  ◆
:End

An even better way to do this would be directly check the position, without using a variable in-between.

Code:
◆If:Script:$gamePlayer.x >= 9
  ◆Play SE:Bell3 (30, 100, 0)
  ◆
:End

Edit:
You can also do it with pure eventing by using the Game Data section of the Control Variables command.
Code:
◆Control Variables:#0009 = Map X of Player
◆If: #0009 >= 9
  ◆Play SE:Bell3 (30, 100, 0)
  ◆
:End
@Aloe Guvner I actually used the code I put up solely as an example. the real code i was experimenting with was a piece of code that another person on this forum made for me:
Code:
◆Script:var yDist = Math.abs($gamePlayer.y - $gameMap.event(this._eventId).y);
:      :var minX = yDist === 0 ? 1 : 1 + (yDist - 1) * 2;
:      :var xDist = $gamePlayer.x - $gameMap.event(this._eventId).x;
:      :$gameVariables.setValue(16, xDist + '>=' + minX + '&&' + xDist + '<= 6')
:      :this.wait(3);
◆If:Script:$gameVariables.value(16)
  ◆Play SE:Bell3 (90, 100, 0)
  ◆
:End
After your response, i actually figured out you could also do it like this:
Code:
◆Script:var yDist = Math.abs($gamePlayer.y - $gameMap.event(this._eventId).y);
:      :var minX = yDist === 0 ? 1 : 1 + (yDist - 1) * 2;
:      :var xDist = $gamePlayer.x - $gameMap.event(this._eventId).x;
:      :$gameVariables.setValue(16, xDist >= minX && xDist <= 6)
:      :this.wait(3);
◆If:Script:$gameVariables.value(16)
  ◆Play SE:Bell3 (90, 100, 0)
  ◆
:End
Im honestly not sure why i thought i had to quote everything, but sometimes the most obvious solutions go right over your head. Also thanks to your explanation, I understand the logic behind how cond branches work better now! I can really appreciate how thorough you explain things, youd be an excellent teacher imo, if you arent already one that is.
There is a difference between doing it with "" and eval, and doing it directly like in your example above.

Assigning the value directly means that it is calculated and the result stored in the variable, then checked directly in the conditional branch.

Setting "" and having it eval'd in the conditional branch means that the variable contains the code and the result is calculated when the conditional branch with the eval is executed.

There is no difference if those two commands parts follow one after another, but if they are in different events or segments then eval will recalculate with current values, while a die t assignment will keep the result from when the assignment was done, even if the other values changed since then.
@Andar youre right, you mean with gamePlayer.x for example, where in eval it calculates the thing at that spot, but without "" its already calculated beforehand and gives the cond branch either a true or a 0, if i understand correctly.
Also what do you mean with ''die t assignment'', is that a typo?
I believe it was a typo and he meant to say "direct assignment".

gives the cond branch either a true or a 0, if i understand correctly

Almost. Whenever a comparison operator is used (equals, greater than, etc.) the result is always true or false.
In your longer example, the test is:
Code:
xDist >= minX && xDist <= 6
This statement, as a whole, is either true or false. Either the x distance is greater than/equal the minimum x and less than/equal to 6, or it isn't. So Variable #16 will have a value of true or false.
The conditional branch will execute if the result is true, and will execute any 'else' branch if the result is false (if there is an 'else' branch).

That statement also uses the && logical operator which is the logical 'and'.

BTW, it might make more sense to use a Game Switch and not a Game Variable for this example. Behind the scenes, it will work either way. But traditionally speaking, in RPG Maker a Switch is used for True/False and a Variable is used for numbers and math.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-use-contents-of-a-variable-as-a-whole-condition-in-a-cond-branch.94258/