Parameter Structs

Discussion in 'Learning Javascript' started by Rachnera, Feb 23, 2019.

Thread Status:
Not open for further replies.
  1. Rachnera

    Rachnera Caffeinated Spider Veteran

    Messages:
    174
    Likes Received:
    47
    First Language:
    English
    Primarily Uses:
    RMMV
    I can't seem to wrap my head around how these work in RPG Maker MV. I notice some plugins have a struct like feature where if you click them a menu pops up. I have several sound effects that need to be setup in my plugin. But sound objects have volume, pitch, and pan as well. I which to have something like below. for efficiency purposes. But I don't know how to define structs as far as params are concerned.

    Code:
    /*:
    * @param OpenSound
    * > Name
    * > Volume
    * > Pitch
    * > Pan
    *@param CloseSound
    * ditto
    *@param PageSound
    * ditto
    */
    Could someone explain the syntax for the parameters to me. specifically the structs.
     
    #1
  2. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,340
    Likes Received:
    8,095
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
  3. Rachnera

    Rachnera Caffeinated Spider Veteran

    Messages:
    174
    Likes Received:
    47
    First Language:
    English
    Primarily Uses:
    RMMV
    Code:
    //=============================================================================
    // Manor Mouse Map System
    // MM_MapSystem.js
    //=============================================================================
    
    var Imported = Imported || {};
    Imported.MM_MapSystem = true;
    
    var Rachnera = Rachnera || {};
    Rachnera.MapSystem = Rachnera.MapSystem || {};
    
    //=============================================================================
    /*:
     * @plugindesc Handles the map system.
     * @author Rachnera
     *
     * @param Images
     * @default
    
     * @param Floor 1 Overlay
     * @parent Images
     * @type file
     * @dir img/pictures/
     * @require 1
     * @desc This is the filename of your map base. Do not include an extension.
     * @default floor1overlay
    
     * @param Floor 2 Overlay
     * @parent Images
     * @type file
     * @dir img/pictures/
     * @require 1
     * @desc This is the filename of your map base. Do not include an extension.
     * @default floor2overlay
    
     * @param Floor 3 Overlay
     * @parent Images
     * @type file
     * @dir img/pictures/
     * @require 1
     * @desc This is the filename of your map base. Do not include an extension.
     * @default floor3overlay
    
     * @param Outside Overlay
     * @parent Images
     * @type file
     * @dir img/pictures/
     * @require 1
     * @desc This is the filename of your map base. Do not include an extension.
     * @default outsideoverlay
    
     * @param MapOverlay Picture Number
     * @min 1
     * @max 100
     * @desc This is picture number that is used for displaying the map overlays.
     * @default 52
    
     * @param Map Icon
     * @desc This is the icon to display the players position on the map.
     * @default 15
     *
     * @param Open Sound
     * @type struct<Sound>
     * @default {"Sound":"book2","Volume":90,"Number":"1","Pan":"0"}*
    
     * @param Close Sound
     * @type struct<Sound>
     * @default {"Sound":"book2","Volume":90,"Number":"1","Pan":"0"}
     *
     * @param Page Sound
     * @type struct<Sound>
     * @default {"Sound":"book2","Volume":90,"Number":"1","Pan":"0"}
     *
    */
    
    /*~struct~Sound:
     * @param name
     * @type file
     * @dir audio/se/
     * @require 1
    
     * @param volume
     * @type number
     * @min 1
     * @max 100
     * @default 90
     *
     * @param pitch
     * @type number
     * @decimals 2
     * @min 0.5
     * @max 1.5
     * @default 1
     *
     * @param pan
     * @type number
     * @min -100
     * @max 100
     * @default 0
    */
    //=============================================================================
    
    //=============================================================================
    //Parameters
    //=============================================================================
    
    Rachnera.Parameters = PluginManager.parameters('MM_MapSystem');
    Rachnera.Param = Rachnera.Param || {};
    
    Rachnera.Param.MapOverlay1 = String(Rachnera.Parameters['Floor 1 Overlay']);
    Rachnera.Param.MapOverlay2 = String(Rachnera.Parameters['Floor 2 Overlay']);
    Rachnera.Param.MapOverlay3 = String(Rachnera.Parameters['Floor 3 Overlay']);
    Rachnera.Param.MapOverlay4 = String(Rachnera.Parameters['Outside Overlay']);
    Rachnera.Param.MapPicture = Number(Rachnera.Parameters['MapOverlay Picture Number']);
    Rachnera.Param.MapIcon = Number(Rachnera.Parameters['Map Icon']);
    Rachnera.Param.OpenSound = Json(Rachnera.Parameters['OpenSound'])['Structure'];
    Rachnera.Param.CloseSound = Json(Rachnera.Parameters['CloseSound'])['Structure'];
    Rachnera.Param.PageSound = Json(Rachnera.Parameters['PageSound'])['Structure'];
    
    //=============================================================================
    //Data Manager
    //=============================================================================
    
    Rachnera.MapSystem.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
    DataManager.isDatabaseLoaded = function() {
      if (!Rachnera.MapSystem.DataManager_isDatabaseLoaded.call(this)) return false;
      return true;
    };
    
    DataManager.processMapSystemNotetags = function() {
      if (!$dataMap) return;
    
      $dataMap.mapOverlay = 0;
      $dataMap.mapIconX = 0;
      $dataMap.mapIconY = 0;
    
      if (!$dataMap.note) return;
      var notedata = $dataMap.note.split(/[\r\n]+/);
      for (var i = 0; i < notedata.length; i++) {
        var line = notedata[i];
        if (line.match(/<(?:map):[ ](\d+)[ ](\d+)[ ](\d+)>/i)) {
          $dataMap.mapOverlay = parseInt(RegExp.$1).clamp(0,3);
          $dataMap.mapIconX = parseInt(RegExp.$2);
          $dataMap.mapIconY = parseInt(RegExp.$3);
        }
      }
    };
    
    //=============================================================================
    // Scene_Map
    //=============================================================================
    
    Scene_Map.prototype.callMapDisplay = function() {
        var se = {
          name:   JSON.parse(Rachnera.Param.OpenSound['name']),
          volume: JSON.parse(Rachnera.Param.OpenSound['volume']),
          pitch:  JSON.parse(Rachnera.Param.OpenSound['pitch']),
          pan:    JSON.parse(Rachnera.Param.OpenSound['pan'])
        };
        AudioManager.playSe(se);
        SceneManager.push(Scene_MapDisplay);
        $gameTemp.clearDestination();
        this._waitCount = 2;
    };
    
    //=============================================================================
    // Scene_mapdisplay
    //=============================================================================
    
    function Scene_MapDisplay() {
        this.initialize.apply(this, arguments);
    }
    
    Scene_MapDisplay.prototype = Object.create(Scene_MapDisplayBase.prototype);
    Scene_MapDisplay.prototype.constructor = Scene_MapDisplay;
    
    Scene_MapDisplay.prototype.initialize = function() {
        Scene_Base.prototype.initialize.call(this);
    };
    
    Scene_MapDisplay.prototype.create = function() {
        Scene_Base.prototype.create.call(this);
        this.createMap();
    };
    
    Scene_MapDisplay.prototype.start = function() {
        Scene_MapDisplayBase.prototype.start.call(this);
        this._statusWindow.refresh();
    };
    
    Scene_MapDisplay.prototype.createMap = function() {
        var overlays = [
          Rachnera.Param.MapOverlay1,
          Rachnera.Param.MapOverlay2,
          Rachnera.Param.MapOverlay3,
          Rachnera.Param.MapOverlay4
        ];
        $gameVariables.setValue(20,$dataMap.mapOverlay);
        $gameScreen.showPicture(100, overlays[$gameVariables.value(20), 0, 0, 0, 100, 100, 255, 0);
    };
    
    Scene_MapDisplay.prototype.update = function() {
      var currentMap = $gameVariables.value(20);
      var closesound = {
        name:   JSON.parse(Rachnera.Param.PageSound['name']),
        volume: JSON.parse(Rachnera.Param.PageSound['volume']),
        pitch:  JSON.parse(Rachnera.Param.PageSound['pitch']),
        pan:    JSON.parse(Rachnera.Param.PageSound['pan'])
      };
      var pagesound = {
        name:   JSON.parse(Rachnera.Param.CloseSound['name']),
        volume: JSON.parse(Rachnera.Param.CloseSound['volume']),
        pitch:  JSON.parse(Rachnera.Param.CloseSound['pitch']),
        pan:    JSON.parse(Rachnera.Param.CloseSound['pan'])
      };
      if(Input.isPressed('left')) {
        AudioManager.playSe(pagesound);
        currentMap = (currentMap-1);
        if (currentMap < 0) currentMap = 3;
        $gameVariables.setValue(20,currentMap);
        $gameScreen.showPicture(100, overlays[$gameVariables.value(20), 0, 0, 0, 100, 100, 255, 0);
      }
      else if(Input.isPressed('right')) {
        AudioManager.playSe(pagesound);
        currentMap = (currentMap+1)%4;
        $gameVariables.setValue(20,currentMap);
        $gameScreen.showPicture(100, overlays[$gameVariables.value(20), 0, 0, 0, 100, 100, 255, 0);
      }
      else if(Input.isPressed('escape')) {
        AudioManager.playSe(closesound);
        this.popScene.bind(this));
      }
      else {
        if (currentMap === $dataMap.mapOverlay) ;
        $windowBase.drawIcon(Rachnera.Param.MapIcon,$dataMap.mapIconX, $dataMap.mapIconY);
      }
    };

    So It's not working. I think I got the struct right. But how do I parse them into parameters i can access?
     
    #3
  4. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,340
    Likes Received:
    8,095
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    The answer would be pretty much in every plugin that has some parameters.
    PluginManager.parameters(plugin filename without extension)
     
    #4
  5. caethyril

    caethyril ^_^ Veteran

    Messages:
    531
    Likes Received:
    304
    Location:
    UK
    First Language:
    English
    Primarily Uses:
    RMMV
    Mostly correct! All parameters are retrieved in string format; JSON.parse (case-sensitive) doesn't just convert to primitive types (number, boolean, etc), it also extracts {objects} and arrays[]. You've tried to extract your parameters using Json (invalid) rather than JSON.parse, easy fix:
    Code:
    Rachnera.Param.OpenSound = JSON.parse(Rachnera.Parameters['OpenSound']);
    Rachnera.Param.CloseSound = JSON.parse(Rachnera.Parameters['CloseSound']);
    Rachnera.Param.PageSound = JSON.parse(Rachnera.Parameters['PageSound']);
    I notice you parsed the values OK later on to change the property type from string -> number where appropriate. ^_^

    Here's a link to the JSON.parse article on MDN, in case you find it helpful:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
     
    #5
    Rachnera and Engr. Adiktuzmiko like this.
  6. mlogan

    mlogan Global Moderators Global Mod

    Messages:
    13,260
    Likes Received:
    7,323
    Location:
    Texas
    First Language:
    English
    Primarily Uses:
    RMMV

    Closed at OP's request.

     
    #6
Thread Status:
Not open for further replies.

Share This Page