Lesson Goal: Learn how to build a common event that can be used to decide if the player is in or out of range.
Trying to think about how to do this, for most people thier head would just spin. "How on earth can you test out range?" well
chin up because it really is quite easy! For this lesson I will show you how to make a common event that will test to see
if the player is in melee range of the monster. First off, we'll need to make an event on the map, and name the event "Monster".
Now choose what ever graphic you would like for this bad guy. Once done, hit ok because we do not need to do anything to that
event. Lets open up our database and go to our good ol' common event tab. Find a blank one, and lets name it "Player/Monster X/Y".
This common event will only consist of 4 things, all we are gonna do is set 4 variables. double click in the common event box,
and go to Variable Control. First common event we're going to name "X Range". We'll set this variable equal to Player X (we will set the values to Player X, Player Y, Monster X, and MonsterY later in the tutorial, so just name them for now and we'll use them later)
2nd variable we'll name "Y Range" and we will set it equal to the Player's Y. Now below these 2 variable operations, we will
do two more. Now, take the X Range variable and MINUS the Monster X and the 4th operation, we will take Y Range variable and
minus the Monster Y. Now comes the fun part! Make a new common event and let's name it "In or out of range". This condition
branch will be used to check if the user is in or out of range and will give a message according to where the player is standing.
Our goal is to have the "in range" be at the sides of the monster and out of range be everywhere else. Our first condition branch
will be set to "If X Range = 0" (make sure you have the else handle unchecked), now inside this condition branch we are going to make
another condition branch set to "If Y Range = 1" and inside this condition branch we'll add a text message saying "You are in melee
range!" What we just did was make it so if the player is below the monster event, the game can test the range and it knows they are
close enough. Now below what we just did, we'll do the same thing, but this time the X Range condition will be -1 and Y Range
condition will be 0. This makes it so if the player is standing to the left they are in melee range. (don't forget to add a message
alerting the player they are in range!) Below that one we'll do the same thing again, but X Range condition will be 0 and Y Range
condition will be -1. This will test to see if the player is just above the monster. Our last one will be if X Range condition is
1 and Y Range condition is 0. This is the area just on the right of the monster.
Now we need to make a condition branch that will test if the user is out of range. This will be the time we will check the "else"
box, as if we don't we will get multiple messages saying "You are out of range." So for the first condition branch we will have
the condition set to "If X Range <= -2" in this condition we'll add the messages "You are out of range!" Now in the else part of
this condition branch we're going to make another condition branch. The condition will be set to "If X Range >= 2" and make sure
you check the 'else' box. Just like before, in this condition branch we'll add "You are out of range!" and in the else area we're
going to make another condition branch. Condition will be "If Y Range <= -2", like before, give them the message they are out of
range, and finally, in the else area we're going to make the last condition branch for this part (for this one you can leave the
else part unchecked) and set the condition to "If Y Range >= 2" (sorry if I sound like a broken record) and make sure you add
the "out of range!" message. Phew, hard part is done, but there are still 4 more condition branchs we'll need to do. The ones
we just did will give us a message if we are standing next to the monster or a square away, but what about if we are right next
to the monster but just in the upper right box? The as of now the player will not get any message if they are standing on the corner
boxs of the monster. We'll have to make a condition branch for each corner.
This will be easy, for the first condition branch set the condition to "If X Range = 1" and in that condition branch make another
one "If Y Range = 1" and give the message "You are out of range!". This covers the lower right box now for the other 3. Make
another condition branch with "If X Range = 1" and then a condition branch "If Y Range = -1". This covers the upper right box.
For our 3rd one, we'll have "If X Range = -1" then "If Y Range = 1". This will cover the lower left box, and finally, we'll
do our last one and we'll have "If X Range = -1" then "If Y Range = -1" and this will cover the upper left box. Now all that is
left is to actually use these common events! Go into your game, and on the map make tiles that are surrounding the monster that
are player touch. For the event they should look something like this:
This is where we set the variable numbers for Player X, Player Y, Monster X, Monster Y. We do this on the event we step on because we want up to date info on where the player is vs where the monster is. (Though this can lead to lag if not used carefully. I will explain in a later lesson on how to make a enemy/player range event that can keep you lag free) You can set these variables like so:
And then your test map should look something like this:
Now start your game and test it out! If you did everything like you should, then everything should work out just fine. Congrats
you finished Lesson #3.