Use script as a conditional derivation

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
Hello group, I am sorry if this does not go here.
I am having a problem, I want to represent Conditional branch math operations, but I don't want to use the event command, I need it inside script.
JavaScript:
if (origen_y < 0) {origen_y * (-1)};
Is this the correct way to write?
If "variable" <0, multiply "variable" * (- 1)
This is what I'm trying to do, but I don't know much about code.
RMMV Script Calls
I use the information here.
Conditional branch/ if (code) {stuff} plus {stuff}
But I don't know how to fill in "code" and "stuff"


Translated with google translator.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,660
Reaction score
13,270
First Language
English
Primarily Uses
RMMV
Close.

origen_y *= -1
 

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
Close.

origen_y *= -1
"Unexpected token ="

This is what appears, also something about left-hand appears. It wouldn't be working.
It means that I am writing something wrong.2020-07-10 (4).png
This is an example of what I want:
2020-07-10 (5).png
 

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
446
Reaction score
1,402
First Language
Indonesia
Primarily Uses
RMMV
$gameVariables.value(ID) is "read only".
If you want to change the value, it should be: $gameVariables.setValue(ID, NEW_VALUE)

So it will be:
if ($gameVariables.value(170) < 0) { $gameVariables.setValue(170, $gameVariables.value(170) * -1) }

----

Anyway, it seems your purpose is to not allow negative value.
In that case, you should have used Math.abs instead, as it returns "absolute" value:
$gameVariables.setValue(170, Math.abs($gameVariables.value(170)))

Thus you don't need "if" condition.

Even more, your event in the screenshot could be further shortened like this:
1594418169889.png
 
Last edited:

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
$gameVariables.value(ID) is "read only".
If you want to change the value, it should be: $gameVariables.setValue(ID, NEW_VALUE)

So it will be:
if ($gameVariables.value(170) < 0) { $gameVariables.setValue(170, $gameVariables.value(170) * -1) }

----

Anyway, it seems your purpose is to not allow negative value.
In that case, you should have used Math.abs instead, as it returns "absolute" value:
$gameVariables.setValue(170, Math.abs($gameVariables.value(170)))

Thus you don't need "if" condition.

Even more, your event in the screenshot could be further shortened like this:
View attachment 150825
Now I understand, of course it was a variable control, I was not seeing it well.
The script is more complex, that was only a summary, the objective that always has a positive number. It is for a pan sound scrip.
Thanks for helping me, I am just learning javascript, I know that these things are very basic, but it is still difficult for me to be aware of everything. You have helped me a lot.


JavaScript:
if (origen_y < 0) {Math.abs(origen_y)};
So with the first example, would it be this way?
I want it to be a positive number.
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,660
Reaction score
13,270
First Language
English
Primarily Uses
RMMV
You're using a mix of pure script, and Control Variables.

If you have a Variable that you have named origen_y, you don't actually refer to it as origen_y anywhere. You always refer to it using the variable ID.

You can do one of these:

Code:
If Variable 170 < 0
  Control Variables 170 *= -1
or without a conditional branch:
Code:
Control Variables 170 = Script: Math.abs($gameVariables.value(170))
or
Code:
Script: $gameVariables.setValue(170) = Math.abs($gameVariables.value(170));

If this is confusing, please put in what you think should be right, and then take a screenshot of that window, so we can see where and how you're trying to use it, and not just the syntax, because the syntax needs to be correct for where and how you're entering it.
 

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
You're using a mix of pure script, and Control Variables.

If you have a Variable that you have named origen_y, you don't actually refer to it as origen_y anywhere. You always refer to it using the variable ID.

You can do one of these:

Code:
If Variable 170 < 0
  Control Variables 170 *= -1
or without a conditional branch:
Code:
Control Variables 170 = Script: Math.abs($gameVariables.value(170))
or
Code:
Script: $gameVariables.setValue(170) = Math.abs($gameVariables.value(170));

If this is confusing, please put in what you think should be right, and then take a screenshot of that window, so we can see where and how you're trying to use it, and not just the syntax, because the syntax needs to be correct for where and how you're entering it.
JavaScript:
var VolumenMax   = 50;
var DistanciaMax = 15;
var falloff = VolumenMax / DistanciaMax;
var origen_x    = $gameMap.event(6).x;
var origen_y    = $gameMap.event(6).y;
var se = {"name": "Bell1", "pitch": 70};
var pan = $gamePlayer.x - origen_x; 
se.pan = (pan * -9);
if (origen_x < 0) {Math.abs(origen_x)};
var pow_x = (origen_x - $gamePlayer.x) * (origen_x - $gamePlayer.x);
var pow_y = (origen_y - $gamePlayer.y) * (origen_y - $gamePlayer.y);
var realDistance = Math.sqrt(pow_x + pow_y);
se.volume = VolumenMax - falloff * realDistance, 0 ;
AudioManager.playSe(se);
It is a panning sound script.
I use a variable and I needed it to be positive at some point.
I did the tests and it worked fine.
Creo que todo ya está resuelto.
JavaScript:
if (origen_x < 0) {Math.abs(origen_x)};
This was the part I didn't understand before.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,660
Reaction score
13,270
First Language
English
Primarily Uses
RMMV
Okay, you can leave out the if part and just make that line

origen_x = Math.abs(origen_x);

However, my original answer should have worked:

if (origen_x < 0) {origen_x *= -1};
 

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
I understand, and I'm sorry, maybe I wrote incorrectly, when I copied what you wrote, I got an error. But I am new to javascrip, maybe the error was mine.
Thanks for your help.
It is my first script command, thanks for your help.

Sorry, I don't know how to delete this message.
 

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
Okay, you can leave out the if part and just make that line

origen_x = Math.abs(origen_x);

However, my original answer should have worked:

if (origen_x < 0) {origen_x *= -1};
I understand, and I'm sorry, maybe I wrote incorrectly, when I copied what you wrote, I got an error. But I am new to javascrip, maybe the error was mine.
Thanks for your help.
It is my first script command, thanks for your help.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,660
Reaction score
13,270
First Language
English
Primarily Uses
RMMV
We can't delete messages. You could report the post and ask a mod to delete or hide it if you want.

Is the issue solved - is it all working for you now?
 

Arise501

Villager
Member
Joined
Oct 23, 2019
Messages
13
Reaction score
1
First Language
spanish
Primarily Uses
RMMV
If it works, turn the volume up and down, and panning works. Thank you very much.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

So I just spent the weekend...playing my own game. The one I released last year. Yep you heard that right.
Goal for MZ? None... I'm still curious how to finish my VXA project and how it looks like when it actually finished. Which will take at least another two years. Coming late again to the party.
Finding it amusing that people are posting support questions in new threads for an engine that isn't even released yet. Touch and Archeia are the only ones authorised to say anything, and there's only one active thread where they'll be answering questions.
I think my goal for MZ ... is to actually release a game and not have twenty technical demos and debugging projects. Let's see if I can manage that. ;o
I am starting to enjoy writing. I never thought I would enjoy it since my dyslexia was holding me back so much.
Thanks to the community here :)
For those interested chapter 2 and 3 are available to read here:

Forum statistics

Threads
100,652
Messages
978,096
Members
132,261
Latest member
DarthNinjus
Top