MZ Scripting/Mechanic Implementation Help

Hello and good day. This is my first time ever using a forum medium so hopefully I classified this post in the right place.

I need help with a mechanic. I'd like to make it so that the player is vulnerable in the darkness. Here is a diagram:
1749252788824.png

Enemies are repelled by light completely and can never enter the light radius: this also applies to mobile light sources including torches and flashlights (I'm sure this will be far more complicated, if even doable in this engine. If light dies, enemy immediately advances on the player.

Where would I start with coding this? I would appreciate the help!

Edit: Forgot to add - when there is not a tangible enemy, as in the diagram, the darkness itself will kill the player after a set amount of time passes without igniting another light source (Think kind of like Dont Starve.)
Does it work if you put all the tiles in your light sources to an array, like the coordinates. Get the pairs by calculating the area, like if it's a bonfire, use its coordinates and light radius and if it's a flashlight use player coordinates and direction and the length and width of the light. Then in your enemy move routes check stuff like the tile in front of the enemy, if it's in the light-tile coordinates array then make the enemy move backwards. Otherwise the enemy can move randomly, or if it's close to the player, move to the player.
Does it work if you put all the tiles in your light sources to an array, like the coordinates. Get the pairs by calculating the area, like if it's a bonfire, use its coordinates and light radius and if it's a flashlight use player coordinates and direction and the length and width of the light. Then in your enemy move routes check stuff like the tile in front of the enemy, if it's in the light-tile coordinates array then make the enemy move backwards. Otherwise the enemy can move randomly, or if it's close to the player, move to the player.
I have no clue where to even start with what you mentioned here. I've been working with RPGMAKERMZ for about two years now but I am still very much lost within the engine on a lot of things. I would need things illustrated for me to understand -- i cannot picture this concept in my head.

I am using Visustella Lighting plugin for the actual lighting graphics and figured maybe I could finagle something, but I don't believe anything in the presets could be tied to something to work a mechanic like im aiming for.
This is gonna require some scripting because it's a many to many relationship where both sides are variables. That is, you have many enemies and many sources of light and you need to be checking the distance between each one of them every frame or every few frames. It also may lag the game if you have a lot of both.

Generally you'd have an array or a script that returns an array like $gameMap.events() and use a "for each" statement or a loop to go through each element one by one, use a script to calculate distance between the current element (itself) and every light event position you want to check (the data for these is also probably in another array, e.g. a filtered $gameMap.events() or some custom array you declared)
This is gonna require some scripting because it's a many to many relationship where both sides are variables. That is, you have many enemies and many sources of light and you need to be checking the distance between each one of them every frame or every few frames. It also may lag the game if you have a lot of both.

Generally you'd have an array or a script that returns an array like $gameMap.events() and use a "for each" statement or a loop to go through each element one by one, use a script to calculate distance between the current element (itself) and every light event position you want to check (the data for these is also probably in another array, e.g. a filtered $gameMap.events() or some custom array you declared)
I did not plan to have more than one enemy pursuing the player at once.
I did not plan to have more than one enemy pursuing the player at once.
Ok, great, the logic is the same except instead of a nested for loop you just have one for loop.
Ok, great, the logic is the same except instead of a nested for loop you just have one for loop.
My friend, I have absolutely no idea what that means. I am no coder; still learning. 🙁
I think I can see a way for this to work:

Let's say we have the bonfire. We'll give it three stages: Strong, Weak, and Out. To symbolize this, we'll make an event with three pages: one for the strong fire, one for the weak fire, and one for the extinguished fire. We'll assign them to a variable: Time. So, if Time is 0, the fire is Strong. If Time is 50, the fire is Weak, and if Time is 100, the fire is Out.

When the player enters the map, we'll run a parallel event that will Wait for a certain time, then increase Time by 1. For example: Wait 60; Time += 1. Then, when the player activates the bonfire (perhaps with a conditional branch that checks for Fuel), it sets Time back to 0.

Next, we will put two regions around the fire. Region 2 will be the "safe" space when the fire is Weak, and Region 1 will be the "safe" space when the fire is Strong.

Now, we make the monsters. We make a parallel event that has a priority above the player. Then we get the Region ID for the event (using Get Location info). We put this into a variable "Enemy Region". Then, we add the conditional branch "If: Fire Timer < 49 Else" (the behavior when the fire is strong). This will set the enemy AI when the fire is Strong. Under the first branch, we and another conditional branch "If: Enemy Region > 1 Else". Under the first branch of this statement, we add "Set Movement Route: This Event *1 step backwards", and under the second branch, we add the enemy AI (at its most basic, "Set Movement Route: This Event *Move at Random"). At the end of each of these, we check the variable "This Event: Map X" and "This Event Map Y". You can then repeat it for "If: Fire Timer < 99 Else" (the behavior when the fire is weak), and the final else statement (the behavior when the fire is Out). At the end, we check if This Event Map X and This Event Map Y match the player's Map X and Map Y. If they do, then it's game over (or whatever you want the consequence to be). If you're having trouble visualizing it, here's a picture:
1749453708090.png

1749453733542.png

1749453768722.png

1749453826071.png

1749453848373.png


Try it, and see if it works for your project.

EDIT *Missed a line. The final "If Enemy AI >= 1" should have a line setting "Control Variable: Enemy AI == Random 0...2" before it. Otherwise, the enemy will only choose one action (move toward player or move at random) rather than sometimes choosing the one, and sometimes choosing the other.
*Missed a line. The final "If Enemy AI >= 1" should have a line setting "Control Variable: Enemy AI == Random 0...2" before it. Otherwise, the enemy will only choose one action (move toward player or move at random) rather than sometimes choosing the one, and sometimes choosing the other.
Ahh, God bless you! This makes sense to me and is something I was conceptualizing but wasn't sure how to implement either! Thank you for the visuals as well, it helps me break it down. I think I will use this method -- Thanks, StultusTemporis!!
No problem! Good luck!

Original thread: https://forums.rpgmakerweb.com/threads/scripting-mechanic-implementation-help.178149/