How to reference Variable ID or Event ID in custom script or loop

Hi All,

I need to write script as below in MV (it's supposed to be a common event).
Can you advise how to do it or share necessary syntax to do it?

I think it's impossible to do as simple loop in game without script.
---
For each var id (in set range, say 1-80)
if var is 0 then var is 2

Choose at random two var ids (in range) which are equal to 1 and set them to 0

if none vars (in range) are equal 1 during randomization, then change all vars which are equal 2 to be equal 1 (and continue randomization)
---

Also I wonder if I can put this effect in event page: ", for var's id which is equal to this event's id change this var to be equal 3
I'm slightly confused by exactly what it is you're looking for. Will you be setting these variables to 1 manually somewhere for the second bit?
Yes.
It's supposed to mirror a card game mechanic.
Each event is a "card" and variable which corresponds to the event tells us it's status.
0 - Active / in hand
1 - In deck.
2 - Discarded
3 - Inactive

So I need script to:
- Discard all active cards
- Activate two random cards from deck (reshuffle discarded if needed)
Code:
for (var i = 1; i <= 80; i++)
{
  if ($gameVariables.value(i) === 0) { $gameVariables.setValue(i, 2); }
}
var activatedCards = 0;
while (activatedCards < 2)
{
  if (activateRandomCard());
  {
    activatedCards++;
  } else {
    for (var i = 1; i <= 80; i++)
    {
      if ($gameVariables.value(i) === 2) { $gameVariables.setValue(i, 1); }
    }
  }
}

function activateRandomCard()
{
  var deck = $gameVariables._data.filter(function(variable) { return $gameVariables.value(variable) === 1; });
  if (deck.length > 0) {
    $gameVariables.setValue(deck[Math.randomInt(deck.length - 1)], 0);
    return true;
  }
  return false;
}

Try that.
Thanks a lot. Will try .
I believe there is extraneous ; after "if (activateRandomCard())" so I removed it.

1. Can you explain how exactly these lines work?
Code:
var deck = $gameVariables._data.filter(function(variable) { return $gameVariables.value(variable) === 1; });
  if (deck.length > 0) {
    $gameVariables.setValue(deck[Math.randomInt(deck.length - 1)], 0);
I seem to never end up with Variables equal to 2 as if it shuffles each time, even when not needed.

2. Should there be "deck.length - 1" I think random(0) is the same as random(1), no?

Thanks.

Edit: I think removing the "-1" solves everything. Will do more testing.
Sorry for bumping. I tested all parts of the code and only this line is not working as expected:
Code:
var deck = $gameVariables._data.filter(function(variable) { return $gameVariables.value(variable) === 1; });
I'm checking deck.length at this point and it doesn't correspond to number of variables equal 1 in range.

Please advise how to code setting deck variable.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-reference-variable-id-or-event-id-in-custom-script-or-loop.86218/