im using an array to play random attack animations
how do i prevent the last animation from getting selected from the random code?

var n = randi() % (attackAnims.size())
myAnimation.play((attackAnims[n]),-1, fireAnimSpeed)
  • # at top of script
    var last_index = -1
    # in function
    var new_index = randi() % attackAnims.size()
    if new_index == last_index:
    	new_index = (new_index + 1) % attackAnims.size()
    last_index = new_index
    print("Animation: ", attackAnims[new_index])

If attackAnims has distinct values you can just select next available attack in case of duplication.

var n = randi() % (attackAnims.size())
if attackAnims[n] == myAnimation.current_animation:
    n = (n + 1) % attackAnims.size()
myAnimation.play((attackAnims[n]), -1, fireAnimSpeed)
  • DJM replied to this.

    pkowal1982
    that doesnt seem to work

    animations are declared in this way>
    var attackAnims = {0: "knifeAttack1",1: "knifeAttack2", 2: "knifeAttack3",3: "knifeAttack4"}

    this is what animations it prints out when using your code

      DJM
      It's not going to cycle that many times so you could just use a while loop:

      var lastAnim = 0 #put this at top
      var newAnim
      while newAnim != lastAnim:
           #newAnim = get random animation index
      lastAnim = newAnim
      play(newAnim)

      Not sure I got the syntax right, but you get the idea.

      • DJM replied to this.

        fire7side
        i dont want to use a while loop
        i should be able to do something like this?

        var n = randi() % (attackAnims.size() - 1) + 1
        myAnimation.play((attackAnims[n]), -1, fireAnimSpeed)
        	
        attackAnims[n] = attackAnims[0]
        attackAnims[0] = myAnimation.attackAnims[n]

        however , 'myAnimation.attackAnims[n]' isnt the correct syntax to assign an animation

        # at top of script
        var last_index = -1
        # in function
        var new_index = randi() % attackAnims.size()
        if new_index == last_index:
        	new_index = (new_index + 1) % attackAnims.size()
        last_index = new_index
        print("Animation: ", attackAnims[new_index])

          cybereality It seems like you would divide the larger number by the smaller to get a remainder, to me. I never use mods except for the odd and even thing, though. I don't really see how that works but I suppose it does somehow. I think I would go with size - index + 1.

          just curious , why can i do it this way with audiostreamsamples and not with animations?

          var n = randi() % (footstepsNormal.size() - 1) + 1
          self.stream= footstepsNormal[n]
          self.pitch_scale = random.randf_range(0.8, 1.2)
          if self.playing  == false:
          	self.play()
          	
          footstepsNormal[n] = footstepsNormal[0]
          footstepsNormal[0] = self.stream

          That code is not correct, though I guess it provides plausible results.

          So for an explanation, this line will not choose all sounds.

          var n = randi() % (footstepsNormal.size() - 1) + 1

          Assuming there are 6 sounds, you find a random number of the space of 6 - 1, which is 5, then add 1. So the randi() statement generates some huge random integer number, the modulus 5 then restricts it to the numbers 0, 1, 2, 3, 4 and then you add 1. Which means the value of n can only be 1, 2, 3, 4, or 5. Meaning it will never play the first sound.

          footstepsNormal[n] = footstepsNormal[0]
          footstepsNormal[0] = self.stream

          These 2 lines take the first sound in the array and swaps it with the index of the random sound that just played. This is not an error, but it does absolutely nothing. Since the first code snippet (above) chooses a random sample, the order of the array is irrelevant. So it just makes things confusing for no reason, and does not change the functionality of the code.

          Also, since the previous sample is not checked, that audio code can repeat the same sample any number of times. For footsteps, this is probably okay and you would never notice. However, it is theoretically possible that it plays the same sound 1,000 times in a row. Yes, highly unlikely, but entirely possible.

          • DJM replied to this.

            cybereality aha, i think i understand.
            ill use the same code from your animation example for the sounds too then. tnx