How does RMMZ calculates Variables?

I've set a variable to be random between 1...100 to set rarity for a spawn. RMMZ seems to like to roll above 95 a lot.

EDIT: when the game generates the variable with the parallel process it's always 95 or more, when I trigger it with a normal event, it behaves normally.

This is the event that is always triggering a 95 or more.

1612240866792.png


and this is the one that's behaving normally

1612240931892.png
1612240886883.png


It's unlikely there's any problem with the RNG, but there may be something off on your parallel process (like multiple things bein executed or some other code increasing the variable value somewhere else).
We'll need to see the event commands to be able to say anything.
View attachment 177997

It's unlikely there's any problem with the RNG, but there may be something off on your parallel process (like multiple things bein executed or some other code increasing the variable value somewhere else).
We'll need to see the event commands to be able to say anything.
I've just edited the post with the event 🙂 I'll paste it here completely just to be sure.

1612241105333.png

1612241119658.png


The spawn is working fine, just the way I've set the "rarity" that is kinda weird.
If this is in a parallel process, then the problem is not that it's generating >= 95 too often, but that the code is running too many times.

A parallel event is executed 60 times per second, so 60 times per second you have one or two 5% chances of adding a critter, that would generate at least one critter per second for sure. If my sleepy math is correct that's like 4.5 critters per second.
Well... there's a switch to turn it off. Is it in the wrong place? It was suppose to generate between 1 and 2 critters IF the rarity is rolled 95 or more.

EDIT: In the other map that I use the a similar event with the same variables it works properly, although the rarity there indicate the kind of critter that will spawn no only if it will spawn.



1612242257596.png
1612242286042.png

1612242302598.png
The switch is being turned on at the end, is that right?
@Hudell Yes.

I added a text command to show me the number that the variable had generated in the event so it would make testing easier. When I added it stopped generation 95 or higher only. But only if I have this text after the control variable .-.

1612243643575.png
That's probably because it's stopping each time to show you the text and waiting for you to close the window, so it's no longer running every 1/60th of a second.

Normally, a switch triggered parallel process is going to run so long as the switch it's connected to is on. The process you've got turns the switch on, so it isn't doing anything. If you want to stop the process you've got to turn the switch off.
That's probably because it's stopping each time to show you the text, so it's no longer running every 1/60th of a second.

Normally, a switch triggered parallel process is going to run so long as the switch it's connected to is on. The process you've got turns the switch on, so it isn't doing anything. If you want to stop the process you've got to turn the switch off.
That's true for common events running in parallel. But that's a map event. The switch is there o stop the event triggering it to go to the next page which doesn't contain anything
I'm just curious what exactly you are trying to accomplish that you have it in a parallel process? Is this on all maps, only one map? How often are you wanting it to try to spawn a creature?
First, you should probably clean up the code:
  • You can move the "critter quantity -=1; if critter quantity <= 0 then break" logic out of the conditionals, just do it once right before the end of the loop;
  • It helps to disable the "Else" clauses on conditionals that do not use them too;
  • Also, the "Spawn rarity >= 0" does not serve any purpose, since the spawn rarity is something between 1 and 100 anyway (it's always >= 0! There's no branching!).
Other than that, honestly, I have no idea what the problem is. Instead of showing a message box, try running this script:
JavaScript:
console.log($gameVariables.value(1));

This way you'll see which values the variable takes without interrupting the event, which is not nearly as disruptive and is not expected to change the behavior of the system.
I'm just curious what exactly you are trying to accomplish that you have it in a parallel process? Is this on all maps, only one map? How often are you wanting it to try to spawn a creature?
technically is not a creature, it's flowers, but that's besides the point hehe.

It supposed to spawn as the player enter the map. The idea is every time the player enters the map the creatures are spawned in different areas with different chances.

First, you should probably clean up the code:
  • You can move the "critter quantity -=1; if critter quantity <= 0 then break" logic out of the conditionals, just do it once right before the end of the loop;
  • It helps to disable the "Else" clauses on conditionals that do not use them too;
  • Also, the "Spawn rarity >= 0" does not serve any purpose, since the spawn rarity is something between 1 and 100 anyway (it's always >= 0! There's no branching!).
Other than that, honestly, I have no idea what the problem is. Instead of showing a message box, try running this script:
JavaScript:
console.log($gameVariables.value(1));

This way you'll see which values the variable takes without interrupting the event, which is not nearly as disruptive and is not expected to change the behavior of the system.

I'll try do make the cod again, although I've tried a lot before. It is just "messy" because I was trying to fix it, I added the elses, and the other things to see if it would help.
I have no idea how to use a script to do that, so I don't think I can :/ For me to check the variable number I just put a npc/event in the map to show me number. I can provide you guys with the game files if any of you want.


EDIT: Code clean. Game keep generating 95 or higher..

