Hi everybody,
I'm having some trouble figuring out how to create a set of weighted probabilities easily in the event code. Let me outline what I have:
if(EntityA is overlapping Trigger)
Set EntityA.ExecuteSub = true; // this is so EntityA only runs the overlap code a single time.
System.Create(EntityA)
Set EntityA Animation (Choose("Animation1", "Animation2", etc))
Set EntityA size
Set EntityA Position
Set EntityA localvar1
Set EntityA localvar2
Set EntityA localvar3
Set EntityA localvar4
Set EntityA Speed
{ ... more instructions involving EntityA }
I have ten different animations in Entity A and it randomly selects one each time. However, now I want to weight the probability so certain animations are more likely to appear than others.
I planned on setting a global variable called RandomNumber, and than basically being like:
if(RandomNumber < 10) Choose (EntityA.Animation1)
else if(RandomNumber < 25) Choose (EntityA.Animation2)
else if(RandomNumber < 30) Choose (EntityA.Animation3)
I have a feeling I'm going to have to create a new global boolean variable called RandomNumberMatched and then:
0. Check primary condition overlapping
1. Generate random number
2. Spawn EntityA - and hope that the following subevents automatically refers to this one
3. Write a bunch of Subevents under the primary event checking the Random number value as follows:
Sub event:
if (RandomNumber < 10)
RandomNumberMatched = true
EntityA.SetAnimation( EntityA.Animation1 )
if (RandomNumber < 25)
RandomNumberMatched = true
EntityA.SetAnimation( EntityA.Animation2 )
if (RandomNumber < 30)
RandomNumberMatched = true
EntityA.SetAnimation( EntityA.Animation3 )
4. Rerun the rest of the set size,position,etc etc on this EntityA *AFTER* the subevent code has finished I guess putting the entirety of this code in a blank sub-event on the primary event.
Without an if-elseif-else condition I have to use an additional variable to falsify the following subevents.
I really hope there's a more modular way to encapsulate what would ordinarily be a fairly and I would think common thing to do in conventional OO coding.
I wish I could just write a function
GetWeightedString( ProbabilityArray, StringArray )
which takes a list of numbers up to 100 returns the respective String in the array, and then I could just call a simple function that would Spawn based on the Object's name.
Something like this:
Spawn randomentity = GetWeightedString(new Array[2, 4, 8, 20, 25, 60, 80, 100], new String[EntityA, EntityB, EntityC, EntityD, EntityE, EntityF, EntityG, EntityH])
Thanks everybody for your assistance!
-- cacotigon