Pick a random element from an array

● ARCHIVED · READ-ONLY
Started by jjleroy 12 posts View original ↗
  1. hhhhhhhhhhhhhhh I just don't get it.

    I want a random battle sound to play, but it depends on which actors are in the team (and alive).

    So for example if I have Son Goku, Pikachu and Princess Peach in the team but Pikachu is dead, then either Son Goku's sound effect of Peach's would be playing. This is what I have:

    Code:
    allout = new Array();
    
    if ($gameActors.actor(2).isBattleMember() && $gameActors.actor(1).isStateAffected(1) == false) {allout.push("1");}
    if ($gameActors.actor(2).isBattleMember() && $gameActors.actor(2).isStateAffected(1) == false) {allout.push("2");}
    if ($gameActors.actor(3).isBattleMember() && $gameActors.actor(3).isStateAffected(1) == false) {allout.push("3");}
      
    actorToUse = allout[Math.floor(Math.random() * allout.length)];
    
    if (actorToUse == 1) {BattleSoundManager.playSe(allout_1_1)} 
    if (actorToUse == 2) {BattleSoundManager.playSe(allout_2_1)}
    if (actorToUse == 3) {BattleSoundManager.playSe(allout_3_1)}

    I get a lot of errors saying: "actorToUse is not defined" and "allout is not defined" ;_;
  2. you need to define variables before you use them. if you're starting out it's probably best to use var.

    Code:
    var allOut = [];
    // ...etc
  3. jjleroy said:
    Code:
    if ($gameActors.actor(2).isBattleMember() && $gameActors.actor(1).isStateAffected(1) == false) {allout.push("1");}
    if ($gameActors.actor(2).isBattleMember() && $gameActors.actor(2).isStateAffected(1) == false) {allout.push("2");} 
    if ($gameActors.actor(3).isBattleMember() && $gameActors.actor(3).isStateAffected(1) == false) {allout.push("3");}

    oh lord, if only we had an optimized structure to deal with repetitive operations!
    .............right?
  4. @aethon Thank you but that didn't change anything, it is still undefined...
    Is it because I use the script event in RPG maker and not a plugin? with multiple script events because the window is so small ;_;

    @gstv87 No more arrays!!!!arrays are my enemies!!!!!!!!!
  5. Well, it's probably not lying to you. Did you make sure to define every variable you use in your script? ...without a console log I'm not really going to be able to help much.

    Also arrays are important, you're going to have to get used to them if you want to start programming more seriously.
  6. @aethon Ok I apologize, arrays are friendly and beautiful, but please help me




    F1TQBxN.jpg
    undefined.jpg
  7. ...it still looks like a variable assignment issue to me. Could you post the script you're trying to run?
  8. yes, this is the full version:

    Code:
    var allout = [];
    
    if ($gameActors.actor(1).isStateAffected(1) == false) {allout.push("1");} 
    if ($gameActors.actor(2).isBattleMember() && $gameActors.actor(2).isStateAffected(1) == false) {allout.push("2");} 
    if ($gameActors.actor(3).isBattleMember() && $gameActors.actor(3).isStateAffected(1) == false) {allout.push("3");} 
    if ($gameActors.actor(4).isBattleMember() && $gameActors.actor(4).isStateAffected(1) == false) {allout.push("4");} 
    if ($gameActors.actor(5).isBattleMember() && $gameActors.actor(5).isStateAffected(1) == false) {allout.push("5");} 
    if ($gameActors.actor(6).isBattleMember() && $gameActors.actor(6).isStateAffected(1) == false) {allout.push("6");} 
    if ($gameActors.actor(8).isBattleMember() && $gameActors.actor(8).isStateAffected(1) == false) {allout.push("8");} 
    if ($gameActors.actor(9).isBattleMember() && $gameActors.actor(9).isStateAffected(1) == false) {allout.push("9");} 
       
    actorToUse = allout[Math.floor(Math.random() * allout.length)];
    if (actorToUse == 1) {
        var random = Math.floor(Math.random() * 2) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_1_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_1_2)}}
           
    if (actorToUse == 2) {BattleSoundManager.playSe(allout_1_1)}
    
    if (actorToUse == 3) {
    var random = Math.floor(Math.random() * 3) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_3_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_3_2)}
            if (random == 3) {BattleSoundManager.playSe(allout_3_3)}}
    
    if (actorToUse == 4) {
        BattleSoundManager.playSe(allout_4_1)}
    
    if (actorToUse == 5) {
    var random = Math.floor(Math.random() * 3) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_5_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_5_2)}
            if (random == 3) {BattleSoundManager.playSe(allout_5_3)}}
    
    if (actorToUse == 6) {
    var random = Math.floor(Math.random() * 2) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_6_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_6_2)}}
    
    if (actorToUse == 8) {
    var random = Math.floor(Math.random() * 3) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_8_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_8_2)}
            if (random == 3) {BattleSoundManager.playSe(allout_8_3)}}
    
    if (actorToUse == 9) {
    var random = Math.floor(Math.random() * 2) + 1;
            if (random == 1) {BattleSoundManager.playSe(allout_9_1)}
            if (random == 2) {BattleSoundManager.playSe(allout_9_2)}}
  9. jjleroy said:
    Code:
    actorToUse = allout[Math.floor(Math.random() * allout.length)];

    actorToUse needs to be defined with "var" as well.
  10. Well, there are a few issues.
    1. you're not defining your actorToUse variable.
    2. you're pushing strings onto the array and looking for numbers in it later on
    3. you've got a bunch of sound effects that don't have references anywhere.
    I'd recommend using a text editor with linting or autocomplete, like https://code.visualstudio.com.
  11. @Aloe Guvner @aethon

    hhhhhhhhhhhhhhhhhhh it works now!!!!!!!! they said it!!!!!!!!!!!!! I'M SO HAPPY you developer people are like Jesus to me
    thank you thank youthank youthank youthank youthank youthank youthank youthank you
  12. jjleroy said:
    No more arrays!!!!arrays are my enemies!!!!!!!!!

    I was talking about loops!
    Code:
    for (id=1; id < $gameParty.members.length; id++){
     if  ($gameActors.actor(id).isBattleMember() && $gameActors.actor(id).isStateAffected(1) == false) {allout.push(id.prototype.toString())};
    }

    .......you get the idea.