One Variable reference multiple variables

So, what I am looking for is one variable to reference the levels of ALL players.

What I have:

Variable {Player Level} referencing {Harold Level}, {Marsha Level}, {Therese Level} and {Sarial Level}. {Harold Level}, {Marsha Level}, {Therese Level} and {Sarial Level} then each respectively reference their character levels.

Does this work? I haven't tried it yet. I would just like one Variable to use rather than 4.

Thanks!
You can set it as an array I believe...

Like

Code:
$gameVariables.setValue(id,[$gameActors.actor(id1).level,$gameActors.actor(id2).level,$gameActors.actor(id3).level,$gameActors.actor(id4).level])

Also depending on where you want to use it, there is probably a better way to do it.

PS: Not sure if its level, level() or _level
You can set it as an array I believe...

Like

Code:
$gameVariables.setValue(id,[$gameActors.actor(id1).level,$gameActors.actor(id2).level,$gameActors.actor(id3).level,$gameActors.actor(id4).level])

Also depending on where you want to use it, there is probably a better way to do it.

PS: Not sure if its level, level() or _level

Tried to use this, didnt work

Still looking for something that will.
@Engr. Adiktuzmiko 's method should work (and it's .level). Are you replacing the "id1" and other variables by the correct numbers?
@Engr. Adiktuzmiko 's method should work (and it's .level). Are you replacing the "id1" and other variables by the correct numbers?

So where "id" is, would be the variable i am calling, and "id1" would be the first variable in the array right?
Yes, so to make things clearer:

Code:
$gameVariables.setValue(id,             // replace the "id" for the variable id
[$gameActors.actor(id1).level,          // replace the "id1" for the id of the first actor. Id starts off at 1.
$gameActors.actor(id2).level,           // replace the "id2" for the id of the second actor. Id starts off at 1.
$gameActors.actor(id3).level,           // replace the "id3" for the id of the third actor. Id starts off at 1.
$gameActors.actor(id4).level])          // replace the "id4" for the id of the fourth actor. Id starts off at 1.

** NOTE: the "//" means that everything that follows it on the same line is a comment, so it isn't part of the code read by the CPU.

Don't copy my format, use Engr.'s format. I simply divided to code so it's easier to understand. You can keep on going and add more actors. Now, to actually call the variable and read the value, arrays start at 0. So if you wanted to get the level of the actor id #1 in this example, you would have to do this:

Code:
$gameVariables.value(id)[0];     // Again, replace "id" by the id of the variable everything is stored in.
Yes, so to make things clearer:

Code:
$gameVariables.setValue(id,             // replace the "id" for the variable id
[$gameActors.actor(id1).level,          // replace the "id1" for the id of the first actor. Id starts off at 1.
$gameActors.actor(id2).level,           // replace the "id2" for the id of the second actor. Id starts off at 1.
$gameActors.actor(id3).level,           // replace the "id3" for the id of the third actor. Id starts off at 1.
$gameActors.actor(id4).level])          // replace the "id4" for the id of the fourth actor. Id starts off at 1.

** NOTE: the "//" means that everything that follows it on the same line is a comment, so it isn't part of the code read by the CPU.

Don't copy my format, use Engr.'s format. I simply divided to code so it's easier to understand. You can keep on going and add more actors. Now, to actually call the variable and read the value, arrays start at 0. So if you wanted to get the level of the actor id #1 in this example, you would have to do this:

Code:
$gameVariables.value(id)[0];     // Again, replace "id" by the id of the variable everything is stored in.

Ok, so if i wanted Variable 2 (Player Level) and actor # 1-4, would it look like this:

Code:
$gameVariables.setValue(2,[$gameActors.actor(1).level,$gameActors.actor(2).level,$gameActors.actor(3).level,$gameActors.actor(4).level])
Yeah, it looks good. Is it working?
Yeah, it looks good. Is it working?

Not really.

So let me give some background of what I am trying to achieve:

I want certain areas locked down if the player level average is equal to or higher than 99. What I was doing was storing each characters level into a separate variable and using a single variable to have all 4 set too. That didn't work. I am using a conditional branch to look at the variable and see if its higher than 99, else let them inside the area.

Using said script above, I was using it as a parallel process at the very beginning of the game. Still, didnt work.

