Advanced Party Changer

Hello fellow Rpg Makers,

I'm only a novice coder, (at best) and have just started to take a stab at java script, but so far the syntax has me muddled quite profoundly. 

My game has various locations that need defending. I need a way to send party members to various locations. The game needs know where each party member is, and whether the location their sent to is full or not. So far I've done this through eventing + variables, but I believe this can be done far easier through coding with an array or something. 

What I've done essentially looks like this;

Locations;     (These are just written down on paper, not actually variables in the game.)

0 = dead

1 = castle

2 = town

3 = tower

4 = mine

My game has many playable characters. I've made variables each of them, and when they change locations I set set their variables accordingly. 

Characters;

Tom = 0 

Dick = 1

Harry = 2

So by this Tom is dead, Dick is at the castle, and Harry's having a pint in the town's pub. 

Pretty straightforward up to here, but when I begin to put in a method to check whether a locations is 'full', or when a certain location is attacked, to know who should be in that particular party to defend it, things get more complicated. After much head scratching, I think I've found ways to accomplish most of this through events and variables, but it's quite tedious and I thought it would be good opportunity to try and learn some java script.

I'm not sure if I need to build a plugin, or if I can get away with calling script through an event. Any advice on how to proceed would be infinitely appreciated. Cheers
You could fairly simply do this with a script just by storing this in an Object

I see you're new to JavaScript, one of the most attractive features of JavaScript are objects, if you've ever seen a JSON file, you'll probably understand what an object, but in case you haven't, here's how it goes:

Objects can be used to store multiple pieces and types of data under a single variable, here's a look at the syntax of creating an object:

var Object = {id: 0, name: "Exmaple Object"};console.log(Object.id + ": " + Object.name);The above will yield you "0: Example Object" in your console.

So, what do we do with this amazing technology? Well we create an array of objects!

For your purposes something like this could be implemented:

var Locations = [];/* * 0 = dead * 1 = castle * 2 = town * 3 = tower * 4 = mine */Locations[0] = {name: "Dead", characters: [Tom], maxCharacters: 99};Locations[1] = {name: "Castle", characters: [Dick], maxCharacters: 2};Locations[2] = {name: "Town", characters: [Harry], maxCharacters: 3};Of course you'd replace the names with the Actor's ID or and Object of them! (Objects in Arrays in Objects in Arrays!)

So to refer to any locations current characters in them you'd call something like this:

Locations[LocationID].charactersAnd you'll be given an array of all characters in that current location

If you also wanted to work backwards at some point, you could also add a "Location" property to the $dataActors entry for your actors.

In RPG Maker MV, all of the data is loaded in under these objects called $dataX (where X is the type of data), these are easily editable and are maintained throughout play.

To do so, in a swap locations function you'd do something like this:

