I How do i change chances of Random?

0 favourites
  • 6 posts
From the Asset Store
Simplistic hyper-casual game with nature elements. Tap to switch between the 4 elements and reach a better score.
  • Imagine I have 4 types of enemies in my game and I spawn them at random:

    NewEnemyType=Choose("zombie","vampire","goblin","troll")

    In this case each enemy has equal chances to spawn, lets say 100 probability points.

    zombie - 100 probability points

    vampire - 100 probability points

    goblin - 100 probability points

    troll - 100 probability points

    Now depending on player's progress in my game I want to dynamically change spawning chances of enemies. On early levels it could be:

    zombie - 300

    vampire - 150

    goblin - 100

    troll - 0

    (Notice, that I don't need chances of all enemies to add up to 400, I just want to change relative probabilities to each other).

    Later in the game it could be like this:

    zombie - 30

    vampire - 60

    goblin - 170

    troll - 200

    So how do I do this?

    Currently I'm using an algorithm which is quite complex.

    I put all probabilities into an array:
    Array=[30,60,170,200]
    
    Then I calculate the total:
    TotalProbability=460
    
    Then I generate a random number from this total:
    R=Random(TotalProbability)
    
    Then I iterate the array, searching which array member is "hit" by R:
    For i=0 to 3
     set x=x+Array.At(i)
     if R<=x then // "i" is our monster type
     Function.Call("SpawnMonster", i) 
    

    Graphically it looks like this:

    The whole thing is a bit messy and complex, I need to iterate the array several times.

    Is there a better solution? Maybe a formula or a built-in functionality or a plugin for this?

  • Just set a variable to random(0,10) before spawning.

    Then compare the variable and spawn the enemy based on that. For example in early game you can have this:

    Variable is between 0-6 spawn zombie

    Variable is between 6-9 spawn vampire

    Variable is between 9-10 spawn goblin

    And then just change that based on progress.

    But there are tons of simple ways of doing it. You don't need a complex algorithm that's for sure

  • Just set a variable to random(0,10) before spawning.

    Variable is between 0-6 spawn zombie

    Variable is between 6-9 spawn vampire

    Variable is between 9-10 spawn goblin

    This approach is simple and straightforward and yes it will work for a small number of enemies and fixed odds of spawning.

    However imagine that I have 30 different types of enemies, and I need to constantly increase/decrease spawning chances for different enemies depending on various factors (game difficulty, player progress, number of already spawned enemies of this type etc.)

    With your method I will need to create hundreds of different conditions.

    So unfortunately, I need a more universal solution.

    Another example is my game Doptrix (see link below). It's based on Tetris, but the gameplay is completely different and for better gaming experience I had to tweak chances of appearance for different tetromino shapes. For example square shape spawns a little more often than the L-shape. And in different game modes these tweaks are different.

    Also, when the player is in trouble and about to lose the game, I sometimes may want to "cheat" the random generator in their favor - temporary increase chances of good shapes. I don't actually do this in Doptrix, but many game developers use this trick in their games.

  • Store each levels spawn rates for each enemy in a 2D array? With X being level and Y being monster type and spawn value

    So if you think of Y0 = Zombie, Y1 = Vampire, Y2 = goblin, Y3 = troll.

    The actual array just keeps numbers, but they need to be INVERSE percentages.

    So if you have:

      0 % spawn rate, store 100 80 %, store 20 10%, store 90

    This is because we will do a GREATER THAN check.

    Then you generate a random number between 0-100.

    You know what level you're at so you just loop for Array.Height, checking Random > Array.Value

    If true, spawn a mob that corresponds to that level of the array.

    If you always want to spawn something, you'll need a default spawn at the end of your array that has 0.

    This was top of my head, and completely un tested but in theory could work... right?

    You can then also dynamically change the spawn rate as you go, just update the array.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I've created something general here in 2 events without using an array, although your array idea seems reasonable. https://www.dropbox.com/s/irmajka5z3ihj ... .capx?dl=0

    You add more enemies to the game, you don't need to change anything. The only extra logic required is how and when you change the min and max probability values in the game, but yeah you said they are based on various things so I assume this will be some kind of calculation.

  • KeeghanM, Unfortunately, your method will not work.. First, the values in the array will need to be sorted in descending order.

    And then what will happens if there are 2 values like this?

    Goblin 79%, store 21

    Vampire 80%, store 20

    When looping through the array, Goblin will be checked first and will have 79% chance of spawning. And the vampire will only have 20% chance.

    This will work if the array is sorted and all percentages for all enemies add up to 100%, but I'm trying to avoid this, because (see below)

    , I like that your example doesn't use arrays, however it has a serious limitation - min-max values for different enemies should not overlap and should cover the entire 0-100 range.

    This makes the job of changing the min and max probability values in the game quite difficult. I can't just decide "lets spawn zombies twice more often!", I will need to re-calculate ranges for all other enemies.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)