JK Preserve Map Event States

Pirobi

Veteran
Veteran
Joined
Oct 25, 2015
Messages
69
Reaction score
90
First Language
English
Primarily Uses

JK Preserve Map Event States v1.1


Author: Pirobi


Introduction


Normally in RPG Maker, if you have an event with multiple pages, the event will automatically switch to the highest page # that has its conditions fulfilled. For example, if you have an event that disappears after a switch turns on, it will disappear as soon as that switch turns on.


Sometimes, this behavior is not desired. What if you have an npc that will disappear when it becomes night (he leaves to go to bed)? It would become jarring to see that event suddenly disappear. This plugin aims to resolve this issue.


Features


The plugin keeps track of switch and/or variable IDs that you want to control to prevent the above behavior. The event pages using these variables/switches as their conditions will only become active when the player changes maps. Going back to the above example: If you have a switch that sets to true when it is nighttime, and this plugin is tracking that switch, the npc will not disappear off the map immediately when the switch is set to true, instead disappearing after you leave the map and come back to it, or use a command(see Plugin Commands).


How to Use


Add the plugin to your plugins list. Then, add the variable and/or switch IDs that you wish to track. (i.e. 1    or    1,2,3,4,. . .)


Now, when you go through the game, if a switch changes value that is in the tracking list, the game will flag it as having changed. Events that use that switch in their event pages will not update as long as you are on the same map.


NEW in 1.1! 


You can now have events on the map that ignore this plugin, essentially behaving as if you don't have this plugin.


To do this, add the following notetag at the END of the object's note box:


<AllowStateChange>


Plugin Commands


If you want a switch or variable to resume normal behavior while the player is still on that map, use one of the following commands:


ResetVars - This will Clear the list of flagged variables


ResetSwitches - Same as above, except for switches


ResetVars id1 id2 . . . - This will remove the specified variables(by ID) from the list of flagged variables.


ResetSwitches id1 id2 . . . - Same as above, except for switches


Using these plugin commands will cause events to update normally(see Things to Consider), so even if an event page would not change because of the tracked switch, using the command will then allow the page to change.


Demo


Download the Demo Here


Things to Consider

  • This doesn't prevent the value of the switch or variable changing. The values will still change, but the event pages won't update if their conditions become true/false with them.
  • This doesn't consider Self Switches, Items, or Actors in the condition pages for events. They will function the same.
  • This doesn't consider switches or variables that are changed by script call. Only switches/variables changed using the event commands (Control Switches/Variables).
  • Currently, this does not force events to immediately refresh after using the plugin commands.

    If you reset switch 1, and then, for example, Self Switch A is activated, then the events using switch 1 will process normally. I might fix this in a future update to auto-refresh.




Script

//:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
// JK_PreserveMapEventStates.js v1.1
//:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
/*:
* @plugindesc Have certain event pages only update on map load. <JKMap>
* @author Pirobi

* @param SwitchesToCheck
* @desc The switch(es) to keep track of. (i.e. 1 or 1,2,3,...)
* @default

* @param VarsToCheck
* @desc The variable(s) to keep track of. (i.e. 1 or 1,2,3,...)
* @default

* @help Normally, if you change the value of a switch, any event
pages on the map that use that switch will immediately switch to
that page if the condition is true.
It can be jarring to have people appear on the map
suddenly if using a time-based system, for example.
This plugin aims to make it so if an event's page would change
due to a switch/variable making the page active,
the event page won't be updated until you leave and re-enter the map.

NOTE: If you are checking if a switch is activated, or checking if a
variable is a specific value, it WILL STILL UPDATE even though the
variable/switch is being tracked.

This only prevents EVENT PAGES from activating due to the Switch/Variable!
Switches/Variables WILL STILL CHANGE VALUE!!!!!!!!

In addition, this also does not take into account variables or switches
that were changed by script(i.e. not through the event commands)!

This also does not take into account other page conditions
(Self Switch, Item, Actor). These will still perform as normal.

:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
Instructions For Use:
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
Just set the variables/switches that the plugin will keep track of,
and you are good to go!

NEW in 1.1: If you want an event to ignore this script, add
the following to the note box of the event:

<AllowStateChange>

:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
Plugin Commands:
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:

ResetVars
This will clear the List of Variables that changed while on the map.

ResetVars id1 id2 . . .
This will remove the specified Variable Ids from the list of changed
variables.

ResetSwitches
This will clear the list of Switches that changed while on the map.

ResetSwitches id1 id2 . . .
This will remove the specified Switch Ids from the list of changed
switches.

You can use these to basically force the original event behavior.
After the command is called, the events will all update again, so
Any events that were flagged to not change will now change.

:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
Updates
:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
-v1.1: Added <AllowStateChange> notetag to have events that ignore this script.
-v1.0: Initial Version

:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
Copyright Information
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
Copyright 2016, JK Software

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var Imported = Imported || {};
var JKMap = JKMap || {};

//:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
//JKMap
//:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
(function($){
"use strict";
var parameters = $plugins.filter(function(plugin){return plugin.description.contains('<JKMap>')})
if(parameters.length === 0){
throw new Error("Couldn't find parameters.");
}
$.Parameters = parameters[0].parameters;
$.Param = {};

$.VariableList = $.Parameters.VarsToCheck.split(",") || [];
$.SwitchList = $.Parameters.SwitchesToCheck.split(",") || [];

$.MapVarsChanged = {};
$.MapSwchsChanged = {};
$.CurrentMap = null;
})(JKMap);

//When the switch value is changed through an event
//And the switch is a part of the list of switches to watch,
//Add the switch to the list of switches changed.
var JKSwitchChanged = Game_Interpreter.prototype.command121;
Game_Interpreter.prototype.command121 = function(){
var rv = JKSwitchChanged.call(this);
for(var i = this._params[0]; i <= this._params[1]; i++){
if(JKMap.SwitchList.contains(String(i))){
JKMap.MapSwchsChanged = true;
}
}
//console.warn(JKMap.MapSwchsChanged);
return rv;
};


//When the variable value is changed through an event
//And the variable is a part of the list of variables to watch,
//Add the variable to the list of variables changed.
var JKVarChanged = Game_Interpreter.prototype.command122;
Game_Interpreter.prototype.command122 = function(){
var rv = JKVarChanged.call(this);
for(var i = this._params[0]; i <= this._params[1]; i++){
if(JKMap.VariableList.contains(String(i))){
JKMap.MapVarsChanged = true;
}
}
//console.warn(JKMap.MapVarsChanged);
return rv;
};

//When the player enters a new map, clear the list of vars/switches
//That were changed on the previous map.
var JKLoadMap = DataManager.loadMapData;
DataManager.loadMapData = function(mapId){
JKLoadMap.call(this, mapId);
if(JKMap.CurrentMap === null) JKMap.CurrentMap = mapId;
else if(mapId !== JKMap.CurrentMap){
JKMap.MapSwchsChanged = {};
JKMap.MapVarsChanged = {};
JKMap.CurrentMap = mapId;
}
};

//When checking if an event page should be selected, add an
//Additional condition to check if the variable/switches used
//On the page did NOT change while the player was on the map
//I.E. This should return true only if the switch/variables
//On the page meet the conditions AND they did not meet
//Conditions while the player was still on the map.
var JKMeetsConditions = Game_Event.prototype.meetsConditions;
Game_Event.prototype.meetsConditions = function(page){
var meetsConditions = JKMeetsConditions.call(this, page);
return meetsConditions && (this.event().meta.AllowStateChange ||
this.pageCondChangedOnSameMap(page) === false);
};

//Custom function to check if the switches/variables on this event page
//Were changed while on the map.
//When the player enters a new map, MapSwchsChanged and MapVarsChanged
//Will both reset.
Game_Event.prototype.pageCondChangedOnSameMap = function(page){
var c = page.conditions;
var swchKeys = Object.keys(JKMap.MapSwchsChanged);
var varKeys = Object.keys(JKMap.MapVarsChanged);
var changedOnSameMap =
(
swchKeys !== null &&
varKeys !== null &&
(
(c.switch1Valid && swchKeys.contains(String(c.switch1Id))) ||
(c.switch2Valid && swchKeys.contains(String(c.switch2Id))) ||
(c.variableValid && varKeys.contains(String(c.variableId)))
)
)
return changedOnSameMap;
};

//When determining which page to use,
//Either take the page that meets conditions and wasn't changed on the same map,
//(An event appearing because the page is now true)
//OR
//The current page being displayed whose conditions changed on the same map.
//The event disappearing because the page is now false).
Game_Event.prototype.findProperPageIndex = function() {
var pages = this.event().pages;
for (var i = pages.length - 1; i >= 0; i--) {
var page = pages;
if(this.event().meta.AllowStateChange){
if(this.meetsConditions(page)){
return i;
}
}
else if (this.meetsConditions(page) || (i === this._pageIndex && this.pageCondChangedOnSameMap(page))) {
return i;
}
}
return -1;
};

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args){
switch(command){
case "ResetVars":
if(args.length > 0){
for(var i = 0; i < args.length; i++){
delete JKMap.MapVarsChanged[args];
}
}
else{
JKMap.MapVarsChanged = {};
}
break;
case "ResetSwitches":
if(args.length > 0){
for(var i = 0; i < args.length; i++){
delete JKMap.MapSwchsChanged[args];
}
}
else{
JKMap.MapSwchsChanged = {};
}
break;
default:
_Game_Interpreter_pluginCommand.call(this, command, args);
break;
}
};

Imported["JKMap"] = 1.1;






FAQQ: Can I use this plugin for my commercial game?A: Yes, you can! Please be sure to check the licensing information, either in the script or below. And be sure to let me know if you do use it (a copy of the game would be nice too!)


Q: I'm running into [insert bug/error here], what do I do?


A: Please report any problems, suggestions, or comments here in this thread, or send me a pm. I will do my best to keep working on the plugin to make it more beautiful, optimized, etc.


Credit And Thanks


- Pirobi(me)


- Liquidize


- JK Software
 
Last edited by a moderator:

Neo Soul Gamer

Veteran
Veteran
Joined
Aug 10, 2012
Messages
644
Reaction score
378
First Language
English
Primarily Uses
N/A
I can see this as an amazing plugin for help with Cutscenes. 


Question, what about battles that occur within the same map? Does the switch/variable update when you finish the battle?
 

Pirobi

Veteran
Veteran
Joined
Oct 25, 2015
Messages
69
Reaction score
90
First Language
English
Primarily Uses
I can see this as an amazing plugin for help with Cutscenes. 


Question, what about battles that occur within the same map? Does the switch/variable update when you finish the battle?


That's a good question. I believe it would update, since when I was still making the plugin, even going into the menu updated the variable.


Additionally, I plan on adding an option to add a tag to events to have them ignore this plugin.
 

Solis

Veteran
Veteran
Joined
Oct 24, 2015
Messages
376
Reaction score
84
First Language
English
I plan on using it as my NPC does this exact thing! 
 

Neo Soul Gamer

Veteran
Veteran
Joined
Aug 10, 2012
Messages
644
Reaction score
378
First Language
English
Primarily Uses
N/A
@Pirobi So then it must update when changing maps AND calling on scenes? I'll have to test this when I have the time. Thanks!
 

Pirobi

Veteran
Veteran
Joined
Oct 25, 2015
Messages
69
Reaction score
90
First Language
English
Primarily Uses
@Neo Soul Gamer let me know the results of your testing! I am fairly certain that it will update when the user returns to the map after a battle.


Updated to v1.1!


I added the ability to have events ignore this plugin, essentially making the object behave like normal(as if you weren't using this plugin).


To do this, just add this notetag at the END of the object's note box:


<AllowStateChange>


The demo was updated to have the script, but doesn't implement the new features - but you can play around with the demo to test it out! 


Currently, the tag DOES NOT allow you to allow state change for specific variables or switches(e.g <AllowVariableChange: 1,2,3> if you only wanted variables 1,2,3 to make the event behave normally). I am not sure if this will become a feature in the future, as the current tag seems sufficient enough. 


Let me know if there are any issues!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

I hope my annual #DoItForHargon contest gets lots of entries this year (since a Playstation 5 is a pretty big prize lol) XD On further news - I'm back to game devving next week after hiatus cause of that tragedy of losing everything I own.
Rome Trastevere deserve a tileset. Mumble mumble...
Before the full time job, I can make 1 MZ plugin per day, even though it's still hard; With the full time job, making 1 such plugin per week becomes just as hard lol
I have a new RPG Maker music pack announcement coming soon. I am really having good time composing this one!

Forum statistics

Threads
102,989
Messages
996,500
Members
134,458
Latest member
MsAva
Top