Custom script call [pick random from list]

Hello,

I would like to know how I would write out a custom script that does the following

[Pick one of the random following:
>>ShowVAnimOnSpriteset(001, "testanim1", 60, 60, true)
>>ShowVAnimOnSpriteset(002, "testanim2", 60, 60, true)
>>ShowVAnimOnSpriteset(003, "testanim3", 60, 60, true)
>>ShowVAnimOnSpriteset(004, "testanim4", 60, 60, true)
]

I know I can do this via Else/If cond branches in the database, but circumstances in my game would make it a lot easier if I could just make a script call directly doing the above. How to I do this?

Looking at some javascript threads I came up with this:

Code:
const anim01 = ShowVAnimOnSpriteset(001, "test_idle01", 60, 60, true)"
const anim02 = ShowVAnimOnSpriteset(002, "test_idle02", 60, 60, true)"
const anim03 = ShowVAnimOnSpriteset(003, "test_idle03", 60, 60, true)"
const anim04 = ShowVAnimOnSpriteset(004, "test_idle04", 60, 60, true)"

const test = ["anim01", "anim02", "anim03", "anim04"];

const random = Math.floor(Math.random() * test.length);
console.log(random, test[random]);-------------

But I am getting the error "Error: --SyntaxError: Invalid or unexpected token"
it depends on what you want to randomize in which way - you can replace the numbers with variables for example.

that said a warning: NEVER use leading zeroes for numbers in scripts. the numbers are 3, NOT 003 and so on.
that is because in scripts, leading zeroes change the base for the number, two leading zeroes mean that the number that follows is an octal number instead of a decimal number.
You have " at the end of each line, and call(args)"text" is invalid syntax, hence the error. Also, you're storing strings (i.e. text) in the array rather than the variables you just defined.

Try something like this instead?
JavaScript:
const anim01 = ShowVAnimOnSpriteset(001, "test_idle01", 60, 60, true);
const anim02 = ShowVAnimOnSpriteset(002, "test_idle02", 60, 60, true);
const anim03 = ShowVAnimOnSpriteset(003, "test_idle03", 60, 60, true);
const anim04 = ShowVAnimOnSpriteset(004, "test_idle04", 60, 60, true);

const test = [anim01, anim02, anim03, anim04];

const random = Math.randomInt(test.length);
console.log(random, test[random]);
I've used MZ's Math.randomInt function here, but it does exactly the same as what you were doing with Math.random.

As an aside: I would personally set it up to choose a random number and then create the corresponding object, rather than creating all objects up-front. Your approach should work, though~
You have " at the end of each line, and call(args)"text" is invalid syntax, hence the error. Also, you're storing strings (i.e. text) in the array rather than the variables you just defined.

Try something like this instead?
JavaScript:
const anim01 = ShowVAnimOnSpriteset(001, "test_idle01", 60, 60, true);
const anim02 = ShowVAnimOnSpriteset(002, "test_idle02", 60, 60, true);
const anim03 = ShowVAnimOnSpriteset(003, "test_idle03", 60, 60, true);
const anim04 = ShowVAnimOnSpriteset(004, "test_idle04", 60, 60, true);

const test = [anim01, anim02, anim03, anim04];

const random = Math.randomInt(test.length);
console.log(random, test[random]);
I've used MZ's Math.randomInt function here, but it does exactly the same as what you were doing with Math.random.

As an aside: I would personally set it up to choose a random number and then create the corresponding object, rather than creating all objects up-front. Your approach should work, though~


Thank you. I knew I had something wrong with the Syntax but I couldn't find it.

And you're right, since I've defined all my constants all the images I'm generating are created at the same time, instead of only picking the random one selected. How would I make it only pick the random one?

UPDATE

Alright well I got something that works the way I want using 'else if' scripts instead.

JavaScript:
const test = [1, 2, 3, 4];


const random = Math.randomInt(test.length);

console.log(random, test[random]);


if (random == 0) {

    ShowVAnimOnSpriteset(11, "testanim1", 60, 60, true)

} else if (random == 1) {

    ShowVAnimOnSpriteset(12, "testanim2", 60, 60, true)

} else if (random == 2) {

    ShowVAnimOnSpriteset(13, "testanim3", 60, 60, true)

} else if (random == 3) {

    ShowVAnimOnSpriteset(14, "testanim4", 60, 60, true)

} else {

    ShowVAnimOnSpriteset(11, "testanim1", 60, 60, true)

}

