Mobs wont reset how I want

Hello!
Thanks for reading, I hope this is posted in the right place, I'm new to both the forums and RPG VXA ( I do have experience with rpg2000 for playstation, but that was many moons ago. Many, many moons...)

I have a game with visible Mob encounters, and once they are defeated, they turn into a invisible uninteractable event with a 999 wait cooldown before they "respawn" becoming visible and available to interact/fight with again. This is all fine and how I want it to act.

My problem is, When I leave said area, I want the mob respawn timers to either A: Immediately reset the mob back to fight ready mode... OR B: allow the wait timers to continue counting down when im in another zone. Im sure there is an easy way (with or without extra script) to do this and im overcomplicating things, my brain just isn't coming up with the solution today...

Any help or suggestions is much appreciated!
Actually there's no easy way as events do not run when you are off the map. So the wait timer cannot run while you are in another zone by default.

The only way I know to do this is to do option A which will require you to reset the self switch for each and every event when you enter the map. I know there's script code for it but I don't know it off the top of my head, but if you put that code in a parallel process which runs when you enter the map it should reset all of the events when you enter the map. Just be sure to turn off the parallel process too else it will always reset your events!
You could just use Erase Event instead of turning on a self switch, and then the mobs will reset when you leave the map and come back. They will not reset while you're still on the map.

The only way to use a second event page and have the timer count down while you're on another map AND reset while you're on the current map, is a script that will allow you to set a timer for each event.
Since solutions for option A have already been mentioned in posts above mine I am going to give you a few hints for option B.

You can use a FIFO (First In First Out) approach. Each time an event disappears you can store Map ID, Event ID and Defeat Time into an array (a variable). Then you can have a parallel process event check if the first element of that array is ready to respawn. If it is you turn its self switch off then repeat the check until you find an event that does not need to respawn (see note in the spoiler below).

That means you have to set your event blank page to have "Self Switch A is ON" as triggering condition. Since your events respwan each 999 frames you can just update a variable each frame from 0 to 999 (I am going to explain this later, I know this makes 1000 frames), this will prevent any overflow and makes things much easier to handle.
Spoiler: All this can be achieved with two simple script calls
Code:
$game_variables[your_var] << [$game_map.map_id, @event_id, $game_variables[counter] - 1]
# your var is the variable ID for the one storing the list of events waiting to respawn
# counter is the variable ID for the one storing your frames count
# that -1 there ensures that everything works properly (999 frames and correct extraction time)

This is what you should put in your event, just copy/paste this in each event you want to use as a monster. Just do not forget to turn self switch A on after that!

Your common event (parallel process) should look like this:
Code:
$game_variables[counter] += 1
$game_variables[counter] = 0 if ($game_variables[counter] > 999)
unless $game_variables[your_var].empty?
  while ($game_variables[your_var][0][2] == $game_variables[counter])
    dat = $game_variables[your_var].slice!(0)
    $game_self_switches[[dat[0], dat[1], 'A']] = false
  end
end

I wrote this from mobile so I cannot check possible mistakes in the code but as long as you can identify them it should work.

NOTE: the while loop in the parallel process event is actually useless. It would be THEORETICALLY useful if it were possible to beat two enemies in the same frame. I hardly think that is possible but since I am not 100% sure about that I am leaving it there - just in case. If that is not the case feel free to remove the line containing "while" and the last one containing "end".
Thanks to all for the help and suggestions!

I think I'm getting ahead of myself with this, so I am going to side table this issue until I feel more comfortable in general. I was hoping there was an easy work around that I simply was missing, ( I was trying to use a switch activated when transferring out of an area to reset the mob event back to it's page 1, but evidently this does not work. I assume because it can't interrupt the wait timer, but I don't really know) but from what I'm reading, the only real way to achieve what I'm looking for will be with some sort of script/parallel process checking a bunch of variables. I'm simply not good enough yet at making parallel processes and have no clue about writing script per say XD I'm still a noob.

I am gonna check out the links you guys included for new users, at this point I need to become more fluent in basics like parallel processes and variables. Very much enjoying Rpgmaker again!! Glad it has not only survived but flourished!!
No, you could use a switch, but whether it works or not depends on what you do with it. If you want to go that way and need more help, post a screenshot of your monster event (all pages), and one where you turn the switch on and turn it off again.

