Storing The Party's Current Item List

Discussion in 'JS Plugin Requests' started by junglechief, Nov 26, 2015.

  1. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    I have a retry function built into my game. The only piece it is missing is returning used items. My thought process is that I would need to create a temporary array to store the current item list on battle start and then on retry set the party's item list equal to whatever is in the temporary array. Can anyone help me with this. I don't know which functions from the source code I would need to pull into the plugin. I would like to call this function on battle start and presumable set the temp array equal to my current inventory within the common event that is the base of my retry function. Thanks in advanced for any help!

    Anyone have any idea's on this?

    If anyone doesn't have an answer to this, do they know if there is an easy way to restore a state before a battle or if there is a plugin that does this.
     
    Last edited by a moderator: Dec 3, 2015
    #1
  2. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    923
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    All you need to do is store $gameParty._items, $gameParty._weapons, and $gameParty.armors in 3 separate variables. However, since these are stored as JS Objects, you'll need to clone them, not just set a new variable equal to it. You may want to consider this deep clone function, from the MVCommons:

    Code:
      /**   * MVC.deepClone(obj)   * @param obj The object to clone   * @return The cloned object   *   * Deep clones an object and its properties.   * This function will crash when used on recursive objects.   * Numbers, Strings and Functions are not deep cloned, thus, if they   * are used for the obj param, they will be returned without change.   */  function deepClone(obj) {    var result;    if ($.isArray(obj)) {      return obj.map(function (i) { return $.deepClone(i); });    } else if (obj && !obj.prototype && (typeof obj == 'object' || obj instanceof Object)) {      result = {};      for (var p in obj) {        result[p] = $.deepClone(obj[p]);      }      return result;    }    return obj;  }
     
    #2
  3. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    If I added MVCommons to my script list how would this look. Say on battle start I wanted to store what was currently in my inventory. Would I do something like:

    int curItems

    curItems = deepclone($gameParty._items)

    then on retry I would call the opposite

    $gameParty._items = deepclone(curItems)

    ???
     
    #3
  4. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    923
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    To copy the items, you'll want to do

    currentItems = MVC.deepClone($gameParty._items);currentWeapons = MVC.deepClone($gameParty._weapons);currentArmors = MVC.deepClone($gameParty._armors);To set them back on a retry, you'll probably want to deep clone them again, in case they retry more than once. This way, currentItems and such will stay as a complete copy of what the party had before the battle. In this case, it'd look like this:

    $gameParty._items = MVC.deepClone(currentItems);$gameParty._weapons = MVC.deepClone(currentWeapons);$gameParty._armors = MVC.deepClone(currentArmors);However, to make sure that the variables don't get deleted by the Garbage Collector, you might want to set them as properties to Game_Temp. This variable will be cleared each time the game is started, so the values won't linger between games, and you'll be pretty much guaranteed your variables will not get deleted.
     
    #4
  5. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    How do I set values to game temp in a plugin, so I don't have to change the base engine js files, I think I've messed with those too much already and would like to try and keep this all to plugins.

    Also this is what the common event I am running looks like: http://i.imgur.com/vDV62ja.png

    I am just calling that common event on party defeat in a .js script, pretty basic. Not sure if current items will pull or if I will get a variable not defined like that, still don't know all the ins and outs of js. Might I need to do some scoping to get this to work?
     
    Last edited by a moderator: Dec 4, 2015
    #5
  6. Zalerinian

    Zalerinian Jack of all Errors Veteran

    Messages:
    4,695
    Likes Received:
    923
    Location:
    The Internet.
    First Language:
    English
    Primarily Uses:
    N/A
    (function(){ var _Game_Temp_initialize = Game_Temp.prototype.initialize; Game_Temp.prototype.initialize = function() { _Game_Temp_initialize.call(this); this._partyItems = null; this._partyWeapons = null; this._partyArmors = null; }})();This will alias Game_Temp's initialize function, and then add the _partyItems, Weapons, and Armors variables to the class. I started their initial values at Null, because Game_Temp is created before the title screen, so no game is loaded at this point. But, once a game is active, you can get and set these values by saying $gameTemp._partyItems = MVC.deepClone($gameParty._items) and such.
     
    #6
    junglechief likes this.
  7. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    Makes sense. Hopefully I'll start to figure most of this stuff out eventually I hope. My object oriented programming background is sparse and none of the classes I took were in Java Script but I am slowly learning. RPG maker has been really educational. :) Thanks!

    Thanks again! Just got it working in game and tested it. All is well!
     
    Last edited by a moderator: Dec 4, 2015
    #7

Share This Page