Meta as variable, and related JS questions

Discussion in 'Learning Javascript' started by Llareian, Mar 4, 2017.

  1. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,396
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Hi guys. This will be my first post to Learning Javascript. I think this goes here and not in Maker Specific/RM MV because I'm asking about things dealing with the underlying code and using script calls. Please let me know if I have it in the wrong place!

    My question(s) deal with attempting to use a meta value as a variable. I've been watching SoulPour777's awesome JS tutorials and I'm excited to see how easy adding a meta value is. I'd like to be able to use meta values to create variables for actors.

    I realize it is easier to implement using $gameVariables, but this has several drawbacks, the most important to me being that if I'm not sure how many actors I'll have when starting out the project or I change my mind on the number, I will wind up either with a bunch of wasted spaces in unused variables, have to reorder my variables (which could be a major pain), or have the variables out of order. I also realize there are plugins that effectively solve the overarching goal, but I'm trying to learn more about JS and how the game code works, and I find it informative to try to find alternate solutions by using the script.

    So question #1 is, is it possible to vary the value of a meta key during the game?

    I've attempted to solve this myself by calling up "Number($dataActors[n].meta.keyName)" in an event>script and modifying the value. It works great!...right up until you save the game and load it, at which point the meta value reverts to the default in the database.

    Question #2 stems from this. I think that I've come to understand that the JSON files are just there to give templates for the game objects, and the actual in-game values are stored elsewhere. For example, the actors' active stats are actually stored in $gameActors and it's only their initialize templates that are stored in $dataActors. I'd like to verify if I have this right, because I'd hate to proceed with false information on something this basic.

    Going back to Q#1, I've tried to call up the meta value using $gameActors, $gameActor, and $gameParty...at best(?) I can get to a TypeError "Cannot read property 'keyName' of undefined" (when using "alert($gameParty._actors[1].meta.keyName);") and at worst I run into "undefined is not a function" errors because apparently I'm not clear on how to call actors. Is it just Not Possible to change the value of meta without first storing it as a gameVariable or creating a plugin? Or do I just need to learn more about how to call up an actor's current information?
     
    #1
  2. Linard

    Linard Veteran Veteran

    Messages:
    202
    Likes Received:
    17
    First Language:
    Filipino
    Primarily Uses:
    RMMV
    I'm not really sure but this is my experienced on JavaScript.

    1.) Yes??, if and only if you saved that meta value to a specific variable (you can also save it from the $gameVariables).
    Code:
    var alliasGS = Game_System.prototype.initialize;
    Game_System.prototype.initialize = function (){
      alliasGS.call(this);
      this.myActorAge = Number($dataActors[1].meta.age);
    }

    So i've place the meta data on the myActorAge and you can edit the value anytime even saving or load it.
    via script call.
    Code:
    $gameSystem.myActorAge ++;
    
    I don't know if there's a function that calls out the meta data, aside from $dataActors[1].meta
    or if you want to call for that you can make a function that returns the value of the meta data.

    Code:
    Game_Actor.prototype.metaCharacterAge = function (actorId){
      return $dataActors[age].meta.age;
    }

    On script call you can do like this:
    Code:
    $gameActor.metaCharacterAge(1);
    
    Note: this example2 will not help you to change the value of the meta data..

    2.) Yes?? because we have the same thought for that one hahaha.
     
    #2
    Llareian likes this.
  3. Trihan

    Trihan Speedy Scripter Veteran

    Messages:
    1,449
    Likes Received:
    955
    Location:
    Buckie, Scotland
    First Language:
    English
    1. Yes, you can change the meta values for actors during the game. But you would have to somehow add the changed values to the data that gets stored during a save game, or as you've rightly noticed it'll revert to the default when you load again.

    2. Pretty much, yeah. All of the initial data is drawn from the database values on startup, and then the associated $game variables are populated with the retrieved data. These are saved, so if you can get the meta data into a new property (for example, if you have an "age" property via notetag for actors, modify Game_Actor's prototype initialize function to set this._age to the meta value. This will be saved in the $gameActors array, and can then be changed during gameplay and still referenced with scripts).
     
    #3
    Llareian likes this.
  4. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,396
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    #4
  5. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,547
    Likes Received:
    1,292
    Location:
    Canada / Qu├ębec
    First Language:
    French
    Primarily Uses:
    RMMV
    You can also add this example in new blank plugin
    Code:
    alias_MySetup_initialize = DataManager.setupNewGame;
    DataManager.setupNewGame = function() {
        alias_MySetup_initialize.call(this);
        // HERE ALL SETUP I WANT WHEN GAME START AND SAVED IF LOADING
        // $dataActors OBJETS ARE SAVED
        //ACTOR1 NEW SETUP
         $dataActors[1]['AGE'] = 18;
         $dataActors[1]['SEXE'] = 'MALE'
         //ACTOR2 NEW SETUP
         $dataActors[2]['AGE'] = 20;
         $dataActors[2]['SEXE'] = 'MALE';
    }
         //FUNCTION INDIVIDUAL FOR ACTOR IF NEED
         //Example add age
         $dataActors[1]['AgeAdd'] = function(Value){
             if(Value){ this['AGE']+=Value; }else{ this['AGE']++; } }
    Is the easy way to set property in the object actor , will saved.
    In this example, in you console Test [F8} you can try
    Code:
    $dataActors[1]['AgeAdd'](); // increase only +1;
    $dataActors[1]['AgeAdd'](5);//Increase +=5;
    
    //or this is same method call but diferente way.
    $dataActors[1].AgeAdd(); // increase only +1;
    $dataActors[1].AgeAdd(7); //Increase +=7;
    You can also ADD what you want for new proprety in all game objet need.
    Its no make bug at start, because it initially only when you start new game
    i dont know if this can help to found a good way.
     
    Last edited: Mar 5, 2017
    #5
    Llareian likes this.

Share This Page