:/
@Jenova Well then, for what you want to accomplish (checking of the sum of all the party members' levels is higher than a certain number), I would suggest trying this method:

Code:
var allLevels = 0;
$gameParty.members().forEach(function(actor) {
    allLevels += actor.level;
})

$gameVariables.setValue(id, allLevels);    // Again, replace the "id" by the variable you want to store the info in.

Insert this in a script call. You only have to change the id to the variable id you want to use on the last line of the code. This script will calculate the sum of the all the party members's level and store it in the variable you choose (the "id" on the last line). So it's going to set that variables to that value.

After that it's easy to just use a conditional branch and use that variable as a condition.

Tell me if it works or not.
@Jenova Well then, for what you want to accomplish (checking of the sum of all the party members' levels is higher than a certain number), I would suggest trying this method:

Code:
var allLevels = 0;
$gameParty.members().forEach(function(actor) {
    allLevels += actor.level;
})

$gameVariables.setValue(id, allLevels);    // Again, replace the "id" by the variable you want to store the info in.

Insert this in a script call. You only have to change the id to the variable id you want to use on the last line of the code. This script will calculate the sum of the all the party members's level and store it in the variable you choose (the "id" on the last line). So it's going to set that variables to that value.

After that it's easy to just use a conditional branch and use that variable as a condition.

Tell me if it works or not.

No.

If I wanted it to use variable 2, would it look like this:

Code:
var allLevels = 0;
$gameParty.members().forEach(function(actor) {
    allLevels += actor.level;
})

$gameVariables.setValue(2, allLevels);
Yeah that should work. I just tested it.

Add another scrip call after that one:

Code:
console.log($gameVariables.value(2));

Can you tell me what the console shows? (to open the console, press F8 or F12).
Here's a quick 1-liner

Code:
$gameVariables.setValue(2, $gameParty.members().reduce((a, c) => a += c.level, 0) / $gameParty.members().length);

This will assign the average of every current party member to variable 2. Then you'd just need a conditional branch to check something like:

If : #002 < 99
- Stuff that happens if the average level is less than 99
Else
- Stuff that happens if the average level is 99 or higher
End
Yeah that should work. I just tested it.

Add another scrip call after that one:

Code:
console.log($gameVariables.value(2));

Can you tell me what the console shows? (to open the console, press F8 or F12).

[IMG]https://i.ibb.co/BCM82kg/Consle-Log.png[/IMG]

Opened the consle log before the script was triggered, that was the first one. Then I ran the code you gave me in the console log, before and after leveling up my characters. Thats the other ones.

Here's a quick 1-liner

Code:
$gameVariables.setValue(2, $gameParty.members().reduce((a, c) => a += c.level, 0) / $gameParty.members().length);

This will assign the average of every current party member to variable 2. Then you'd just need a conditional branch to check something like:

If : #002 < 99
- Stuff that happens if the average level is less than 99
Else
- Stuff that happens if the average level is 99 or higher
End

Hmmm, still dosnt work.

I wonder what i am doing wrong...
There something seriously weird. I'm ready to bet that it has to do with one of the plugins you have that might modify how leveling works. Try doing the same thing in a new project with no plugin.
There something seriously weird. I'm ready to bet that it has to do with one of the plugins you have that might modify how leveling works. Try doing the same thing in a new project with no plugin.

Same thing. Idk what is happeneing....
Try doing it with the script command in the event command for set variable. If you still get errors something is busted.
Try doing it with the script command in the event command for set variable. If you still get errors something is busted.

Validated files. 9 files reacquired. Still didnt fix it.

I run: console.log($gameVariables.value(2));

And it returns 0, 1 or 4 depending on the script I use, but never increases if I run it again, no matter how many levels I gain.
That will never return it. You need to call the script command to have it update the value. Maybe the poster can clarify what the script command is, as I don't know enough JavaScript to find it, but I do know that what you are calling will never work as all it does is return what is in variable 2, but you need to call the command to update variable 2 first.
That will never return it. You need to call the script command to have it update the value. Maybe the poster can clarify what the script command is, as I don't know enough JavaScript to find it, but I do know that what you are calling will never work as all it does is return what is in variable 2, but you need to call the command to update variable 2 first.

I will have this closed. I will just use Switches and Conditional Branches to get my desired effect. Sorry, this has become too complicated. :/

Original thread: https://forums.rpgmakerweb.com/threads/one-variable-reference-multiple-variables.110838/