Can I calculate variables within a conditional branch?

noah1773

Warper
Member
Joined
Mar 8, 2020
Messages
4
Reaction score
1
First Language
Korean
Primarily Uses
RMMV
Hi everyone. This is my first time posting and I'm just starting to learn how to use RPG Maker MV, so apologies if I'm asking a very basic stupid question or posting this on a wrong forum...

So what I'm trying to do is make an event that if you trigger, it drains your hp if you are close to the event. It's basically a monster that attacks you if you are near it, like in games such as Hollow Knight.

캡처.PNG

The blue slime at the center is the monster, and if the player is at the highlighted tiles they get attacked. If I say the monster's x,y coordinate's (X,Y), player gets attacked if the player's coordinate is either (X, Y+1), (X, Y-1), (X+1, Y), (X-1, Y).

So what I did is set variables to something like this:

캡처e.PNG

oookay this might be confusing because of the parts that aren't english (sorry) so let me explain
I made 8 variables that goes like this:
player x = player's x coord
player y = player's y coord
monster x = event's x coord
monster y = event's y coord
monster x+ = (monster x) +1
monster x- = (monster x) -1
monster y+ = (monster y) +1
monster y- = (monster y) -1

and then conditional branches, so if the player's coords is the same as the yellow highlighted tiles I mentioned before it turns self switch C on, which drains the actor's hp.

This works just perfectly, but it's a very tiring job since I have to set up 8 variables to each monster in every map.
So I was wondering if I could just use a script in conditional branch instead of setting up variables?
For example if I set up a conditional branch like
if player x = (monster x) +1
then I wouldn't have to make a variable monster x+, monster x-, and such.
I barely know anything about programming or scripts so this is a huddle for me.

Also I'm pretty sure there are people who've tried to make monsters like this but I couldn't google it out. If anyone know a plugin that can help it'd be great.
 

Attachments

Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,012
Reaction score
7,024
First Language
German
Primarily Uses
RMMV
no, what you should do is something else.

Instead of using eight variables and different ones for each monster, you should use two variables only, temp_x and temp_y.

Then you make one calculation on each, test by conditional branch and then to the next variation reset the temp variable to X and go into the next position check.

You only need to calculate the coordinates of one position, then check, then calculate the next position, check that and so on for the four positions.
That way you only need two variables and can re-use them for every monster in your entire game.
 

noah1773

Warper
Member
Joined
Mar 8, 2020
Messages
4
Reaction score
1
First Language
Korean
Primarily Uses
RMMV
Oh I forgot to add a very important fact; the monster is moving around, it's not stationary. I have to get new coordinates every time the player interacts with the monster so there are no fixed coord numbers.

I'm sorry but i can't understand what you mean by 'make one calculations on each conditonal branch'. How can I set up a conditional branch saying 'if player is at (x,y)' before I define what x is?

