- 11
- Posts
- 5
- Years
- Limoges, France
- Seen Mar 23, 2025
This tutorial has been made with Essentials v19.1. While the chances of the functions we'll be working with being modified with v20 or future versions are very low, I'll keep an eye on it. If I ever notice any modification, I'll try to get this tutorial up to date as soon as possible
I don't ask for any credits, because what I do is basically just taking an already existing function and creating new ones based on it.
I don't ask for any credits, because what I do is basically just taking an already existing function and creating new ones based on it.
Introduction :
What is a rotating NPC ? Basically, this term is associated with NPCs that stay on the same tile, but randomly change the direction they are facing. In the mainline games, this behaviour is pretty often adopted by trainers who can suddendly look at where the player is, and then engaging a battle.
This is exactly what I'm gonna teach you to do in this tutorial.
Getting started :
Spoiler:
The first thing we need to do is to open the Script Editor and look for the following function with Ctrl + Maj + F :
It should be located in Game_Character. There, we'll be focusing on two functions that can already be used to make an NPC rotate, but the one we just looked for will be the main focus of this tutorial.
The first function works like this => a random number is choosed between 1 and 0. If the result is 0, the NPC will turn right from the direction he's facing. If the result is 1, this NPC will turn left from the direction he's facing.
To give you a more understandable example, let's say we have an NPC who's facing down. We run the function 5 times, and the results are : 0 - 0 - 1 - 1 - 1 (which translates into right - right - left - left - left).
This NPC will then look in these directions : down (starting point) - left - up - left - down - right
The second function (the one we looked for in the first place, and the one that will be used as the base for the next steps) randomly makes an NPC look to any main direction (up - right - left - down), depending of a random number chosen between 0 and 3.
Now, what if you want your NPC to randomly face either up or down, but never right or left ?
Or what if another NPC should randomly face any direction, with the exception of the down direction ?
That's what we'll be looking for right away.
Code:
def turn_random
![[PokeCommunity.com] An easy way to create rotating NPCs [PokeCommunity.com] An easy way to create rotating NPCs](https://i.imgur.com/BqEk9Jq.png)
Before going any further, I'd like to make sure that you understand something. The basis of these script is the fact that a random number is chosen to know where the NPC will look next. When you do rand(x), it will actually choose a random number between 0 and x-1, and not between 1 and x. That's how it works in most (if not every) coding languages. That's why, on the second function, the possible choices are 0, 1, 2 and 3, instead of 1, 2, 3 and 4.
The first function works like this => a random number is choosed between 1 and 0. If the result is 0, the NPC will turn right from the direction he's facing. If the result is 1, this NPC will turn left from the direction he's facing.
To give you a more understandable example, let's say we have an NPC who's facing down. We run the function 5 times, and the results are : 0 - 0 - 1 - 1 - 1 (which translates into right - right - left - left - left).
This NPC will then look in these directions : down (starting point) - left - up - left - down - right
The second function (the one we looked for in the first place, and the one that will be used as the base for the next steps) randomly makes an NPC look to any main direction (up - right - left - down), depending of a random number chosen between 0 and 3.
Now, what if you want your NPC to randomly face either up or down, but never right or left ?
Or what if another NPC should randomly face any direction, with the exception of the down direction ?
That's what we'll be looking for right away.
Randomly facing 2 directions :
Spoiler:
For this part, we'll be creating a function that allows an NPC to randomly face up and down only, and another one that allows an NPC to randomly face right and left only.
A screenshot with these 2 new functions will be available at the end of the step
Let's take a look at the function we were looking for :
Now, copy the entire function and paste it just below. We need to change the name of this new function. What I like to do is def turn_random_DIR, where DIR are the direction the NPC is able to face. These directions can be :
- U for up
- R for right
- L for left
- D for down
For our first function, the possible directions are up and down, so we should name this function def turn_random_UD (this is my way of naming, you can use your own name if you want)
Now, since the NPC can only look at two directions, we should replace rand(4) with rand(2), and delete two of the four lines beginning with when.
The first "when" line remains unchanged, and the second one should become when 1 then turn_down.
The entire function should be
In the same way, the function that allows an NPC to randomly face either right of left should be
In the end, your Game_Character should look like this :
With than in mind, you should be able to also make functions that allow an NPC to face up or right (UR), up or left (UL), down or right (DR), and down or left (DL)
A screenshot with these 2 new functions will be available at the end of the step
Let's take a look at the function we were looking for :
Code:
def turn_random
case rand(4)
when 0 then turn_up
when 1 then turn_right
when 2 then turn_left
when 3 then turn_down
end
end
- U for up
- R for right
- L for left
- D for down
For our first function, the possible directions are up and down, so we should name this function def turn_random_UD (this is my way of naming, you can use your own name if you want)
Now, since the NPC can only look at two directions, we should replace rand(4) with rand(2), and delete two of the four lines beginning with when.
The first "when" line remains unchanged, and the second one should become when 1 then turn_down.
The entire function should be
Code:
def turn_random_UD
case rand(2)
when 0 then turn_up
when 1 then turn_down
end
end
Code:
def turn_random_RL
case rand(2)
when 0 then turn_right
when 1 then turn_left
end
end
Spoiler:
![[PokeCommunity.com] An easy way to create rotating NPCs [PokeCommunity.com] An easy way to create rotating NPCs](https://i.imgur.com/1Pfa6j3.png)
With than in mind, you should be able to also make functions that allow an NPC to face up or right (UR), up or left (UL), down or right (DR), and down or left (DL)
Randomly facing 3 directions :
Spoiler:
With the same logic as the previous step, we'll be creating a function that allows an NPC to face either up, left or down (ULD)
This function should be written like this :
As for your Game_Character, it now looks like :
And now, you should also be able to create def turn_random_URD, def turn_random_LDR and def turn_random_LUR
This function should be written like this :
Code:
def turn_random_ULD
case rand(3)
when 0 then turn_up
when 1 then turn_left
when 2 then turn_down
end
end
Spoiler:
![[PokeCommunity.com] An easy way to create rotating NPCs [PokeCommunity.com] An easy way to create rotating NPCs](https://i.imgur.com/3bYmQl3.png)
And now, you should also be able to create def turn_random_URD, def turn_random_LDR and def turn_random_LUR
Assigning a function to an NPC :
Spoiler:
To begin with, create a new event and choose a graphic of your choice (in the example below, I chose the policeman sprite)
Now, you have to adjust the Autonomous Movement's frequence. The higher the frequence, the more often your NPC will change direction or not. In my case, I picked a frequence of 5, but you can pick another number.
The last thing to do is to change the Autonomous Movement type to Custom, and set the move route. After that, select the Script option and write one of the functions you previously created. (Make sure that the Repeat Action option is also checked)
If you decided to create a function for each possible behaviour, you can write :
2 directions
- turn_random_UD
// (or DU)
- turn_random_RL
// (or LR)
- turn_random_UR
// (or RU)
- turn_random_UL
// (or LU)
- turn_random_DR
// (or RD)
- turn_random_DL
// (or LD)
3 directions
- turn_random_ULD
// (or LUD, or DUL, or DLU)
- turn_random_URD
// (or RUD, or DUR, or DRU)
- turn_random_LUR
// (or URL, or ULR, or RUL)
- turn_random_LDR
// (or DRL, or DLR, or RDL)
Now, you have to adjust the Autonomous Movement's frequence. The higher the frequence, the more often your NPC will change direction or not. In my case, I picked a frequence of 5, but you can pick another number.
Spoiler:
![[PokeCommunity.com] An easy way to create rotating NPCs [PokeCommunity.com] An easy way to create rotating NPCs](https://i.imgur.com/U3XGhg5.png)
The last thing to do is to change the Autonomous Movement type to Custom, and set the move route. After that, select the Script option and write one of the functions you previously created. (Make sure that the Repeat Action option is also checked)
Spoiler:
![[PokeCommunity.com] An easy way to create rotating NPCs [PokeCommunity.com] An easy way to create rotating NPCs](https://i.imgur.com/AUpclvR.png)
If you decided to create a function for each possible behaviour, you can write :
2 directions
- turn_random_UD
// (or DU)
- turn_random_RL
// (or LR)
- turn_random_UR
// (or RU)
- turn_random_UL
// (or LU)
- turn_random_DR
// (or RD)
- turn_random_DL
// (or LD)
3 directions
- turn_random_ULD
// (or LUD, or DUL, or DLU)
- turn_random_URD
// (or RUD, or DUR, or DRU)
- turn_random_LUR
// (or URL, or ULR, or RUL)
- turn_random_LDR
// (or DRL, or DLR, or RDL)
That's all for this tutorial! If something isn't clear, or if you want more explainations, please feel free to ask anything on this thread, and I'll try to respond as fast as possible.
Also, I'm sorry if you spotted some mistakes. As a resident of Baguette-Land, English is not my native language, and I can make some oopsies sometimes, but I try to be as understandable as possible.
Thank you for following this tutorial!
ChromusSama
Last edited: