Spawn/create family member in a rate

Get help using Construct 2

Post » Fri May 10, 2013 11:45 am

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.
porky2013-05-11 22:59:51
B
14
S
2
G
1
Posts: 94
Reputation: 2,089

Post » Sat May 11, 2013 11:00 pm

Edited and bump
B
14
S
2
G
1
Posts: 94
Reputation: 2,089

Post » Sun May 12, 2013 5:54 am

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
24
S
9
G
11
Posts: 409
Reputation: 8,042

Post » Sun May 12, 2013 10:04 am

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?
B
14
S
2
G
1
Posts: 94
Reputation: 2,089

Post » Sun May 12, 2013 11:34 am

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:

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

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:

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

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

[code]

ran = random(100);

while ( tokencount(chance, "|") > loopindex){

if ( int(tokenat(chance, loopindex, "|")) < ran)
    return tokenat(drop, loopindex, "|");

loopindex = loopindex + 1;
}[/code]

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
24
S
9
G
11
Posts: 409
Reputation: 8,042

Post » Sun May 12, 2013 11:36 am

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
24
S
9
G
11
Posts: 409
Reputation: 8,042

Post » Sun May 12, 2013 11:43 am

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
24
S
9
G
11
Posts: 409
Reputation: 8,042


Return to How do I....?

Who is online

Users browsing this forum: Renfd, supreme676 and 27 guests