Maybe I'm totally misunderstanding what you're saying. Could you explain it in more detail? Sorry for bothering you :(
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
Here's a little snippet. Run it as a script command before you do any checks. You can run it wherever, whenever, as long as it's run at least once before you use it in a conditional branch. It initializes the function you're going to use:
JavaScript:
function prox(a, b, r) {
    if (r === undefined) r = 1;
    dix = Math.abs(a.x - b.x);
    diy = Math.abs(a.y - b.y);
    sum = Math.sqrt(Math.pow(dix, 2) + Math.pow(diy, 2));
    sum = Math.ceil(sum);
    if (sum <= r) {return true} else {return false};
}

Then to check if the player is in the highlighted tiles of the current event, you put this into the conditional branch:
JavaScript:
prox($gamePlayer, $gameMap.event(this.eventId()))

To check from another event, let's say between the player and event 11, do this:
JavaScript:
prox($gamePlayer, $gameMap.event(11))

Bonus: You can increase the size of the detection area, by entering a radius number after the entries:
JavaScript:
prox($gamePlayer, $gameMap.event(this.eventId()), 2)
JavaScript:
prox($gamePlayer, $gameMap.event(11), 5)

The default radius, if you don't specify one, is 1. That will look like the yellow squares on your picture :kaopride:
 

hinola

Veteran
Veteran
Joined
Jun 27, 2015
Messages
49
Reaction score
67
First Language
Français
Primarily Uses
RMVXA
The way I do that in ace (and it's almost the same on MV) is this one :

One common event
distance_fr_1.png
Here is a common event which when I call :
Look the player position
Look the avtual event (calling) position
Compare the X distance
Compare the Y distance
And use absolute of them both to get the final distance (this part will be different for MV, as it's javascript and not ruby)
It should be something like " Math.abs($gameVariables[1]); "
MV coders could tell you


And on the monster event :
distance_fr_2.png
I call the common event, I have now on variables :
1 : distance on X axis
2 : distance on Y axis
3 : distance (in tiles)
12 : (because I use this one on other systems) Id of the zone where the player is

The first condition detect if the player is close to the monster (1 tile) (and active self switch A)
The second condition move the monster closer to the player if this one is 4 tiles or less away.

You can also detect if the player is walking on a zone, or if it's only 4 tiles away on the left for exemple.

There is almost no script here, only to find the absolute (which is something that should be possible in the software with events commands ><, like the modulo, it's super usefull)
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
What's wrong with using scripts :LZSevil:
 

noah1773

Warper
Member
Joined
Mar 8, 2020
Messages
4
Reaction score
1
First Language
Korean
Primarily Uses
RMMV
Ok so I've tried out the script one first, thank you so much @ozubon ,
but when I tryout it just says 'ReferenceError: prox is not defined'
I pasted the script into a common event(parallel) and I made sure I pushed the button that triggers the common event before I interact with the monster... Why would it say it can't define the function?
 

ImaginaryVillain

Now A YouTube Cool Kid! =D
Veteran
Joined
Jun 22, 2019
Messages
421
Reaction score
1,526
First Language
Absurdism
Primarily Uses
RMMV
It turns out MV already has this function....
$gameMap.distance(x1, y1, x2, y2)

So if you just want to use the built-in "conditional branch" command to check if something is one space away from the player, just put...

JavaScript:
$gameMap.distance($gamePlayer.x, $gamePlayer.y, $gameMap.event(this._eventId).x, $gameMap.event(this._eventId).y) === 1;
..into the script area of the conditional branch.
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
Oh yes why of course! That's totally it, ImaginaryVillain!

Sorry Noah, I forgot you have to initialize the function before you're even on the same map as the event checking it. ImaginaryVillain's answer is easier.
 

hinola

Veteran
Veteran
Joined
Jun 27, 2015
Messages
49
Reaction score
67
First Language
Français
Primarily Uses
RMVXA
I gave an event solution because the person started with an event solution.

Scripts may be good, but doesn't teach how to use the soft.
Events are better, and easier to adapt after.

If you want to add new conditions, new ways to do the thing, you can easily change.
It's the purpose of RPG maker, make a game without programming
But sometimes, you have to add a lign of code.

If you want to make it in scripts, and if you want it to be understandable and still event-friendly, the best way I found is to make interpreter commands with readable names

function distance_between_events(A, B )
A and B are 2 ids, the function return the distance
You can set a variable with the script option, and then use the variable in conditionnal branch

You don't have to write a 5 kilometers long line of code that you don't understand.


EDIT

Code:
Game_Interpreter.prototype.distance_events = function(event_a, event_b) {
    event_1 = this.character(event_a);
    event_2 = this.character(event_b);
    if (event_1 && event_2)
    {
        return $gameMap.distance(event_1.x, event_1.y, event_2.x, event_2.y);
    }
    return -1;
};
I don't know where to put that, I just opened rpg_objects.js and wrote it in ^^'

distance_mv.png

-1 is used to select the player
This event test if the distance between the player and event 2 is = or < 4
 
Last edited:

ImaginaryVillain

Now A YouTube Cool Kid! =D
Veteran
Joined
Jun 22, 2019
Messages
421
Reaction score
1,526
First Language
Absurdism
Primarily Uses
RMMV
@hinola It's usually better to save that kind of code as a plugin instead of just directly editing the core files. This way the plugin only overwrites the code during runtime, and if the core files are ever updated you don't lose your changes. Not to mention if you end up using someone else's plugin down the road that relies on what you edited, you don't end up needing to undo the changes you made.

@noah1773 Glad I could help. :LZSexcite:
 

pasunna

Veteran
Veteran
Joined
Feb 3, 2019
Messages
485
Reaction score
190
First Language
thai
Primarily Uses
RMMV
I gave an event solution because the person started with an event solution.

Scripts may be good, but doesn't teach how to use the soft.
Events are better, and easier to adapt after.

If you want to add new conditions, new ways to do the thing, you can easily change.
It's the purpose of RPG maker, make a game without programming
But sometimes, you have to add a lign of code.

If you want to make it in scripts, and if you want it to be understandable and still event-friendly, the best way I found is to make interpreter commands with readable names

function distance_between_events(A, B )
A and B are 2 ids, the function return the distance
You can set a variable with the script option, and then use the variable in conditionnal branch

You don't have to write a 5 kilometers long line of code that you don't understand.


EDIT

Code:
Game_Interpreter.prototype.distance_events = function(event_a, event_b) {
    event_1 = this.character(event_a);
    event_2 = this.character(event_b);
    if (event_1 && event_2)
    {
        return $gameMap.distance(event_1.x, event_1.y, event_2.x, event_2.y);
    }
    return -1;
};
I don't know where to put that, I just opened rpg_objects.js and wrote it in ^^'

View attachment 139171

-1 is used to select the player
This event test if the distance between the player and event 2 is = or < 4
hi what the benefits of use game interpreter on this
instead of normal functions
I always use function only for these script call function
thank you
 

hinola

Veteran
Veteran
Joined
Jun 27, 2015
Messages
49
Reaction score
67
First Language
Français
Primarily Uses
RMVXA
@ImaginaryVillain
I know Imaginary, but it's just an addition, not an edit.
And I made it for the test, I don't code much on MV.
So I don't even know where to add this snippets (as I said in my post)
And making a pluggin for one new command is meh, better wait to have some more


@pasunna
Why commands
To me, the benefits are to use simply lines of codes to do things you can't (or hardly) in event
For the present situation, I still proposed the event solution first, and I thinks it's still the more malleable if you want te re-use it differently.

as exemple, for a system I made of jumps using the zone ids, I had to make the character jump in every direction 1, 2, or 3 tiles.
Witch a command directly in the Interpreter, I could recrate the jump, but with the ability to put variables as parameters (distance X and distance Y)
I then only needed 1 command to manage every jumps possibles

Why a command over direct scripts,
It's all about the way you make it, not really about the possibility of the game engine

distance_mv_2.png
Is long to write, hard to read, doesn't even enter the command box
And the test " ===1 " is on the end of the script line

If you want to make it for an other event in an other context, you always have to copy this line and change every occurences of ids if you don't use the actual event.

Why in the interpreter
And I wrote it in Interpreter just to make the command call smaller
And still keep it tidy in a place with other commands
I could have made it "at the root" of the scripts, just a function, but I personally don't like that
And that's purely personnel

In ace, I don't even need the "this." when I put commands in interpreter
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,012
Reaction score
7,024
First Language
German
Primarily Uses
RMMV
the monster is moving around, it's not stationary.
That is what I automatically assumed as you won't need variables otherwise.

here is a sequence like I described:
player x = player's x coord
player y = player's y coord
temp_x = event's x coord
temp_y = event's y coord
temp_x = (temp_x) +1
conditional branch player x = temp_x (and same for temp_y)
temp_x = event's x coord
temp_x = temp_x -1
conditional branch player x = temp x
temp_x = event's x position
temp_y = event's y position
temp_y = temp_y -1
conditional branch...

and so on for every position

you don't need to store the positions in variables for all eternity, you only need them for the next conitional branch and can then overwrite the old numbers.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

I forget what is bumping rule in this forum. Is anyone know? Thank you.
BCj
Ew, why does my blogpost on the rpgmaker.net site look like a garbled mess? Are html codes disabled or something?
I feel that I should cut down on my order queue so I can get some more time for my projects, its been quite a longggg while since I worked on them LOL
I friend of mine insisted on practicing dubbing things with him... and its quite funny, yet, it really takes a huge amount of practice!
Gotta save this for my next thread later...

Forum statistics

Threads
97,882
Messages
947,617
Members
129,116
Latest member
Lupom
Top