Random enemy events

The enemies in my game are represented by different events on the map, but at the moment they are all scripted and removed when you win them in the battle or re-enter the map. The enemy event itself looks like this in general, it's not parallel, movement scripted by YEP chase player plugin.
1649925345919.png


I want to make it the way, when there will be for example 10 enemy events in different places on the map. But when you enter it, only a random part of them will appear on the map. Thus, with one entry on the map there will be 7 enemy events, with the other only 2. These events should also be deleted when you defeat enemies or when you re-enter the map, that is, the point is that each time you enter the map, enemies will spawn in random places and numbers.

So far I have some results of my own, but I don't want to tell you right away because I just want to hear how you would do it. I think my results are a bit cumbersome. I used 10 individual switches and one common event.

I need a solution that uses as few common events, switches, and variables as possible. Maybe even scripts, if you can think of any. Thanks in advance.
the key to this are the two parts you didn't mention in your list and your screenshots:
event pages and self-switches (which are not the same as switches).

basically all your events need two event pages.

the first page has no conditions and is on parallel process.
first command uses a temporary variable to get a random number
second command is a conditional branch that checks this random number to determine if this specific event should be visible or not, and depending on the result either control self-switch A or erase event.

second page is what you have above, only conditioned to self-switch A.
and you need to control self-switch A off directly before the erase event.
the key to this are the two parts you didn't mention in your list and your screenshots:
event pages and self-switches (which are not the same as switches).

basically all your events need two event pages.

the first page has no conditions and is on parallel process.
first command uses a temporary variable to get a random number
second command is a conditional branch that checks this random number to determine if this specific event should be visible or not, and depending on the result either control self-switch A or erase event.

second page is what you have above, only conditioned to self-switch A.
and you need to control self-switch A off directly before the erase event.
Okay, I think it should look like this:

1649929075321.png


1649929223387.png


But here is the thing, the active event page is not parallel. So if it was activated once, it remains active until you will defeat the enemy. So that way if you re-enter the map a few times, the enemies will gather there. I also mentioned that I want all events to reset each time you enter location. I'm thinking about it myself, maybe it is possible to somehow swap pages?
OK, then you'll need to work with a reset-switch for the map.
you'll need one switch per map if it is to be done easily, although in some cases you can handle it by alternating switches on alternating maps.

and one additional comment: when I'm writing parallel process, you should use it instead of autorun.
because there is one additional difference: all parallel processes are handled before the first autorun is activated, causing autorun events to delay a bit on entering the map.

add a third page to all events, conditioned to a switch "map reset". This switch needs to be activated on the previous map before the transfer to this map, which also means the previous map can't have the same switch for resetting.

on that third page, set it to parallel process with the following commands:
1) control switch off for every switch that was set on for the event.
2) wait 2 frames
3) control switch map reset off

the wait is to allow every event to switch off their self-switches and whatever else before the reset switch (and thereby the reset-process) is disabled
OK, then you'll need to work with a reset-switch for the map.
you'll need one switch per map if it is to be done easily, although in some cases you can handle it by alternating switches on alternating maps.

and one additional comment: when I'm writing parallel process, you should use it instead of autorun.
because there is one additional difference: all parallel processes are handled before the first autorun is activated, causing autorun events to delay a bit on entering the map.

add a third page to all events, conditioned to a switch "map reset". This switch needs to be activated on the previous map before the transfer to this map, which also means the previous map can't have the same switch for resetting.

on that third page, set it to parallel process with the following commands:
1) control switch off for every switch that was set on for the event.
2) wait 2 frames
3) control switch map reset off

the wait is to allow every event to switch off their self-switches and whatever else before the reset switch (and thereby the reset-process) is disabled
I'd say that unfortunately one switch per map is pretty high price for me to pay for this task, because I have a lot of maps. But I still thank you for your time, and I can say that I have come up with a somewhat interesting solution.

I tried just swapping out the 2 pages above (and adjusting all the switches accordingly) and now it works so that when you first go into any map, there you see all the enemies at once and there are lots of them. But when you kill them, each event goes into an indefinite phase where enemies can either spawn or be left empty. It turns out that the player will have to clear the maps of enemies on his own to move around in peace, and then, as he goes back and forth through the location, it may be repopulated again, as the enemies will respawn with a certain chance. Simply put, just by changing the pages I turned a problem in this system into a whole new gameplay mechanic and I'm really glad I did, since I'll be able to develop this concept to some degree.

I understand that I set you certain limits and decided to do things differently, but I think you understand that sometimes we're not really sure what solution we want, but we end up with the one we need. Thank you for your help.🙂
Do you have all the yanfly plugins? Or only a few free ones?

If you own the whole library, then YEP_EventSpawner.js is the plugin that will allow you to spawn enemies on a random spot on the map.

Since you want to be more efficient with your switches, then the YEP_SelfSwVar.js plugin might help as well.
The Switch you were talking about could be reused for every Map in your Game.
Best would be one Event that is controlling the Monster Spawn when Map is Entered and than Erases itself until map is reentered.
@Andar's started solution way allows you easier copy paste later on but for around 10 Enemies on a Map
i would suggest something like this:
EDIT: This example could be used for every Map in the Game and the Switches and Variables are all reusable and stay the same on every Map.

Spoiler: Spawn Example
Code:
◆Comment:On/Off Switches for the 3 Enemies
◆Control Switches:#0001..0003 = OFF
◆Comment:Amount of Enemies spawning this Time
◆Control Variables:#0005 Var5 = Random 0..3
◆Loop
  ◆If:Var5 = 0
    ◆Break Loop
    ◆
  :End
  ◆Control Variables:#0006 Var6 = Random 1..3
  ◆If:Var6 = 1
    ◆If:Switch1 is OFF
      ◆Control Switches:#0001 Switch1 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆If:Var6 = 2
    ◆If:Switch2 is OFF
      ◆Control Switches:#0002 Switch2 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆If:Var6 = 3
    ◆If:Switch3 is OFF
      ◆Control Switches:#0003 Switch3 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆
