Having NPCs return home at night.

EDIT: I guess I could trigger the D switch for every single NPC with a schedule whenever the player opens a door or engages with a transition event...though I'm wondering if that's the best way to handle it.
That would probably be the optimal solution. However, keep in mind that doing something like this is definitely complex. There is no short way around.

To trigger D self switch on each event you have to add this to all your transfer events:
Code:
m=$game_map.map_id
non_npc_events = [event1, event2,etc]
$game_map.events.each_idex do |i|
  $game_self_switches[[m, i, 'D']] = true unless non_npc_events.include?(i)
end
Change event1, event2 and etc to be your non character events. You can add more as long as you separate them with a comma.
Was going to make a script call using Galv's script for every one but that looks much more organized and less tedious--thanks! Only thing I need to consider is making sure I always perform this step last...God forbid I add a new NPC to a large town and and need to add them to all the transfer events.
God forbid I add a new NPC to a large town and and need to add them to all the transfer events.
Not really. The array in the script calls is for non-npc events. If you add a new npc you don't have to do anything in particular. The script call automatically activates the switch for that event as well.
Recently got around to trying this out--when I tried the script you gave me, I got the error "undefined method each_idex".
That is quite strange, it is an existing method in ruby 1.9.2 (the one RGSS3 uses). Are you sure you called that method on $game_map.events and there is no typo in what you wrote? There are more ways to do it, but they are less idiomatic.
Code:
each.with_index do |e, i| # this is one
for i in 0..($game_map.events.length - 1) # this is another one
everything else in the loop should stay the same.
For the sake of testing, this is what I did--it always gave an error just froze.

Code:
m=$game_map.map_id
non_npc_events = [1, 2]
$game_map.events.each_idex do |i|
  $game_self_switches[[m, i, 'D']] = true unless non_npc_events.include?(i)
end

Though I ended up implementing the schedule differently, due to the current method having difficulties. Rather than managing the schedule in the NPCs themselves, I simply have a few events that manage their schedules for them. For now, I'm testing it on two NPCs, a mother and daughter, and it seems to be working out pretty well. Outside I have two schedule events.

The event first checks if the current hour matches their waking schedule or sleeping schedule. If it is their waking schedule, their D switch is flipped off and the self switch of the scheduler is set to A. If it is their sleeping hour, their D switch is set on (they disappear) and the self switch of the scheduler is set to B. When the scheduler is set to A, it waits for the hour of her sleeping schedule to come, at which point she will make her way home and disappear. Switch B is turned on and switch A is turned off. When the scheduler is set to B, it waits for the hour of her waking schedule to come, at which point her graphic will change to her normal graphic and she will make her way back to her usual spot before the D switch is flipped off.

Basically, if you transition outside, she will already be where she's supposed to be (inside or out). You won't actually notice the schedule unless you happen to be outside just as it shifts to her transition period. The daughter's is similar, except she wakes up an hour later and goes to sleep two hours earlier. She will walk outside, make a path to her hang out spot and either run around the house or play hopskotch with her little brother until it's time to go in.

There are also schedule events inside, but they do the reverse.

Your second method also gave an error, but the third one did work. I changed it up so it would turn off the A and B switches for all the schedule events, thus resetting them so the characters can be placed where they need to be each time the player enters a new area.

Code:
m=$game_map.map_id
schedule_events = [225, 226]
for i in 0..($game_map.events.length - 1)
  $game_self_switches[[m, i, 'A']] = false if schedule_events.include?(i)
  $game_self_switches[[m, i, 'B']] = false if schedule_events.include?(i)
end

It's going to take some busy work, but I'm confident now that I can get the schedules set up. Thought I'd share my method in case anyone was interested. I used Theo's Pathfinding and Heirukichi's DayNightCycle to pull it off.

That said, there is a potential minor issue I'm worried about and may have to consider...I'm wondering how this will handle distance. I.e. a shop keeper is supposed to go to bed at 8:00 PM and wake up at 8:00 AM to start shop. If I'm on the other end of town at 8:00 PM, don't enter any buildings, and never go to the shop keepers side until 10:00 AM, will he just walk inside for no reason, then walk back out?

Frankly I'm not too concerned. I really doubt the player is just going to stay in one place for an hour and then happen to stumble across this minor bug. Still, I suppose fixing it could be a little challenge for myself if I ever feel like tackling it.
That was my mistake, I am sorry. I treated events as arrays but they are actually part of a Hash. The method you should use is "each_key" and not "each_index". If you change that it works.

