Help with randomize script

Discussion in 'RPG Maker MV' started by junglechief, Jan 23, 2018.

  1. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    So looking around online I pieced together a script that should be able to assign a random non-repeating value to a variable from a pool, it looks like this:
    var towns = [1, 2, 3, 4, 5];
    var townVar = [town1,town2,town3,town4,town5]
    var getNumber = function () {
    for (i=0,i < townVar.length, i++){
    var townsIndex = Math.floor(towns.length * Math.random());
    var townVar = towns.splice(townsIndex, 1);
    }
    };

    I am trying to figure out how to call this when the game start, either from inside an event or just as part of the function. Trying to randomly assign town locations from a pool. This should work, I plan to use conditionals that check each of the numbers assigned to the variables and the appropriate location transfer underneath each conditional in an event. Thanks in advanced.
     
    #1
  2. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Code:
    function getNumber () {
    var towns = [1, 2, 3, 4, 5];
    var townVar = [town1,town2,town3,town4,town5]
    for (i=0,i < townVar.length, i++){
    var townsIndex = Math.floor(towns.length * Math.random());
    var townVar = towns.splice(townsIndex, 1);
    }
    };
    Create a plugin from that. Then simply inside an event call getNumber using the Script field.
     
    #2
  3. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    awesome thanks!
     
    #3
  4. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    Okay one more question, sorry. I did this by adding the script: getNumber();

    when I try to run the game I get an error that says getNumber is undefined. It does not appear to be finding the function, my file is included in the plugin manager.

    I guess I should be more specific. I use an events advanced > script box and just added getNumber(); to it and when I started the game I got a black screen with getNumber not defined. I don't know the correct syntax for calling this function from a plugin. The plugin is a .js file with only that specific code in it.
     
    Last edited: Jan 23, 2018
    #4
  5. Frogboy

    Frogboy I'm not weak to fire Veteran

    Messages:
    1,676
    Likes Received:
    2,100
    Location:
    North Carolina, U.S.
    First Language:
    English
    Primarily Uses:
    RMMV
    I did something similar with with randomizing towns and such. Just curious, why do you need a script for this? I just evented it at the start of the game and stored all of the locations into variables. Then when you enter the map, transfer them to assigned spots. I don't see why a custom script is needed here.
     
    #5
  6. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    I'm planning to randomize more than just towns and a script is cleaner and easier to edit/fix. The wall of conditionals for the event solution is pretty poor for things like scale-ability. If I can get this working for one pool of variables I can make it work for many more.
     
    #6
  7. junglechief

    junglechief Veteran Veteran

    Messages:
    46
    Likes Received:
    3
    First Language:
    English
    Okay I figured it out. For those coming here looking for a good way to randomize a pool of variables via a script. I am running the script on game start, hence why I am calling it in a script that uses the setupNewGame function. This should be able to easily work with any set of variables.

    (function() {
    DataManager.setupNewGame = function() {
    this.createGameObjects();
    this.randomizeTowns();
    this.selectSavefileForNewGame();
    $gameParty.setupStartingMembers();
    $gamePlayer.reserveTransfer($dataSystem.startMapId,
    $dataSystem.startX, $dataSystem.startY);
    Graphics.frameCount = 0;
    };

    DataManager.setupEventTest = function() {
    this.createGameObjects();
    this.randomizeTowns();
    this.selectSavefileForNewGame();
    $gameParty.setupStartingMembers();
    $gamePlayer.reserveTransfer(-1, 8, 6);
    $gamePlayer.setTransparent(false);
    };

    DataManager.randomizeTowns = function() {
    var towns = [1, 2, 3, 4, 5];
    var townVar = new Array(5);
    var townsIndex = 0;

    for (i=0;i < townVar.length; i++){
    townsIndex = Math.floor(towns.length * Math.random());
    townVar = towns.splice(townsIndex - 1, 1);
    $gameVariables.setValue(i+2,townVar);
    }
    };

    })();
     
    #7

Share This Page