:Repeat Above
◆Erase Event
Do you have all the yanfly plugins? Or only a few free ones?

If you own the whole library, then YEP_EventSpawner.js is the plugin that will allow you to spawn enemies on a random spot on the map.

Since you want to be more efficient with your switches, then the YEP_SelfSwVar.js plugin might help as well.

I do have the whole library. Checked the plugin and just wanted to ask, did you use is yourself? The best solution for my described problem I have already found as it is very simple and just suits me, but for example I have many nps, which are just standing, but for which I would like to set different random "standing points".

I've watched the video about the plugin very briefly, but I was wondering if it would be really easier to do that with this plugin than to just copy the nps-events to 3 different points and place them depending on the random value of variable?
The Switch you were talking about could be reused for every Map in your Game.
Best would be one Event that is controlling the Monster Spawn when Map is Entered and than Erases itself until map is reentered.
@Andar's started solution way allows you easier copy paste later on but for around 10 Enemies on a Map
i would suggest something like this:
EDIT: This example could be used for every Map in the Game and the Switches and Variables are all reusable and stay the same on every Map.

Spoiler: Spawn Example
Code:
◆Comment:On/Off Switches for the 3 Enemies
◆Control Switches:#0001..0003 = OFF
◆Comment:Amount of Enemies spawning this Time
◆Control Variables:#0005 Var5 = Random 0..3
◆Loop
  ◆If:Var5 = 0
    ◆Break Loop
    ◆
  :End
  ◆Control Variables:#0006 Var6 = Random 1..3
  ◆If:Var6 = 1
    ◆If:Switch1 is OFF
      ◆Control Switches:#0001 Switch1 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆If:Var6 = 2
    ◆If:Switch2 is OFF
      ◆Control Switches:#0002 Switch2 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆If:Var6 = 3
    ◆If:Switch3 is OFF
      ◆Control Switches:#0003 Switch3 = ON
      ◆Control Variables:#0005 Var5 -= 1
      ◆
    :End
    ◆
  :End
  ◆
:Repeat Above
◆Erase Event
I honestly admit that it took me a long time just to figure out how it worked. Probably because I would have done it something like this: (example for 2 enemies and without the "else" condition that would remove them if the variable was zero. Because the window is very small...)
Spoiler: Don't look...
1649945918453.png

Your option is elegant, I admit. But I like things simpler. Even if they are a bit longer. 😀

1649946193044.png


But as I said previously, the best solution exactly for me I already found, so yours will serve those who want to implement the system that I described originally. Thanks guys.
The spawner is most useful if you want the same event to show up on multiple locations on different maps. Very useful for random enemies and NPCs.
@redmedved2 i see if you have 10 Enemies than Variable2 determines
which shall spawn.
If Var2 = 0 than all 10 Enemies Spawn
If Var = 5 than Enemy 5 to 10 Spawn
If Variable = 10 than no Enemies Spawn
That is an interesting approach.

But is this realy random enough for you?
Its always the same Enemies spawning, one Time more, one Time less, but always the
same Positions, or atleast the Code reads like that to me. Maybe you did not recognice that while play test?
Or i missread your Eventcode or parts of it are missing.

shutterstock_277478681.jpg

Next Morning ready for Testplay and than this XD.
@redmedved2 i see if you have 10 Enemies than Variable2 determines
which shall spawn.
If Var2 = 0 than all 10 Enemies Spawn
If Var = 5 than Enemy 5 to 10 Spawn
If Variable = 10 than no Enemies Spawn
That is an interesting approach.
Not exactly. Variable 2 just determines for which opponent the dice are rolled in the current cycle.

So if variable 2 = 0 - The system determines whether the enemy 1 appears or not.
Then in the end of the circle variable 2 value +=1, so in the next loop dice will roll for the next enemy. So it just consistently decides which enemy event will appear on the map and which won't. Events locations are predetermined, yes. But if you imagine 10 enemies that each time appears in random order, almost none of the players will even think that they have specific spawn spots, at least it will create the illusion I think.

But now I just have to ask you, what does your code actually do? I thought it's doing the same, selects which switches to turn on and which do not.

The spawner is most useful if you want the same event to show up on multiple locations on different maps. Very useful for random enemies and NPCs.
Thank you. If it really designed for such purpouces, I will try it out.
But now I just have to ask you, what does your code actually do?
It trys to do what you asked for XD.
I want to make it the way, when there will be for example 10 enemy events in different places on the map. But when you enter it, only a random part of them will appear on the map. Thus, with one entry on the map there will be 7 enemy events, with the other only 2. ... ... the point is that each time you enter the map, enemies will spawn in random places and numbers.
Depending on which of the 10 Events gets activated the Player sees Enemys at different Locations.
Thatway it looks like different Enemies would appear on different Positions in different Numbers each Time the Map is entered.
If you need something else, you would need to explain in detail or Edit it yourself if possible.
Planning the Logic behind a Mechanic is a very important part and offers more work or bugs behind every corner 🙏. My English is not the best, so its possible that i missread the instruction Info. 😅
But now I just have to ask you, what does your code actually do?

It trys to do what you asked for XD.

To be honest... I don't see it either. I think you forgot the actual spawn. 😛

Original thread: https://forums.rpgmakerweb.com/threads/random-enemy-events.146741/