Code:
m=$game_map.map_id
schedule_events = [225, 226]
for i in 0..($game_map.events.length - 1)
$game_self_switches[[m, i, 'A']] = false if schedule_events.include?(i)
$game_self_switches[[m, i, 'B']] = false if schedule_events.include?(i)
end
This is terribly inefficient. As a matter of fact, THIS is what we were previously talking about and it should be avoided. The "include?" method has to scan each element in the array to see if the array contains the element. The way you wrote it calls that method twice for each iteration of the loop. While the overall complexity is still O(n²) you are checking twice something that you should only check once, thus effectively doubling the time required to perform that operation. That said, once you save the scheduled events in an array, what is the point of calling the "include?" method when you can simply check the array of scheduled events?

I used the other way around in my example because I assumed that the number of events you wanted to move was higher than the number of events who should not be moved and therefore storing them all in a single array would have been impossible. However, once you are storing them into a different array there is no need for that.
Code:
m=$game_map.map_id
schedule_events = [225, 226]
schedule_events.each do |i|
    $game_self_switches[[m, i, 'A']] = false
    $game_self_switches[[m, i, 'B']] = false
end
This is much more efficient. You simply scan the array of scheduled events. However, you have to initialize the array each time since "schedule_events" is a local variable. Doing that is time consuming and requires an initialization for EACH element of the array itself. It is fine if the array is short but when it becomes a long one there are better ways to achieve this.

Another optimal (linear) solution would be commenting each event that you want to move and checking the contents of that comment. That way there is no need to check if the event is part of an array; you can simply check the very first command in the event and see if you have to move it or not.
Code:
m=$game_map.map_id
$game_map.events.each_key do |i|
  if ($game_map.events[i].list[0].parameters[0] == "Move")
    do_whatever_you_have_to_do_here
  end
end
If you use this approach, you can simply add a comment to scheduled events that includes the "Move" string (without quotation marks).

This guarantees that you only check a total of four characters for each events - thus scanning an array of 4 elements - regardless of the number of events you want to move in a certain map. If you want to make it more efficient you can shorten the comment string as much as you want. Even a single character is fine if you want to speed up things. Do not forget that you are the one who writes those comments; as long as you remember what is the meaning of what you are writing in them, any length is fine.

Spoiler: You could even do it this way
Code:
m=$game_map.map_id
$game_map.events.each_key do |i|
  if ($game_map.events[i].list[0].parameters[0] == "+")
    do_whatever_you_have_to_do_here
  end
end
This means you simply add a comment saying "+" (without quotation marks) to each scheduled event. No need for long strings at all.

Both solutions have a complexity that is O(n) - and can thus be considered optimal solutions. For the first solution n is the length of the scheduled events array, for the second solution n is the number of events in your map. However, as I said, since you have to keep array initialization into account, the second solution will probably prove to be faster in the majority of situations. Mathematically speaking, the second solution is always faster as long as you have to move a number of events that is 50% or more of the total amount of events in your map. When the amount of scheduled events is smaller than that you have to try things out.

Spoiler: My personal tips
In my opinion you are using too many unnecessary switches. You could save them for things that really need them.

Now that I know that you are using my script I can say for sure that you do not need a switch to trigger the event movement at all. You can perform a check on the current clock phase for that and simply change a self switch to trigger movement. The direction of the movement is then obtained (once again) by checking the current clock phase.
Spoiler: Pseudo Code
Code:
For Each Event {

  Self Switch A -> ON
  Self Switch B -> OFF

  If (time_to_go_home) {
    Set_event_destination -> Home
  } # End If
  Else {
    Set_event_destination -> Original Event Position
  } # End Else

} # End For Each

For Each Event {

  If (Event out of screen) && (Destination out of screen) {
    Move To Destination
  } # End If

  If (Destination reached) {
    Self Switch B = time_to_go_home
  } # End If

  If (Self Switch A is ON) {
    Move one step toward destination
  } # End If

} # End For Each
As you can see, this algorithm automatically sets Self Switch B to be the same as "time_to_go_home", which is the result of your check on the current clock phase, upon reaching destination. The movement is triggered for each event and set to false if the event already reached its destination. This automatically skips movement for events that cannot be seen and whose destination is not something he player can see. Of course you can change the definition of "on screen" to be something "close to what the player can see". But doing it this way prevents unnecessary movement for events that would only be a waste of time.