Also give us an idea of how many of these monsters you might have on a map, and whether you're using the same switch to reset all of them.
I am gonna check out the links you guys included for new users, at this point I need to become more fluent in basics like parallel processes and variables.
In the meanwhile I am preparing a tutorial about this. Of course that will still require you to know the basics but I hope you can find it useful. I will ad a link as soon as it is finished - that will take a couple of days though.
In the meanwhile I am preparing a tutorial about this. Of course that will still require you to know the basics but I hope you can find it useful. I will ad a link as soon as it is finished - that will take a couple of days though.

That would be most awesome of you, as I'm sure I am not the first person to ask such a question. I look forward to perusing it when it's all said and done!



Thank you also to Shaz, I appreciate the help, but If it's possible to do with a switch, I want to attempt to figure it out on my own, I'm hoping I will learn what I am missing from the beginner tutorials. If I'm serious about this, I had better learn all the basics, so my first "learning" project is on hold for now till I complete the tutorials. If I'm still stuck in a month after I'm done the tuts, I'll certainly be back on the forums asking again, but this time with screenshots of my event pages.

Thanks again community!
allow the wait timers to continue counting down when im in another zone

*kinda* possible with a small plugin, if you make the event itself ping a general manager that'd store the event ID and it's cooldown time.
but I don't know how it would scale up, if you have many events being updated at one time.... it might lag a lot.

@Heirukichi 's idea, but not as a standalone loop. It should be tied to the map update routine: the map would always tick the array of queued events, and events would only "put themselves" into it, and let the loop itself take them out.
but I don't know how it would scale up, if you have many events being updated at one time.... it might lag a lot
Why would you want to update many events at the same time?
because you can't know which events are queued for update, on which map, at any given moment.
the player would just play, and send events to the queue for update, which is always going to be greater than 1.
@gstv87 It looks like you missed a very important key point in my post. If you do it using a FIFO approach you do know which event requires an update, in which map at any given moment. The answer is: the first element of the array. The whole FIFO approach is based on that.

Spoiler: Explanation
This solution only checks one element each time and eventually updates (shifts) array elements only when the said event requires an update. The complexity of this algorithm is T = O(1) when the time is not right and no event has to be updated and T = O(n) when one or more events have to be updated.

Truth be told you could reduce extraction time to T = O(1) but that means extracting the last element of the array, which requires shifting everything to the right when adding elements to the array - thus having a complexity of T = O(n). This is not the optimal solution because the number of times you have to extract elements is always less than or equal to the number of elements you add. So shifting the array each time you add a new element means shifting it a number of times that is greater than or equal to the number of times you would do it while extracting - that is because you can theoretically extract more than one element each frame.

Since I suggested a FIFO approach there is no need to check - and update - any element in the array. If the first one is not ready to be updated it means the others are not either. The number of operations performed is two each time:
  • update counter
  • check if first element stored time is equal to counter
Updating each and every element of the array each time is something extremely inefficient as it requires T = O(n) each frame. In the tutorial I made - currently waiting for approval - I explained these things in a much more detailed way. It basically shows how to achieve complex timed systems without using scripts while keeping efficiency in mind at the same time.

However please keep in mind that updating each element of an array each frame to do something like that is not a good solution no matter where you put it - may it be a common event or a method attached to the update method of Game_Map. It still requires unnecessary operations - potentially many of them.
what happens if there's one event queued for cooldown and the player sends another one to the line?
the second event will never update until the first one is clear.

the more the player interacts with events and sends them to respawn, the more time it takes for the whole array to finish respawning!
that's not how respawn works! if you have a 10 second delay for respawn, you have 10 seconds after the first event is sent for respawn, and 10 seconds after the second event is sent for respawn as well!
which means, the time ticks for all objects in the queue, not just the first one.
@gstv87 May I point out that you are assuming that my method - which is actually tested and works perfectly - does not work without even reading it carefully? If you did, you would have know that I never once talked about updating the timer on the first element of the array.

I suggest you go back and read my posts once again - more carefully this time around - because in this situation that FIFO approach really is the optimal solution and you will be surprised by how efficient it is.

There is a better implementation and that would be doing it in the same way but using a list instead of an array. Unfortunately even if ruby arrays have list-like methods they are still not implemented as lists so that would make my explanation much longer and boring.

That is the main reason why i decided to implement it using arrays instead of lists - which are actually a good compromise. As soon as the tutorial is approved I am going to ad a list implementation of this so that you can insert and extract elements with a complexity of T = O(1), but this thread is not the place to explain that.

My tutorial has been approved. You can find it here. It includes a way to do it using lists to get the optimal result and a way to create something more complex. I hope this can clear any misunderstanding on this matter.

Original thread: https://forums.rpgmakerweb.com/threads/mobs-wont-reset-how-i-want.107135/