Pages

Thursday, March 15, 2012

A Simple Probability Class for UnrealScript

*NOTE (May 2013):  The code in this example works, but is a pretty inefficient method to accomplish this. For a better method, check the post titled Using Probability in UDK-based Role-Playing Games


I needed something a bit more in-depth than the ability to generate a random number for my game, so I wrote a simple class that will return a value depending on the percent chance of a random number being within a certain range.

I'm using it to support a character skill based action system with chances of success, failure, critical success, and critical failure, and semi-random events that are based on skills and actions. Since this kind of system is extensively used in AI programming, it could also have some applications in game AI.

The function used in this class takes two parameters: an optional array of integers that contains the percentages that will be compared against (state space), and an optional integer that changes the range the random number that is generated is in.

Code:


In your GameInfo Class:


These first two examples are exec functions so you can try them as console commands. The third ties into ProcessInstantHit() in a weapon class, but can be adapted for other ways of dealing damage, as well.

Examples:
1. Coin Toss Simulator
In anything extending PlayerController:


2. Roll a Dice
Another one for PlayerController due to ClientMessage()
This is just an example, it may or may not be more or less accurate than Rand(6) :)


3. Critical Damage for Weapons (InstantHit)
In your weapon class: (I'm extending from UDKWeapon, modify as you see fit)
Note: I'm only including the functions that are required for this to work.
Note2: TimeWeaponEquipping() is a good place to set up any changes to WeapDamageChances based on stats.

*Edit: Fixed a misspelled variable name... it should work now.

No comments:

Post a Comment