Spawn/create family member in a rate

0 favourites
  • 7 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hi, I'm get stuck on how to do this: Spawn/create family member object random but in a certain rate.

    Here is an example to explain what I want to achieve:

    -Every monster drop a item when get killed.

    -the dropped item is random picked from a group/family.

    -each item has a drop rate, the higher rate has a higher chance to drop. 90 has 90% drop rate, 20 has 20% drop rate, etc.

  • Edited and bump

  • Random expression and the event "is in range" will be your friend here. These two features, not sure the name for the latter is correct, exist and you will use them here.

    For example, you could have a global variable "ran" to store a random value and have something like this for item dropping logc in your event sheet:

    set global variable "ran" to random(100)

    is "ran" in range 0 to 90 : drop item 1

    is "ran" in range 90 to 100 : drop item 2

    Here is an example of item dropping. Common item 1 has 90% chance of being dropped while the rare item 2 has only 10% chance of being dropped.

  • Thanks keroberos.

    this seems a good way to do it if I have certain kinds of items. But what if I want add more item types in the future? Do I need to change all the range value for each items?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Now, you are talking about workflow. ;)

    What I suggested you is a very simple idea, but in order to be efficient and flexible in the future, you will need something else.

    If I were you, I will store which enemy drops which item in 2 instance variables called "drop" and "chance". All enemies will be in one single family and that "droppingItem" family will have these 2 instance variables IN string. WHY in string?

    Because I will store it like this:

    drop = "None|Potion|Antidote"

    chance = "60|30|10"

    Now, when an enemy got killed, it's time to do some string parsing. First, get this instance variable. Parse the string using tokenat expression:

    okenat(src, index, separator)

    Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

    tokenat(drop, 0, "|") shall return "None".

    tokenat(drop, 1, "|") shall return "Potion".

    tokenat(drop, 2, "|") shall return "Antidote".

    tokenat(chance, 0, "|") shall return "60".

    tokenat(chance, 1, "|") shall return "30".

    tokenat(chance, 2, "|") shall return "10".

    Yes, you will put this in a loop and using loopindex instead of 0,1,2. (if you want more technical info here, ask the forums regarding the loop in C2)

    Now, you will want to check your random value against the chance value. Perhaps, with this:

    okencount(src, separator)

    Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.

    The pseudocode will be something like this: (if you never write a program before, just say so)

    ran = random(100);
    
    while ( tokencount(chance, "|") > loopindex){
      
      if ( int(tokenat(chance, loopindex, "|")) < ran)
        return tokenat(drop, loopindex, "|");
    
      loopindex = loopindex + 1; 
    }

    Now, with this here, you will get yourself one of the "None|Potion|Antidote". So how can you create an object type respectively? You need to use a plugin called "nickname" for this. Search for it in the forums. It allows you to create object via string.

    This sounds complicated, but this is one possibility.

  • And one more thing, I prefer to have all the numbers to be in 100 range. That way, it's more like 0-100% to me. Sure, mathematician might slap at me for this, but hey, this chance implementation thing is entirely up to you.

  • But still, I am not sure whether this is overkill for you or not.

    The good thing is, if you implement an event sheet for this, all you need to do, is simply relate new object types to string with nickname plugin when you got a new item type and just modify "chance" and "drop" variables as you wish, for the rest of your project.

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