It's not very elegant, but I guess that's good enough for now...

If anyone has any comments on how to improve this I would greatly appreciate it.
Thank you. I knew I had something wrong with the Syntax but I couldn't find it.

And you're right, since I've defined all my constants all the images I'm generating are created at the same time, instead of only picking the random one selected. How would I make it only pick the random one?

UPDATE

Alright well I got something that works the way I want using 'else if' scripts instead.

JavaScript:
const test = [1, 2, 3, 4];


const random = Math.randomInt(test.length);

console.log(random, test[random]);


if (random == 0) {

    ShowVAnimOnSpriteset(11, "testanim1", 60, 60, true)

} else if (random == 1) {

    ShowVAnimOnSpriteset(12, "testanim2", 60, 60, true)

} else if (random == 2) {

    ShowVAnimOnSpriteset(13, "testanim3", 60, 60, true)

} else if (random == 3) {

    ShowVAnimOnSpriteset(14, "testanim4", 60, 60, true)

} else {

    ShowVAnimOnSpriteset(11, "testanim1", 60, 60, true)

}

It's not very elegant, but I guess that's good enough for now...

If anyone has any comments on how to improve this I would greatly appreciate it.
JavaScript:
const random = Math.randomInt(4) + 1;
ShowVAnimOnSpriteset(10 + random, "testanim" + random, 60, 60, true);
Ok so I tried to refine my result a bit further, trying to make it such that it will pick randomly excluding the previously picked random number, getting the base code from here:

JavaScript:
var random = getNumber();

const scalex = 0.40,
      scaley = 0.40,
      posx = 400,
      posy = 50,
      base = 10,
      duration = 0;
   
if (random === 0) {
    ShowVAnimOnSpriteset((base + 0), "anim_115", posx, posy, true)
    ScaleVAnim((base + random), scalex, scaley, duration)
    DeleteVAnim(base + 2)
    DeleteVAnim(base + 3)
    DeleteVAnim(base + 4)
} else if (random === 1) {
    ShowVAnimOnSpriteset((base + 1), "anim_225", posx, posy, true)
    ScaleVAnim((base + random), scalex, scaley, duration)
    DeleteVAnim(base + 1)
    DeleteVAnim(base + 3)
    DeleteVAnim(base + 4)
} else if (random === 2) {
    ShowVAnimOnSpriteset((base + 2), "anim_335", posx, posy, true)
    ScaleVAnim((base + random), scalex, scaley, duration)
    DeleteVAnim(base + 1)
    DeleteVAnim(base + 2)
    DeleteVAnim(base + 4)
} else { //if this.j == 3
    ShowVAnimOnSpriteset((base + 3), "anim_445", posx, posy, true)
    ScaleVAnim((base + random), scalex, scaley, duration)
    DeleteVAnim(base + 1)
    DeleteVAnim(base + 2)
    DeleteVAnim(base + 3)
}

console.log("random number: ", random);

function getNumber() {
    var min = 0,
        max = 4,
        i = Math.floor(Math.random() * (max - min)) + min;

console.log(`getNumber calls: i: ${i}, this.j: ${this.j}`);

  if (isNaN(this.j) || this.j != i) {
    this.j = i;
    return this.j
  } else {

    if (this.j === i) {

      if (i - 1 < min || i + 1 < max) {
        this.j = i + 1;
        return this.j
      }

      if (i + 1 >= max || i - 1 === min) {
        this.j = i - 1;
        return this.j
      }

      this.j = Math.random() < Math.random() ? --i : ++i;
      return this.j
    }
  }
}

However, the resulting image is only picked from random === 0, even though the console prints out the correct values. What am I doing wrong?

Of note, when moving my var random = getNumber() to the bottom of my script, it returns "undefined", which I suspect might be playing a role in why this isn't working?

EDIT

Holy **** I'm an idiot.
This code works flawlessly, I just messed up my deleteAnims completely and it prevented anything from showing up except the first condition branch.
My apologies...

This thread can be closed. It has been solved.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/custom-script-call-pick-random-from-list.150690/