I've hit a wall, Script Call with Array?

● ARCHIVED · READ-ONLY
Started by Fuchsilein 6 posts View original ↗
  1. Hi there!

    I'm trying to build a Deck logic for my game idea
    the following code work on my test fine but doesnt work as an script call in MV, can anyone say me why or what i have to change?

    var drawArray = [0, 1];
    for (i = 0; i < $gameVariables.value(1); ) {
    var x = Math.floor((Math.random() * ($gameVariables.value(6) - $gameVariables.value(5) + 1) + $gameVariables.value(5)));
    if (drawArray.includes(x)) {
    }
    else {
    drawArray.push(x);
    $gameVariables.setValue(8, x);
    show_actor_command($gameVariables.value(3), "use_skill", $gameVariables.value(8));
    i++
    }
    }
  2. What error are you getting? What's happening, or not happening?

    Shouldn't there be an i++ on the second line?

    When posting code, it's better to use code tags than quote tags - all of your indenting is lost, making it harder to read, and it will also often cause certain character strings to be converted to emoticons.
  3. Hi thanks for the info,

    After some trial and error it turns out that MV doesnt support the .includes() - Array function.

    So instead of
    Code:
    if (drawArray.includes(x)) {}
    I just had to do
    Code:
    if (drawArray.indexOf(x) >= 0) {}
  4. How could it work on your test then? Or were you testing in a different engine (that would be good info to include in the first post).
  5. Tested on a compiler, kinda forgot to include that info :(
  6. Fuchsilein said:
    Hi thanks for the info,

    After some trial and error it turns out that MV doesnt support the .includes() - Array function.

    So instead of
    Code:
    if (drawArray.includes(x)) {}
    I just had to do
    Code:
    if (drawArray.indexOf(x) >= 0) {}
    yep it normal depending the version you use.
    You have several choices.
    1: make a core plugin of you project, and add all polyfill from es6 (you can find it on google)
    2: update to rmmv 1.6, with the new node nwjs that use new chromium with es6.

    in your example polyfill of include are
    polyfill
    PHP:
    // https://tc39.github.io/ecma262/#sec-array.prototype.includes
    if (!Array.prototype.includes) {
      Object.defineProperty(Array.prototype, 'includes', {
        value: function(searchElement, fromIndex) {
    
          // 1. Let O be ? ToObject(this value).
          if (this == null) {
            throw new TypeError('"this" is null or not defined');
          }
    
          var o = Object(this);
    
          // 2. Let len be ? ToLength(? Get(O, "length")).
          var len = o.length >>> 0;
    
          // 3. If len is 0, return false.
          if (len === 0) {
            return false;
          }
    
          // 4. Let n be ? ToInteger(fromIndex).
          //    (If fromIndex is undefined, this step produces the value 0.)
          var n = fromIndex | 0;
    
          // 5. If n ≥ 0, then
          //  a. Let k be n.
          // 6. Else n < 0,
          //  a. Let k be len + n.
          //  b. If k < 0, let k be 0.
          var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
    
          // 7. Repeat, while k < len
          while (k < len) {
            // a. Let elementK be the result of ? Get(O, ! ToString(k)).
            // b. If SameValueZero(searchElement, elementK) is true, return true.
            // NOTE: === provides the correct "SameValueZero" comparison needed here.
            // except for NaN because NaN === NaN returns false
            if (o[k] === searchElement || isNaN(o[k]) && isNaN(searchElement)) {
              return true;
            }
            // c. Increase k by 1.
            k++;
          }
    
          // 8. Return false
          return false;
        }
      });
    }
    and you can add it to your core plugin as function include(){//polyfill}

    also recomended you to use formate you loop like this for keep good performance and save some ms
    for (i = 0, len = $gameVariables.value(1); i <len; i++ ) {

    because the other way, length are recalculate at each iteration !