Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

[Solved] Math random function for Pawn drops?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Solved] Math random function for Pawn drops?

    Solved it! Thanks for the help guys!

    Hello,

    I'm trying to create a function where a random number will be generated. If the generated number equals to the number set, it should execute a spawn function and thus dropping an item from the killed Pawn.

    So far I created the Pawn which drops the item upon death, but I now want it to drop the item with a certain percentage.

    In ActionScript 3.0, this was fairly easy to do so. However in UnrealScript, I have yet to find a well explained Math Random tutorial. I've been searching for a while now, but without success.

    I want the function to do the following;

    Generate a number ranging from zero to three (0-3); four numbers.
    If the number is zero (0) for example, it should execute my 'spawn()'.

    If anyone could help me out with this Math Random function, it would be greatly appreciated.

  • #2
    Rand(3);
    or
    RandRange(this, that);

    if (Rand(3) == 0)
    {
    //do whatever
    }

    Comment


    • #3
      Originally posted by tegleg View Post
      Rand(3);
      or
      RandRange(this, that);

      if (Rand(3) == 0)
      {
      //do whatever
      }
      Thank you a lot. This gets me going again!


      EDIT:

      One problem now, though. It executes the function just even more random than I 'programmed' it.

      Here are a few testlogs;

      [0019.16] ScriptLog: Zombie died!
      [0019.16] ScriptLog: random number= 1
      [0019.17] ScriptLog: Drop that juicy potion!
      [0019.17] ScriptLog: EXP: 100
      [0021.97] ScriptLog: Zombie died!
      [0021.97] ScriptLog: random number= 2
      [0021.97] ScriptLog: Drop that juicy potion!
      [0021.97] ScriptLog: EXP: 200

      [0023.80] ScriptLog: Zombie died!
      [0023.80] ScriptLog: random number= 3
      [0023.80] ScriptLog: EXP: 100
      [0025.06] ScriptLog: Zombie died!
      [0025.06] ScriptLog: random number= 1
      [0025.06] ScriptLog: Drop that juicy potion!
      [0025.06] ScriptLog: EXP: 200

      [0018.45] ScriptLog: Zombie died!
      [0018.45] ScriptLog: random number= 1
      [0018.45] ScriptLog: EXP: 100
      [0020.97] ScriptLog: Zombie died!
      [0020.97] ScriptLog: random number= 0
      [0020.97] ScriptLog: Drop that juicy potion!
      [0020.97] ScriptLog: EXP: 200
      Code;

      Code:
      function bool Died(Controller Killer, class<DamageType> DamageType, vector HitLocation) {
      	Rand(4);
      	`log("Zombie died!");
      	`log("random number=" @Rand(4));
      
      	if (Rand(4) == 1) {
      		spawn(class'MyHealthVial'/*, , , spawnlocation*/);
      		`log("Drop that juicy potion!");
      	}
      
      	super.Died(Killer, DamageType, HitLocation);
      	return TRUE;
      It doesn't care wether the number is correct or not, it just executes it at random will.

      Comment


      • #4
        Hi,
        Try to clean up the unnecessary random generations, that may be messing you up. Try pasting this in(made it only use one Rand(4) call):
        Code:
        function bool Died(Controller Killer, class<DamageType> DamageType, vector HitLocation) {
        	local int TempRandNum;
        	TempRandNum = Rand(4);
        	`log("Zombie died!");
        	`log("random number= "$TempRandNum);
        
        	if (TempRandNum == 1) {
        		spawn(class'MyHealthVial'/*, , , spawnlocation*/);
        		`log("Drop that juicy potion!");
        	}
        
        	super.Died(Killer, DamageType, HitLocation);
        	return TRUE;
        }
        Random number generation can be quite odd at times, I've found that the less times you have to call it, the better, and the more reliable your results will be.
        Hope this helps,
        LordOfOats.

        EDIT: Thanks to Spoof for the explanation.

        Comment


        • #5
          Originally posted by ty4dl View Post
          Code:
          function bool Died(Controller Killer, class<DamageType> DamageType, vector HitLocation) {
              Rand(4);
              `log("Zombie died!");
              `log("random number=" @Rand(4));
          
              if (Rand(4) == 1) {
                  spawn(class'MyHealthVial'/*, , , spawnlocation*/);
                  `log("Drop that juicy potion!");
              }
          
              super.Died(Killer, DamageType, HitLocation);
              return TRUE;
          LordOfOats answered it, but just to clarify...

          Each call to Rand(4) in the above code returns a new random value, not the same one. In any situation like this you should call Rand once to determine the value, assign it to a local variable and use that within your logic.

          Comment


          • #6
            Originally posted by Spoof View Post
            LordOfOats answered it, but just to clarify...

            Each call to Rand(4) in the above code returns a new random value, not the same one. In any situation like this you should call Rand once to determine the value, assign it to a local variable and use that within your logic.
            You are right. I totally forgot that it obviously generates a random number each time the Died function is executed. Thanks for the clarification, though.

            Comment


            • #7
              If you have an array of classes you wish your player to drop you can call rand within the square brackets like so:

              Code:
              RandClass = MyClassList[rand(MyClassList.length)];
              Hope this helps

              Comment


              • #8
                Originally posted by MonsOlympus View Post
                If you have an array of classes you wish your player to drop you can call rand within the square brackets like so:

                Code:
                RandClass = MyClassList[rand(MyClassList.length)];
                Hope this helps
                I was trying to get a 25% drop chance and with this formula it does give that, I guess.

                Comment


                • #9
                  I just thought Id show you because lets say you have a 1 in 4 (25% chance) of dropping 1 of 4 items this method saves you alot of code since youre not generating more random numbers then needed and also excess checks. Lets say for example you have more than just health vials you have armour, health and a potion, each with different chances and if you want to drop a 50 armour only if you dont drop health. This random class allows you to isolate one of the 3 and decide if its a chance to drop, does that make some sense? I know this is solved but I thought I would go into alittle more detail for fun.

                  Comment

                  Working...
                  X