Post by GCN on Nov 7, 2007 15:53:49 GMT -5
Step 4: NPC Homegetter
Coding level [****-]
The system to get your NPCs to their homes and back (hence, homegetter). Unlike the previous tutorials, is this one more focused on the experienced coder in rm2k(3). Consequently, I will not explain much. I'll start with explaining roughly how the system works.
Every NPC will get two extra pages.
Page 1 will determine waking and sleeping time (times where NPC goes back home, or a designated spot).
Page 2 will manage the movements of the NPC.
Page 3 will be the normal NPC page. I'll leave that up to you.
In every map with NPCs using the system, there will be an 'NPC manager' event. A parallel map event that constantly checks and directs all NPCs that have been listed in the event. It will use a common call event that calculates everything an NPC has to do. It will put the command in a variable, and the NPC will read the variable and execute the command.
We will first make the NPC able to follow the commands. Create an NPC and give it 3 pages. On the 2nd page, insert a Fork. Get a new variable for the Fork, call it [hg]Actions. Fork [hg]Actions, same as 1. Inside the Fork: Move Event, This Event- step down, random step. The random step is to prevent the NPC from getting stuck in the map. Good, now do the same for all directions, 2 for left, 3 for right, 4 for up. Don't forget the random step. Finally, make an extra Fork [hg]Actions with 5, with a Move Event, this Event, and leave it blank. This will stop any Move Event before it that was still being executed by the NPC.
In order to get the NPC on the correct place, and let him disappear when he enters a building, we need more commands. We also want to get his location, so we can tell which way it has to go. Start a Fork: Fork [hg]Actions, same as 10. Inside this Fork, insert Change variable. Get a new variable called NPC X. Change NPC X set to This Event X position. Do another Change variable, now for NPC Y. Of course, use This Event Y position.
Next we make the 'goal' command. When you enter a map when it's right past the time the NPC is supposed to start to move to his spot, the NPC should be already at that spot. Below the Fork 'same as 10', insert another Fork: Fork [hg]Actions, same as 20. Inside this Fork, Insert the command Set Event Place. Choose This Event, and for coordinates use new variables, [hg]NPC Goal X and [hg]NPC Goal Y. This command is to place the NPC instantly on the spot.
An almost identical copy of the goal command is the teleport command. The goal command could probably be linked to the teleport command by setting the teleport coordinates equal to the goal coordinates, but this works as well. I prefer keeping things separated. Copy/Paste the Fork 'same as 20' below the existing one, and change it to 'same as 30'. Inside the Fork, change the Set Event Place coordinates to new variables, [hg]Teleport X and [hg]Teleport Y.
Page 2 is now done. Onto page 1.
Page 1 determines the actions and times of actions for the specific NPC. We will do a simple awake/asleep schedule. Before you start, make two new switches, '[hg]Teleport before' and '[hg]Remove after', and make a new variable '[hg]NPC Goal Place'. It may not all make sense now, but it will later on. On page 1, start a new Fork: Fork HOURS, more than [Insert waking time here, like 7 or something], Add Else Case. In this Fork, start another: Fork HOURS, less than [Going to sleep-time, must be more than waking time in this case], Add Else Case.
In this Fork, you can insert the NPC 'waking' behavior. Where does he start walking, where does he walk to and does or does not he disappear into a building? Define the starting place (if you want one) with Teleport X and Y, set '[hg]Teleport before' to ON if you want your teleport to happen. Set a goal to walk towards with NPC Goal X and Y and set '[hg]Remove after' if you want your NPC to disappear after he reached the goal (like, when it enters a building). And last but not least, tag these specifics with its own number defined in '[hg]NPC Goal Place' (simply put, change the variable). Every behavior you make needs its own number, so the NPC movement manager can quickly see where the NPC is and has done at any time. You don't need an original number for every behavior of every NPC, it's just for inside the one you are working with. The game will have to know at what point the NPC is, point 1, or point 235, or whatever point # you gave the specifics. Just make every unique specifics have its unique number.
In both Else Cases that now remain, you can input the NPC 'Going to sleep' specifics. These two cover the time between going to sleep and waking, and they should both contain the same (even the same value for [hg]NPC Goal Place, since it's the same behavior). Defining the specifics works the same as the waking specifics.
Since most NPCs will probably have this day/night rythm of roughly waking up between 0:00 and ?:?? and going to sleep between ?:?? and 23:59, you can make a simplified version of the day/night schedule. It will simply assume you are at your night specifics, and checks if this is wrong, then concludes it should be day specifics
EXTRA STEP: Simplified day/night schedule.
Coding level [**---]
Start a new Call Event, name it NPC day or night. In the event, insert Change Switch, use a new switch called 'NPC day or night?' and set it OFF (night). Change [hg]NPC Goal Place to 2. This will stand for night. After that, start a new Fork: Fork HOURS, more than variable, and for the variable use a new one called [ddn]Waking time. Inside this Fork, create another Fork: Fork HOURS less than variable, and use a new variable called [ddn]Sleeping time (so now, the forks mean 'between waking time and sleeping time'). Inside this Fork, Change [hg]NPC Goal Place to 1, and Change Switch 'NPC day or night?' to ON. This will stand for day.
Now in an NPC's day/night schedule, you should insert the following: Change [ddn]Waking time to your waking time of choice. Change [ddn]Sleeping time to your sleeping time of choice, just make sure it's more than your waking time. After that, Call Event: Common event 'NPC day or night' and start a Fork: Fork Switch 'NPC day or night?', set to ON, Add Else Case. Inside the Fork, set your daytime specifics, just like you did before. But this time you won't have to set [hg]NPC Goal Place and copy the night specifics. In the Else Case, set your night time specifics.
Now it's time for the most important event, the one that directs the NPCs. Go to Common Event in Database. Start a new Call event, name it NPC homegetter. This event will direct any NPC with its event number selected.
Get a new variable called [hg]NPC ID no. This variable will be used to communicate with the NPC that has its event number stored inside this variable. Get another new variable, [hg]Page no. . This variable will be used to define the page of the NPC that should be read. Start the event with a Change variable: Change [hg]Page no. to 1. Insert Call Event, Map Event (variable designation) and set the variables to [hg]NPC ID no. and [hg]Page no. Indeed, this means we're going to load page 1 first, the day/night schedule. After that, it should be specified where the NPC should start, what its goal should be, if he should still be on the map after that etc.
The next step is to get a place to store the NPC traveling process, the NPC Place. We'll be using the variables #5001 and up. Get a new variable called [hg]var no. Change [hg]var no. set to [hg]NPC ID no., then add 5000 to [hg]var no. . Now get a new variable called [hg]NPC Place. Change [hg]NPC Place set to variable #[[hg]var no.], where [hg]var no. is used as the variable number.
Since every event has its unique ID, every NPC will its own variable. NPC with ID 1 will get variable 5001, NPC with ID 34 will get variable 5034. You should get the idea. After the Change variable that put variable [5000+NPC no.]'s value in variable [hg]NPC Place, we add another Fork: Fork [hg]NPC Place, other than [hg]NPC Goal Place. This checks if the NPC has already reached his goal. After the Fork, Change all variables you used (except the 5000+ID no. one) back to 0. Not all variables necessarily have to be set back to 0, but it can be considered a good habit. Below this, Change Switches '[hg]Teleport before' and '[hg]Remove after' to OFF. When this call event is done with the current NPC, it has cleaned itself and is ready for the next NPC.
What we currently have for coding is still only suited for NPCs that don't need directions. In the following part, we're going to insert the coding required if the NPC is already supposed to be somewhere when you enter a map. In order to measure this, we're going to set all Place variables used by NPCs (the 5000+NPC ID ones) back to zero every time we enter a new map. Now we're going to make this Call Event place the NPCs directly on the place they're supposed to be when it measures that its Place variable is zero. Or, if it is supposed to disappear after reaching its goal, it should disappear.
Inside the Fork, start another Fork: Fork [hg]NPC Place, same as 0, Add Else Case. In the Fork, start with another Fork: Fork switch '[hg]Remove after' ON. We can't simply use erase event, since this event could return at a later time. Instead, we're going to throw the event outside the map. Inside the Fork, Change [hg]NPC Goal X set to -10, and do the same for [hg]NPC Goal Y. Outside (below) the Fork, Change [hg]Action set to 20 and Change [hg]Page no. set to 2. Use the same Call Event as you used on top of this event page (Call Event, Map Event etc.). When executed, it should either place the NPC on coordinates -10,-10 outside the map, or on its goal coordinates.
What follows, is the registration that the NPC has reached its goal and optionally, has been removed. However, we don't want this registration to happen when entering a new map, when the Place variables are being set back to zero. The chance of overlap is slim, but it's there. As a safety precaution, we're going to use a switch.
Start a new Fork with a new switch: Fork Switch 'NPC Manager', set to ON. Inside this Fork, Change Variable with number [[hg]var no.] (third option of first section) set to variable [hg]NPC Goal Place. This stores the place number the NPC is set at, in its own Place variable. Recall the first Fork in the event: next time, [hg]NPC Place will be the same as [hg]NPC Goal Place.
There, you now have the part for setting NPCs on the right place when entering a map. Now only rests the part that will get the NPC to walk at the times you set him to. I assume you do, but if you don't have time running within towns (like most towns in Zelda Oot), you can stop here if you want. If you want your NPCs to walk to their homes, continue.
In order to calculate what direction the NPC has to walk to, we must gather both the NPC's current coordinates and the goal coordinates. The goal coordinates are already gathered (by calling page 1 of the NPC event, see top of NPC homegetter). Now we will gather the NPC coordinates.
We continue in the Else Case of Fork 'NPC Place same as 0', add Else case. Change [hg]Action set to 10 and Change [hg]Page no. set to 2. Call Event, Map Event with these variables, just like you did while calling page 1. Good, now we have everything we need. Now we should check if the NPC needs to be teleported onto the map. Make a new Fork: Fork Switch '[hg]Teleport before' is ON. For this, we'll assume that a teleport is only necessary if the NPC is outside the map. In that case, NPC X and Y values are below zero. Make a new Fork within the current Fork: Fork [hg]NPC X, smaller than 0 (excluding 0). Inside, Change [hg]Action set to 30 and use the Call Event to call the NPC again. Now, the NPC should be teleported. This also means that the values of NPC X and Y are no longer correct. Thus, we will input the Teleport coordinate values into the NPC coordinate values. Change [hg]NPC X set to [hg]Teleport X and Change [hg]NPC Y set to [hg]Teleport Y.
Now, we're ready to start making the code for the NPC movement.
We will use the following principle: assume the NPC has reached its goal, and then start checking if the opposite is true. We will do so by setting a variable to zero, and if a movement has to be made, the movement number will be put into the variable. Afterwards, the variable will be checked if it's still the same as zero.
Continue below the Fork '[hg]Teleport before is ON'. Change [hg]Action to 0. Add a Fork: Fork [hg]NPC X, bigger than [hg]NPC Goal X, Add Else Case. Inside the Fork, Change variable [hg]Action to 2. In the Else Case of the Fork, Add another Fork: Fork [hg]NPC X, other than [hg]NPC Goal X. Inside this Fork, Change [hg]Action to 3. This means, if NPC X is bigger than Goal X, go left (2); if NPC X is smaller than Goal X, go right (3). This was the easiest part. We can't do the same for Y, it will cause the NPC to always walk the vertical part of the path first. Since that's very unnatural, we're going to do something different. If the NPC has to walk diagonally, we will randomly choose if it sets a step in the vertical direction, or the horizontal direction. This is enough to make the walking look much more natural.
Below the Fork '[hg]NPC X bigger than [hg]NPC Goal X', start a new Fork: Fork [hg]NPC Y, bigger than [hg]NPC Goal Y, Add Else Case. We're going to check if the variable [hg]Actions has been edited already. In the Fork, start another Fork: Fork [hg]Action, same as 0, Add Else Case. In this Fork, Change [hg]Action to 4. In the Else Case, start another Fork: Fork [hg]Action, same as 2. In this Fork, Change [hg]Action to a random number between 1 and 2. After than Change [hg]Action by multiplying by 2. The outcome will be either 2 or 4 (either left or up). Below the Fork, start another Fork: Fork [hg]Action, same as 3. Inside this Fork, Change [hg]Action to a random value betwen 3 and 4. The outcome will be either 3 or 4 (either right or up).
Now go to the Else Case of Fork '[hg]NPC Y bigger than [hg]NPC Goal Y'. In the Else Case, make a Fork: Fork [hg]NPC Y, other than [hg]NPC Goal Y. Frankly, we will do the same as we just did. In fact, just copy the whole Fork '[hg]Action same as 0' and copy it in the Fork '[hg]NPC Y other than [hg]NPC Goal Y'. But this time in the Fork '[hg]Action same as 0', Change [hg]Action to 1 instead of 4. In the Fork '[hg]Action same as 2', Only Change [hg]Action to random number between 1 and 2, and in the Fork '[hg]Action same as 3', Change [hg]Action to a random number between 1 and 2, multiply by 2 and subtract 1 (the outcome should be 1 or 3).
i6.photobucket.com/albums/y209/Nickyout/NPC8.png
[/img]
Good, this should calculate the directions the NPC should take. What is next, is the check if [hg]Actions is still zero.
Below the Fork '[hg]NPC Y bigger than [hg]NPC Goal Y', start a new Fork: Fork [hg]Action, other than 0, Add Else Case. In this Fork, we only have to run the NPC event page 2 once more. Page 2 should already be set correctly, so just copy the command Call Event from, for instance, the top of the event NPC homegetter, and paste it here. This will cause the NPC to set one step in the right direction towards his goal.
What is more interesting is the Else Case. Apparently, the NPC has reached its goal. This progress needs to be registered. Start by copying the Fork 'NPC Manager is ON' (with Change variable with variable number [[hg]var no.])on top of the event somewhere, and paste it in this Else Case. Below the Fork, we will check if the NPC has to be removed. Make a new Fork: Fork [hg]Remove after, set to ON, Add Else Case. In this Fork, Change NPC Goal X to -10 and Y to -10, and Change [hg]Action to 20. In the Else Case, just change [hg]Action to 5. This will cause the random step (remember) that would still be executed after the NPC reaching its goal to stop. Below the Fork, Call Event should be used one more time. In case of the Fork, the NPC will be erased. In case of the Else Case, the NPC will stop the extra random step.
That's it. You have created the event 'NPC homegetter'.
Now for the final steps. Go to the Common Event Pre-teleporting. After your screen transtion, Change Switch 'NPC Manager' OFF. After that, we're going to set the NPC Place variables back to zero. Change [hg]var no. to 5001. Input a Cycle. Within the cycle, Change Variable with the variable number [[hg]var no.] to 0. Input a Fork: Fork [hg]var no., above 5200 (where the 200 of 5200 is the biggest NPC ID number that you should ever imagine to have, change it to 5100 or 5341 if you want to). Inside this Fork, Break Cycle. Below the Fork, Change [hg]var no. by adding 1. That's it for this event.
Onto the Post-teleporting event. This one is easier. Before your screen transistion, Change Switch 'NPC Manager' back ON, and add a wait 0.1 sec. And that's all for this event.
The last step. In the map where you have NPC that make use of the day/night system, start a Parallel Process. Under Event Condition, mark a Switch slot, and use NPC Manager as the switch. In the process itself, you should do the following for every of those NPC's: Change NPC ID no. to that NPC's ID number and Call (Common) Event, NPC Homegetter. At the bottom of the parallel process, insert a Wait 0.1 sec.
Congratulations. Your NPC Homegetter system should now be fuctioning. With this, you have completely rounded up the tutorial, and ended up with a realistic day and night system. While testing, be sure to have the NPC Mananger switch ON.
Credit goes to someone at Ultima Island for the day and night system(Ultima Island Is a great site by the way)
Coding level [****-]
The system to get your NPCs to their homes and back (hence, homegetter). Unlike the previous tutorials, is this one more focused on the experienced coder in rm2k(3). Consequently, I will not explain much. I'll start with explaining roughly how the system works.
Every NPC will get two extra pages.
Page 1 will determine waking and sleeping time (times where NPC goes back home, or a designated spot).
Page 2 will manage the movements of the NPC.
Page 3 will be the normal NPC page. I'll leave that up to you.
In every map with NPCs using the system, there will be an 'NPC manager' event. A parallel map event that constantly checks and directs all NPCs that have been listed in the event. It will use a common call event that calculates everything an NPC has to do. It will put the command in a variable, and the NPC will read the variable and execute the command.
We will first make the NPC able to follow the commands. Create an NPC and give it 3 pages. On the 2nd page, insert a Fork. Get a new variable for the Fork, call it [hg]Actions. Fork [hg]Actions, same as 1. Inside the Fork: Move Event, This Event- step down, random step. The random step is to prevent the NPC from getting stuck in the map. Good, now do the same for all directions, 2 for left, 3 for right, 4 for up. Don't forget the random step. Finally, make an extra Fork [hg]Actions with 5, with a Move Event, this Event, and leave it blank. This will stop any Move Event before it that was still being executed by the NPC.
In order to get the NPC on the correct place, and let him disappear when he enters a building, we need more commands. We also want to get his location, so we can tell which way it has to go. Start a Fork: Fork [hg]Actions, same as 10. Inside this Fork, insert Change variable. Get a new variable called NPC X. Change NPC X set to This Event X position. Do another Change variable, now for NPC Y. Of course, use This Event Y position.
Next we make the 'goal' command. When you enter a map when it's right past the time the NPC is supposed to start to move to his spot, the NPC should be already at that spot. Below the Fork 'same as 10', insert another Fork: Fork [hg]Actions, same as 20. Inside this Fork, Insert the command Set Event Place. Choose This Event, and for coordinates use new variables, [hg]NPC Goal X and [hg]NPC Goal Y. This command is to place the NPC instantly on the spot.
An almost identical copy of the goal command is the teleport command. The goal command could probably be linked to the teleport command by setting the teleport coordinates equal to the goal coordinates, but this works as well. I prefer keeping things separated. Copy/Paste the Fork 'same as 20' below the existing one, and change it to 'same as 30'. Inside the Fork, change the Set Event Place coordinates to new variables, [hg]Teleport X and [hg]Teleport Y.
Page 2 is now done. Onto page 1.
Page 1 determines the actions and times of actions for the specific NPC. We will do a simple awake/asleep schedule. Before you start, make two new switches, '[hg]Teleport before' and '[hg]Remove after', and make a new variable '[hg]NPC Goal Place'. It may not all make sense now, but it will later on. On page 1, start a new Fork: Fork HOURS, more than [Insert waking time here, like 7 or something], Add Else Case. In this Fork, start another: Fork HOURS, less than [Going to sleep-time, must be more than waking time in this case], Add Else Case.
In this Fork, you can insert the NPC 'waking' behavior. Where does he start walking, where does he walk to and does or does not he disappear into a building? Define the starting place (if you want one) with Teleport X and Y, set '[hg]Teleport before' to ON if you want your teleport to happen. Set a goal to walk towards with NPC Goal X and Y and set '[hg]Remove after' if you want your NPC to disappear after he reached the goal (like, when it enters a building). And last but not least, tag these specifics with its own number defined in '[hg]NPC Goal Place' (simply put, change the variable). Every behavior you make needs its own number, so the NPC movement manager can quickly see where the NPC is and has done at any time. You don't need an original number for every behavior of every NPC, it's just for inside the one you are working with. The game will have to know at what point the NPC is, point 1, or point 235, or whatever point # you gave the specifics. Just make every unique specifics have its unique number.
In both Else Cases that now remain, you can input the NPC 'Going to sleep' specifics. These two cover the time between going to sleep and waking, and they should both contain the same (even the same value for [hg]NPC Goal Place, since it's the same behavior). Defining the specifics works the same as the waking specifics.
Since most NPCs will probably have this day/night rythm of roughly waking up between 0:00 and ?:?? and going to sleep between ?:?? and 23:59, you can make a simplified version of the day/night schedule. It will simply assume you are at your night specifics, and checks if this is wrong, then concludes it should be day specifics
EXTRA STEP: Simplified day/night schedule.
Coding level [**---]
Start a new Call Event, name it NPC day or night. In the event, insert Change Switch, use a new switch called 'NPC day or night?' and set it OFF (night). Change [hg]NPC Goal Place to 2. This will stand for night. After that, start a new Fork: Fork HOURS, more than variable, and for the variable use a new one called [ddn]Waking time. Inside this Fork, create another Fork: Fork HOURS less than variable, and use a new variable called [ddn]Sleeping time (so now, the forks mean 'between waking time and sleeping time'). Inside this Fork, Change [hg]NPC Goal Place to 1, and Change Switch 'NPC day or night?' to ON. This will stand for day.
Now in an NPC's day/night schedule, you should insert the following: Change [ddn]Waking time to your waking time of choice. Change [ddn]Sleeping time to your sleeping time of choice, just make sure it's more than your waking time. After that, Call Event: Common event 'NPC day or night' and start a Fork: Fork Switch 'NPC day or night?', set to ON, Add Else Case. Inside the Fork, set your daytime specifics, just like you did before. But this time you won't have to set [hg]NPC Goal Place and copy the night specifics. In the Else Case, set your night time specifics.
Now it's time for the most important event, the one that directs the NPCs. Go to Common Event in Database. Start a new Call event, name it NPC homegetter. This event will direct any NPC with its event number selected.
Get a new variable called [hg]NPC ID no. This variable will be used to communicate with the NPC that has its event number stored inside this variable. Get another new variable, [hg]Page no. . This variable will be used to define the page of the NPC that should be read. Start the event with a Change variable: Change [hg]Page no. to 1. Insert Call Event, Map Event (variable designation) and set the variables to [hg]NPC ID no. and [hg]Page no. Indeed, this means we're going to load page 1 first, the day/night schedule. After that, it should be specified where the NPC should start, what its goal should be, if he should still be on the map after that etc.
The next step is to get a place to store the NPC traveling process, the NPC Place. We'll be using the variables #5001 and up. Get a new variable called [hg]var no. Change [hg]var no. set to [hg]NPC ID no., then add 5000 to [hg]var no. . Now get a new variable called [hg]NPC Place. Change [hg]NPC Place set to variable #[[hg]var no.], where [hg]var no. is used as the variable number.
Since every event has its unique ID, every NPC will its own variable. NPC with ID 1 will get variable 5001, NPC with ID 34 will get variable 5034. You should get the idea. After the Change variable that put variable [5000+NPC no.]'s value in variable [hg]NPC Place, we add another Fork: Fork [hg]NPC Place, other than [hg]NPC Goal Place. This checks if the NPC has already reached his goal. After the Fork, Change all variables you used (except the 5000+ID no. one) back to 0. Not all variables necessarily have to be set back to 0, but it can be considered a good habit. Below this, Change Switches '[hg]Teleport before' and '[hg]Remove after' to OFF. When this call event is done with the current NPC, it has cleaned itself and is ready for the next NPC.
What we currently have for coding is still only suited for NPCs that don't need directions. In the following part, we're going to insert the coding required if the NPC is already supposed to be somewhere when you enter a map. In order to measure this, we're going to set all Place variables used by NPCs (the 5000+NPC ID ones) back to zero every time we enter a new map. Now we're going to make this Call Event place the NPCs directly on the place they're supposed to be when it measures that its Place variable is zero. Or, if it is supposed to disappear after reaching its goal, it should disappear.
Inside the Fork, start another Fork: Fork [hg]NPC Place, same as 0, Add Else Case. In the Fork, start with another Fork: Fork switch '[hg]Remove after' ON. We can't simply use erase event, since this event could return at a later time. Instead, we're going to throw the event outside the map. Inside the Fork, Change [hg]NPC Goal X set to -10, and do the same for [hg]NPC Goal Y. Outside (below) the Fork, Change [hg]Action set to 20 and Change [hg]Page no. set to 2. Use the same Call Event as you used on top of this event page (Call Event, Map Event etc.). When executed, it should either place the NPC on coordinates -10,-10 outside the map, or on its goal coordinates.
What follows, is the registration that the NPC has reached its goal and optionally, has been removed. However, we don't want this registration to happen when entering a new map, when the Place variables are being set back to zero. The chance of overlap is slim, but it's there. As a safety precaution, we're going to use a switch.
Start a new Fork with a new switch: Fork Switch 'NPC Manager', set to ON. Inside this Fork, Change Variable with number [[hg]var no.] (third option of first section) set to variable [hg]NPC Goal Place. This stores the place number the NPC is set at, in its own Place variable. Recall the first Fork in the event: next time, [hg]NPC Place will be the same as [hg]NPC Goal Place.
There, you now have the part for setting NPCs on the right place when entering a map. Now only rests the part that will get the NPC to walk at the times you set him to. I assume you do, but if you don't have time running within towns (like most towns in Zelda Oot), you can stop here if you want. If you want your NPCs to walk to their homes, continue.
In order to calculate what direction the NPC has to walk to, we must gather both the NPC's current coordinates and the goal coordinates. The goal coordinates are already gathered (by calling page 1 of the NPC event, see top of NPC homegetter). Now we will gather the NPC coordinates.
We continue in the Else Case of Fork 'NPC Place same as 0', add Else case. Change [hg]Action set to 10 and Change [hg]Page no. set to 2. Call Event, Map Event with these variables, just like you did while calling page 1. Good, now we have everything we need. Now we should check if the NPC needs to be teleported onto the map. Make a new Fork: Fork Switch '[hg]Teleport before' is ON. For this, we'll assume that a teleport is only necessary if the NPC is outside the map. In that case, NPC X and Y values are below zero. Make a new Fork within the current Fork: Fork [hg]NPC X, smaller than 0 (excluding 0). Inside, Change [hg]Action set to 30 and use the Call Event to call the NPC again. Now, the NPC should be teleported. This also means that the values of NPC X and Y are no longer correct. Thus, we will input the Teleport coordinate values into the NPC coordinate values. Change [hg]NPC X set to [hg]Teleport X and Change [hg]NPC Y set to [hg]Teleport Y.
Now, we're ready to start making the code for the NPC movement.
We will use the following principle: assume the NPC has reached its goal, and then start checking if the opposite is true. We will do so by setting a variable to zero, and if a movement has to be made, the movement number will be put into the variable. Afterwards, the variable will be checked if it's still the same as zero.
Continue below the Fork '[hg]Teleport before is ON'. Change [hg]Action to 0. Add a Fork: Fork [hg]NPC X, bigger than [hg]NPC Goal X, Add Else Case. Inside the Fork, Change variable [hg]Action to 2. In the Else Case of the Fork, Add another Fork: Fork [hg]NPC X, other than [hg]NPC Goal X. Inside this Fork, Change [hg]Action to 3. This means, if NPC X is bigger than Goal X, go left (2); if NPC X is smaller than Goal X, go right (3). This was the easiest part. We can't do the same for Y, it will cause the NPC to always walk the vertical part of the path first. Since that's very unnatural, we're going to do something different. If the NPC has to walk diagonally, we will randomly choose if it sets a step in the vertical direction, or the horizontal direction. This is enough to make the walking look much more natural.
Below the Fork '[hg]NPC X bigger than [hg]NPC Goal X', start a new Fork: Fork [hg]NPC Y, bigger than [hg]NPC Goal Y, Add Else Case. We're going to check if the variable [hg]Actions has been edited already. In the Fork, start another Fork: Fork [hg]Action, same as 0, Add Else Case. In this Fork, Change [hg]Action to 4. In the Else Case, start another Fork: Fork [hg]Action, same as 2. In this Fork, Change [hg]Action to a random number between 1 and 2. After than Change [hg]Action by multiplying by 2. The outcome will be either 2 or 4 (either left or up). Below the Fork, start another Fork: Fork [hg]Action, same as 3. Inside this Fork, Change [hg]Action to a random value betwen 3 and 4. The outcome will be either 3 or 4 (either right or up).
Now go to the Else Case of Fork '[hg]NPC Y bigger than [hg]NPC Goal Y'. In the Else Case, make a Fork: Fork [hg]NPC Y, other than [hg]NPC Goal Y. Frankly, we will do the same as we just did. In fact, just copy the whole Fork '[hg]Action same as 0' and copy it in the Fork '[hg]NPC Y other than [hg]NPC Goal Y'. But this time in the Fork '[hg]Action same as 0', Change [hg]Action to 1 instead of 4. In the Fork '[hg]Action same as 2', Only Change [hg]Action to random number between 1 and 2, and in the Fork '[hg]Action same as 3', Change [hg]Action to a random number between 1 and 2, multiply by 2 and subtract 1 (the outcome should be 1 or 3).
i6.photobucket.com/albums/y209/Nickyout/NPC8.png
[/img]
Good, this should calculate the directions the NPC should take. What is next, is the check if [hg]Actions is still zero.
Below the Fork '[hg]NPC Y bigger than [hg]NPC Goal Y', start a new Fork: Fork [hg]Action, other than 0, Add Else Case. In this Fork, we only have to run the NPC event page 2 once more. Page 2 should already be set correctly, so just copy the command Call Event from, for instance, the top of the event NPC homegetter, and paste it here. This will cause the NPC to set one step in the right direction towards his goal.
What is more interesting is the Else Case. Apparently, the NPC has reached its goal. This progress needs to be registered. Start by copying the Fork 'NPC Manager is ON' (with Change variable with variable number [[hg]var no.])on top of the event somewhere, and paste it in this Else Case. Below the Fork, we will check if the NPC has to be removed. Make a new Fork: Fork [hg]Remove after, set to ON, Add Else Case. In this Fork, Change NPC Goal X to -10 and Y to -10, and Change [hg]Action to 20. In the Else Case, just change [hg]Action to 5. This will cause the random step (remember) that would still be executed after the NPC reaching its goal to stop. Below the Fork, Call Event should be used one more time. In case of the Fork, the NPC will be erased. In case of the Else Case, the NPC will stop the extra random step.
That's it. You have created the event 'NPC homegetter'.
Now for the final steps. Go to the Common Event Pre-teleporting. After your screen transtion, Change Switch 'NPC Manager' OFF. After that, we're going to set the NPC Place variables back to zero. Change [hg]var no. to 5001. Input a Cycle. Within the cycle, Change Variable with the variable number [[hg]var no.] to 0. Input a Fork: Fork [hg]var no., above 5200 (where the 200 of 5200 is the biggest NPC ID number that you should ever imagine to have, change it to 5100 or 5341 if you want to). Inside this Fork, Break Cycle. Below the Fork, Change [hg]var no. by adding 1. That's it for this event.
Onto the Post-teleporting event. This one is easier. Before your screen transistion, Change Switch 'NPC Manager' back ON, and add a wait 0.1 sec. And that's all for this event.
The last step. In the map where you have NPC that make use of the day/night system, start a Parallel Process. Under Event Condition, mark a Switch slot, and use NPC Manager as the switch. In the process itself, you should do the following for every of those NPC's: Change NPC ID no. to that NPC's ID number and Call (Common) Event, NPC Homegetter. At the bottom of the parallel process, insert a Wait 0.1 sec.
Congratulations. Your NPC Homegetter system should now be fuctioning. With this, you have completely rounded up the tutorial, and ended up with a realistic day and night system. While testing, be sure to have the NPC Mananger switch ON.
Credit goes to someone at Ultima Island for the day and night system(Ultima Island Is a great site by the way)