var swapActorLocation = function(actorId, locationId) { // Function Stuff Here... $dataActor[actodId].location = locationId;}This will at any time allow you just with the actor's ID to find where they are.

And since you are new to JavaScript, you can remove entries from an array like this:

ArrayName.splice(indexOfEntry, 1);This should simplify your script massively, if you need any more help, or have any questions post away.
Of course you'd replace the names with the Actor's ID or and Object of them! (Objects in Arrays in Objects in Arrays!)
I think I get it, because the Actor's ID are themselves Objects in an Array. Trippy! Haha

Location[0] = {name: "dead", characters: [0001], maxCharacters: 99, occupancy: 1};Location[1] = {name: "party", characters: [0002], maxCharacters: 1, occupancy: 1};if (Locations[1].maxCharacters == Locations[1].occupancy) { partyfull = "the party is full.";}I'm not sure if this is redundant, but I added 'occupancy' to Locations so that 'maxCharacters' can have something to check against.

If you also wanted to work backwards at some point, you could also add a "Location" property to the $dataActors entry for your actors.

In RPG Maker MV, all of the data is loaded in under these objects called $dataX (where X is the type of data), these are easily editable and are maintained throughout play.
locationID.png locationID2.png

I thought I was defining the 'locationId' in the first picture, but I'm still getting a not defined error.  🤔

// tom's actorId = 0001// dick's actorId = 0002var Locations = [];Locations[1] = {name: "party", characters: [0002], maxCharacters: 1, occupancy: 2};Locations[1].splice(1, 1, $dataActor[0001]);So using the splice() method to add Tom to the party would look like this? 

var swapActorLocation = function swap(0001, 1) { if (Locations[1].maxCharacters == Locations[1].occupancy) { partyfull = "the party is full...";} else { $dataActor[0001].location = 1;}So if I wanted to check the space of my party before adding someone to it, would this be correct? 

I really appreciate the feedback. I've tried to learn to code so many times, but without something tangible to work on I found it hard to put into practice.  🙂
I think I get it, because the Actor's ID are themselves Objects in an Array. Trippy! Haha

Location[0] = {name: "dead", characters: [0001], maxCharacters: 99, occupancy: 1};Location[1] = {name: "party", characters: [0002], maxCharacters: 1, occupancy: 1};if (Locations[1].maxCharacters == Locations[1].occupancy) { partyfull = "the party is full.";}I'm not sure if this is redundant, but I added 'occupancy' to Locations so that 'maxCharacters' can have something to check against.

attachicon.gif
locationID.png 
attachicon.gif
locationID2.png

I thought I was defining the 'locationId' in the first picture, but I'm still getting a not defined error.  🤔

// tom's actorId = 0001// dick's actorId = 0002var Locations = [];Locations[1] = {name: "party", characters: [0002], maxCharacters: 1, occupancy: 2};Locations[1].splice(1, 1, $dataActor[0001]);So using the splice() method to add Tom to the party would look like this? 

var swapActorLocation = function swap(0001, 1) { if (Locations[1].maxCharacters == Locations[1].occupancy) { partyfull = "the party is full...";} else { $dataActor[0001].location = 1;}So if I wanted to check the space of my party before adding someone to it, would this be correct? 

I really appreciate the feedback. I've tried to learn to code so many times, but without something tangible to work on I found it hard to put into practice.  🙂
Okay, you've got a lot right, and a bit wrong.

Occupancy is redundant as every Array has a variable attached to it called length that you get get like this:

ArrayName.lengthThis will return the total number of entries within the array, so if you want to find how many people are in a location  currently you'd do something like this:

Locations[LocID].characters.lengthWith splice you only include 2 parameters, the index of the entry in the array and the amount of data you want to remove, so in order to get the index of an item in array you can simply do this:

ArrayName.indexOf(itemYouWantToFind)In your case this could be employed:

Locations[LocId].characters.splice(Locations[LocId].indexOf($dataActor[actorId], 1);Next thing: don't modify original RPG Maker files, its unnecessary and messy, you can simply add properties to objects on the start up of your script like this:

for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0;}This will give all of your actors a location property.

With you function, I'm assuming you just filled in the values for an example? If not just replace them with the names I've referred to in the post before.

Also those id numbers aren't correct, in RPG Maker they pad the ids with 0s, so you want to remove those as it'll cause errors.

If you're snuggling with Programming I highly recommend https://www.codecademy.com

as well as http://w3schools.com/

They are both fantastic resources for learning and w3schools is also fantastic as a reference point.
Thanks for taking the time to clarify those details, I can see where I went wrong. I just finished the 'Learn Java' skill on Codecademy, and I'm working my way through the JavaScript one. W3schools definitely looks like a great resource. 

 My plugin just consists of:

var Locations = [];Locations[0]= {name: "Dead", characters: [], maxCharacters: 99};Locations[1]= {name: "Party", characters: [], maxCharacters: 4};Locations[2]= {name: "Castle", characters: [], maxCharacters: 4};if ($dataActors !== null) { for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0;}}I was getting a "TypeError: Cannot read property 'length' of null" error from the console log, but I made it go away. I'm guessing I didn't actually fix the problem though, as when I try to do this;

Locations[1].characters.splice(Locations[1].indexOf($dataActor[1], 1);I get an "unexpected token ;" I know I haven't included a Function yet, but I thought everyone should at least have a locationId. Do I need to import something like the $dataActor object into the plugin?
Thanks for taking the time to clarify those details, I can see where I went wrong. I just finished the 'Learn Java' skill on Codecademy, and I'm working my way through the JavaScript one. W3schools definitely looks like a great resource. 

 My plugin just consists of:

var Locations = [];Locations[0]= {name: "Dead", characters: [], maxCharacters: 99};Locations[1]= {name: "Party", characters: [], maxCharacters: 4};Locations[2]= {name: "Castle", characters: [], maxCharacters: 4};if ($dataActors !== null) { for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0;}}I was getting a "TypeError: Cannot read property 'length' of null" error from the console log, but I made it go away. I'm guessing I didn't actually fix the problem though, as when I try to do this;

Locations[1].characters.splice(Locations[1].indexOf($dataActor[1], 1);I get an "unexpected token ;" I know I haven't included a Function yet, but I thought everyone should at least have a locationId. Do I need to import something like the $dataActor object into the plugin?

You need to ensure that $dataActors actually exists first, because if the check runs nothing is going to happen because it doesn't exist at all yet.

So you'll have to modify the data manger functions, I recommend actually looking at other scripts and how they do this, Yanfly's scripts have this at the start.

var MyCustom_DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() { if (!MyCustom_DataManager_isDatabaseLoaded.call(this)) { return false; } if ($dataActors) { // Do the setup here or call a function to do it here } return true;}Your second error is happening because you forgot a ) at the end of your statement before the ; this is why it is saying it was "unexpected" of the ; to be there rather than the ) as per usual.
var Location_DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() {if (!Location_DataManager_isDatabaseLoaded.call(this)) return false;this.swapActorLocation($dataActors);return true;}DataManager.swapActorLocation = function(actorId, locationId) { for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0; $dataActor[actodId].location = locationId;}}var Locations = [];Locations[0]= {name: "Dead", characters: [1], maxCharacters: 99};Locations[1]= {name: "Party", characters: [1,2], maxCharacters: 2};Locations[2]= {name: "Castle", characters: [3], maxCharacters: 4}; This is the best I could come up with.  :|  

I'm not getting errors, but now Locations is undefined so something isn't going right at the beginning.

if ($dataActors) { // Do the setup here or call a function to do it here } return true;}All the scripts I've scoured didn't have an 'if' statement after the 'return false'. I just don't understand how to turn $dataActors into an argument which sets up a function to make $dataActors exist..

I've been attempting to copy what I found here;

Yanfly.APS.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() { if (!Yanfly.APS.DataManager_isDatabaseLoaded.call(this)) return false; this.processAPSNotetags($dataActors); this.processAPSNotetags($dataClasses); this.processAPSNotetags($dataEnemies); this.processAPSNotetags($dataSkills); this.processAPSNotetags($dataWeapons); this.processAPSNotetags($dataArmors); return true;};DataManager.processAPSNotetags = function(group) { var note1 = /<(?😛ASSIVE STATE):[ ]*(\d+(?:\s*,\s*\d+)*)>/i; var note2 = /<(?😛ASSIVE STATE):[ ](\d+)[ ](?:THROUGH|to)[ ](\d+)>/i; for (var n = 1; n < group.length; n++) { var obj = group[n]; var notedata = obj.note.split(/[\r\n]+/);-but I don't know if the '.this' keyword is applicable in my case. Do I need to worry about Strict mode? 
var Location_DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() {if (!Location_DataManager_isDatabaseLoaded.call(this)) return false;this.swapActorLocation($dataActors);return true;}DataManager.swapActorLocation = function(actorId, locationId) { for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0; $dataActor[actodId].location = locationId;}}var Locations = [];Locations[0]= {name: "Dead", characters: [1], maxCharacters: 99};Locations[1]= {name: "Party", characters: [1,2], maxCharacters: 2};Locations[2]= {name: "Castle", characters: [3], maxCharacters: 4}; This is the best I could come up with.  :|  

I'm not getting errors, but now Locations is undefined so something isn't going right at the beginning.

if ($dataActors) { // Do the setup here or call a function to do it here } return true;}All the scripts I've scoured didn't have an 'if' statement after the 'return false'. I just don't understand how to turn $dataActors into an argument which sets up a function to make $dataActors exist..

I've been attempting to copy what I found here;

Yanfly.APS.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() { if (!Yanfly.APS.DataManager_isDatabaseLoaded.call(this)) return false; this.processAPSNotetags($dataActors); this.processAPSNotetags($dataClasses); this.processAPSNotetags($dataEnemies); this.processAPSNotetags($dataSkills); this.processAPSNotetags($dataWeapons); this.processAPSNotetags($dataArmors); return true;};DataManager.processAPSNotetags = function(group) { var note1 = /<(?😛ASSIVE STATE):[ ]*(\d+(?:\s*,\s*\d+)*)>/i; var note2 = /<(?😛ASSIVE STATE):[ ](\d+)[ ](?:THROUGH|to)[ ](\d+)>/i; for (var n = 1; n < group.length; n++) { var obj = group[n]; var notedata = obj.note.split(/[\r\n]+/);-but I don't know if the '.this' keyword is applicable in my case. Do I need to worry about Strict mode? 

You're running the wrong function, swapActorLocations shouldn't be defined under DataManger but locally in your script just like this:

var swapActorLocation = function(actorId, locationId) { // your function here}The function you want to make and run at start would be something like this:

DataManager.setupActorLocations = function() { for (var i = 1; i < $dataActors.length; i++) { $dataActors.location = 0; }}The "this" keyword is used to refer to variables or functions within the scope of an Object, so in the case of DataManager the function setupActorLocations is defined under it, this prevents clashing with functions in your script locally with the same running.
Aha! It works. Characters have locations!!!  😀

I can manually change a characters location like so;

$dataActors[1].location = 2;but when I try to call the function I get "Cannot set property of location undefined" error.

Code:
swapActorLocation($dataActors[1], 1);
Code:
var Location_DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() {	if (!Location_DataManager_isDatabaseLoaded.call(this)) return false;	this.setupActorLocations($dataActors);	return true;}DataManager.setupActorLocations = function() { for (var i = 1; i < $dataActors.length; i++) {  $dataActors[i].location = 5; }}var Locations = []; Locations[0]= {name: "Dead", characters: [1], maxCharacters: 99};Locations[1]= {name: "Party", characters: [1,2], maxCharacters: 4};Locations[2]= {name: "Castle", characters: [3], maxCharacters: 4}; var swapActorLocation = function(actorId, locationId) {	$dataActors[actorId].location = locationId;}
Aha! It works. Characters have locations!!!  😀

I can manually change a characters location like so;

$dataActors[1].location = 2;but when I try to call the function I get "Cannot set property of location undefined" error.

swapActorLocation($dataActors[1], 1);
Code:
var Location_DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;DataManager.isDatabaseLoaded = function() {	if (!Location_DataManager_isDatabaseLoaded.call(this)) return false;	this.setupActorLocations($dataActors);	return true;}DataManager.setupActorLocations = function() { for (var i = 1; i < $dataActors.length; i++) {  $dataActors[i].location = 5; }}var Locations = []; Locations[0]= {name: "Dead", characters: [1], maxCharacters: 99};Locations[1]= {name: "Party", characters: [1,2], maxCharacters: 4};Locations[2]= {name: "Castle", characters: [3], maxCharacters: 4}; var swapActorLocation = function(actorId, locationId) {	$dataActors[actorId].location = locationId;}
Your actor ID is just 1 and not the actor Object itself. As in the code you've got the actorId being put in the array as an index already.
Oh man, I've been staring at code for far too long. Thank you so much for you help. This has been quite educational, albeit frustrating at times.  😉

Original thread: https://forums.rpgmakerweb.com/threads/advanced-party-changer.48207/