1612276044434.png
How many per map are you wanting to spawn any given time?
If I'm reading that right you've got your second conditional branch nested inside the first. So it can only break the loop if the first conditional branch is triggered.
How many per map are you wanting to spawn any given time?
In this case, up to two.
If I'm reading that right you've got your second conditional branch nested inside the first. So it can only break the loop if the first conditional branch is triggered.
Yes, here is the process.

Set how many events will spawn 1 OR 2.
LOOP STARTS
Check if the rarity value is matched (if it is >= 95)
if true, spawn 1 event in the proper region
SUBTRACT number of events that will spawn by 1
if it reaches 0, break the loop. otherwise repeat it until the number o events that will spawn reaches 0.
activate switch to change pages

EDIT: I've figured out finally. Thanks @oriongates I've misunderstood what you've said the first time, I had to create an "else" for the condition. Now I'm feeling stupid. Here's the event working:


1612277634589.png
This is probably what you're looking for.
CritterEvent.png

You'll notice every time you talk to this event it will say either 1 or 2.
If it says 2 it rolled a 95 or higher. If it says 1 it rolled a 94 or lower.
I have no idea how to use a script to do that, so I don't think I can :/

You can use the Script Call command for that. Just paste the code I gave you into it and call it where you would call the text message.

Set how many events will spawn 1 OR 2.
LOOP STARTS
Check if the rarity value is matched (if it is >= 95)
if true, spawn 1 event in the proper region
SUBTRACT number of events that will spawn by 1
if it reaches 0, break the loop. otherwise repeat it until the number o events that will spawn reaches 0.
activate switch to change pages

His point is that the only way you'll subtract 1 from the number of events that will spawn is if you spawn an event. Basically what your loop does is roll the random variable until you get >= 95 some number of times.

Here's some pseudocode for what you're doing:
Code:
set n_events = random 1..2

loop
  set spawn_rarity = random 0..100

  if spawn_rarity >= 95
    spawn(<ID>)

    n_events -= 1

    if n_events == 0
      break loop
    end if
  end if
end loop

You can see that while spawn_rarity < 95 you'll just keep looping, until it goes above 95 and you finally decrement n_events, then repeat this until you decrement n_events to 0. This basically means you have to roll above 95, the event will simply keep going until you do.

I think what you want is this:

Code:
set n_events = random 1..2

loop
  set spawn_rarity = random 0..100

  if spawn_rarity >= 95
    spawn(<ID>)
  end

  n_events -= 1

  if n_events == 0
    break loop
  end if
end loop

That's what I meant by "move the logic to right before the end of the loop". This way you'll decrement the loop counter whether you roll > 95 or not.




You'll notice every time you talk to this event it will say either 1 or 2.
If it says 2 it rolled a 95 or higher. If it says 1 it rolled a 94 or lower.

@GmOcean that's not true. If it says 1 then it rolled 1 for variable #1, and if it says 2 then it rolled 2 for variable #1. You're not actually doing anything with the value you roll for variable #2. Basically what you did is this:

Code:
set temp = 0
set n = random 1..2

loop
  temp += 1

  set chance = random 1..100

  if chance >= 95
    n -= 1
  else
    n -= 1
  end

  if n == 0
    break loop
  end
end loop

But "if X then Y else Y" is the same as just "Y".

Off: That's a fairly common exercise students are given on an introductory first order logic course btw:
(a => b) ^ (!a => b)
<=> (!a v b) ^ (a v b) [Definition of =>]
<=> (!a ^ a) v (!a ^ b) v (b ^ a) v (b ^ b) [Distributive property]
<=> F v (!a ^ b) v (b ^ a) v b [Inverse law, Idempotence]
<=> b [Identity for F and then absorption twice].

Then that's the same as this:
Code:
set temp = 0
set n = random 1..2

loop
  temp += 1

  set chance = random 1..100

  n -= 1

  if n == 0
    break loop
  end
end loop

Which in turn is just this, since we don't use "chance" anywhere:

Code:
set temp = 0
set n = random 1..2

loop
  temp += 1

  n -= 1

  if n == 0
    break loop
  end
end loop

That's just counting. This is equivalent:

Code:
set temp = random 1..2
Also, an adjacent question to this topic. I've been using the same switch (hence its name being Global Critter Spawn) to disable all spawn events in the map, for every map that have spawns. It seems to work fine. the most spawners a map have is 5. Is this a problem?

Thanks @Masked but I figured out how to do that with events, I was setting it wrong. See my edited post aboce. Thank you!
I don't see how it would be a problem, outside of forgetting to turn it off/on. Assuming that this same switch is used when walking from 1 map to another, that'd be the only hurdle. Just making sure it's turned off/on when it's supposed to.
I don't see how it would be a problem, outside of forgetting to turn it off/on. Assuming that this same switch is used when walking from 1 map to another, that'd be the only hurdle. Just making sure it's turned off/on when it's supposed to.
Yeah, every time you get out of the map the switch is turned off, so the next time you enter the event will trigger again.

Original thread: https://forums.rpgmakerweb.com/threads/how-does-rmmz-calculates-variables.132945/