This is the complete algorithm. I wrote it quite fast but as far as I can see I do not think it lacks anything. Of course, if you find it lacking let me know, but it should work quite well.
Oh! Thank you for pointing that out! I'm not familiar with Ruby and don't have much experience with trying to make code run more efficiently vs "just get it to work". ^^; The second method also allows me to more easily add in new schedules too without changing anything for the transition.

EDIT: I tried this but got an error "undefined method [] for nil"

Code:
m=$game_map.map_id
$game_map.events.each_key do |i|
  if ($game_map.events[i].list[0].parameters[0] == "Move")
    $game_self_switches[[m, i, 'A']] = false
    $game_self_switches[[m, i, 'B']] = false
  end
end

The first method worked--but there was an extra end I had to remove.

Code:
m=$game_map.map_id
schedule_events = [225, 226]
schedule_events.each do |i|
  $game_self_switches[[m, i, 'A']] = false
  $game_self_switches[[m, i, 'B']] = false
end
EDIT: I tried this but got an error "undefined method [] for nil"

Code:
m=$game_map.map_id
$game_map.events.each_key do |i|
  if ($game_map.events[i].list[0].parameters[0] == "Move")
    $game_self_switches[[m, i, 'A']] = false
    $game_self_switches[[m, i, 'B']] = false
  end
end
I tested it in a project where each event had at least one command in it. If that is not the case for you then you should change that to this:
Code:
m=$game_map.map_id
$game_map.events.each_key do |i|
  next if $game_map.events[i].list.nil?
  next if $game_map.events[i].list[0].parameters[0].nil?
  if ($game_map.events[i].list[0].parameters[0] == "Move")
    $game_self_switches[[m, i, 'A']] = false
    $game_self_switches[[m, i, 'B']] = false
  end
end
The new line skips the current iteration if there is nothing in the current event list. If scheduled events have a comment, only non-scheduled events might be skipped for that reason.

You were right, I put one extra "end" in that code. I modified my previous post to show the right code.

I'm not familiar with Ruby and don't have much experience with trying to make code run more efficiently vs "just get it to work". ^^
That is why I pointed that out. Unfortunately the "just get it to work" approach makes things quire nasty later on. Many people experience lag when things are done inefficiently. Just to give you an idea here are a few examples for you.
Spoiler: Examples
Spoiler: Calling the "include?" method on the schedule_events array
If you call the "include?" method on the schedule_events array while having to initialize each element, you perform a total of N + N*N operations. N here is your array length.

N(N+1) <- total number of iterations

If you try to calculate that in a map with 40 scheduled events, that means you have to perform 40 * 41 = 1640 iterations.
40 scheduled events -> 1640 iterations

Spoiler: Initializing elements of the array without calling the "include?" method
If you simply perform iterations on the schedule_array, without calling the "include?" method on it, but you still initialize each element of that said array, you perform N + N operations.

If we use the same sample map with 40 scheduled events, the number of iterations required is 40 + 40 = 80. As you can see, something that is a minor change in the code, results in a huge change in its efficiency.
40 scheduled events -> 80 iterations

Spoiler: Checking if the first element of the event command list is the "Move" comment
Using this approach means you have to perform a number of iterations equal to the comment length multiplied by the number of events: L*M.
In this case L is your comment length (in my example with the "Move" string it was 4) and M is the number of total events. As a matter of fact ruby checks if two strings/arrays have the same length before comparing them. This means that if you are using a different comment with more than 4 characters you only perform a single operation and no iteration on each element of the string is required.

As a matter of fact, the number of iterations is (M-N) + (N*L). If you manage to use a comment with a single character for your scheduled events L becomes 1 and you have to perform a total of M iterations.

In a map with 70 total events, regardless of how many events you are moving, you perform a total of 70 iterations. However, if more than half of the events in the map are not moving, the number of iterations required to do this is higher than the amount of iterations required using the previous approach (at least if we talk about this cycle).
70 total events (40 scheduled events) -> 70 iterations

Once you know these, picking the best one for your game is your choice. Keep in mind that this is just a mere calculation on how efficient your algorithm can be. It does not take into account how much work is required to edit events in your game. This is something that only those involved in the project can know, because it is based on how many times you have to modify those arrays later on (and I would recommend testing it on a single map and then creating those arrays once your game is already completed to reduce the workload).

Have fun picking the option that best fits in your situation.

Original thread: https://forums.rpgmakerweb.com/threads/having-npcs-return-home-at-night.107968/