[SOLVED] Create a Random Object From a Group Based on a Picked Variable

Hello again, GDevelop community…
I do not usually ask for help unless I need it, but I’ve been asking pretty frequently lately, anyways.
I need help with creating a random object from a group based on a randomly picked variable.
image
I know I can use this to create a specific object from an object group, but how do I determine it? (What would I use to get a random result.)
Now I’m going to explain what I actually want in detail.
I want a specific weight of chance on each object.
There will be 9 objects in this one specific group.
Two will be common, one will be uncommon, two will be rare, two epic, one legendary, one mythic, each with lowering chances of rate adding to 100.
I’m planning on one pre-existing object having a min/max rate (e.g. 1000) to determine the object with RandomInRange(Object.MinRange,Object.MaxRange).
My problem being:
How do I give an object a certain range and apply it to the action above (say 1-100 for a 10% chance)?
I don’t want to use specific object conditions so I can easily make new groups and stuff, although I am fine with using separate variables to add to the object name action.

You are addressing here 2 things

Chance in %
And create object from group

Chance is nothing else than RandomInRange(1,
100)
Where more sense would be Random(100) since it skips 0 and now you have automatically range from 1 to 100

So you make variable called RNG
And you simply set RNG to Random(100)

Now you will need to prepare events like
Condition
Value of variable RNG is greater or equal than 1
Value of variable RNG is less or equal than 30

Action
Do something

And you just gave 30% chance for something to happen

So for each range from 1 to 100 in RNG you will need to make event to determine what should happen
Problem is that either you go with
1 to 30 that gives 30% chance
And for next thing you can go with 1 to 10 and that gives 10% chance
Problem is that if RNG will be below 11
Something from 1st and 2nd event will happen
Cause they are in same range

You would need
1st event
Value of variable RNG is greater or equal to 1
Value of variable RNG is less or equal to 30

2nd event
Value of variable RNG is greater than 30
Value of variable RNG is less or equal than 40

And now you covered range from 1 to 30 and 31 to 40
and they DO NOT overlap each other

So you need to plan ahead your ranges
Cause if you gonna shove in there 1 to 70
Then you are left with 30 possible chances for rest
Unless you are ok with ranges overlapping

Think of it like this
1st method
You have 30% to drop gold bar from zombie
And 10% chance to drop diamond from zombie

BUT if RNG = for example 8
Then zombie will drop gold bar AND diamond
Where if RNG = 11 you will get only gold bar

With 2nd method you would only get gold bar if RNG = 1 to 30
And diamond if RNG = 31 to 40

And it does not need to be 1-100
You could set RNG to 1-374
But then you will need to learn how to calculate % from number

100% / Max Range * Range you want to check
100 / 374 * 20 = 5% (more or less)

So in this case range of 20 numbers from 374 = 5%

And that range could be 1 to 20 or 140 to 160 or 354 to 374

Which i say its stupid cause you will just sink in calculations
I say range 1 to 100 is all you need

Now to create objects by that
Create array variable call it MyArray just for now so you can test it works
Later you can name it whatever

Add to it as many child vars (TEXT) as you have objects to spawn
Now you type name of each object in each child

And magic with array is that you can address them by INDEX which is their position on the list instead of their value

So if you have
0 apple
1 orange
2 banana
3 watermelon
4 grape
5 lemon

You don’t need to address name of fruit
You simply go with RandomInRange(0,5)

And in this case
Create object from group MyArray[RandomInRange(0.5)]

And how about combine it with what i wrote at beginning?

You make event
Value of variable RNG is greater or equal to 1
Value of variable RNG is less or equal to 30

Action change variable WhatToSpawn Set to 2

Create object from group MyArray[WhatToSpawn]

And so looking at your array
0 apple
1 orange
2 banana
3 watermelon
4 grape
5 lemon

You just created banana

1 Like

I would do something along these lines:

The card draw variable is a local one, I figure that is easiest.
You could also break this down more if you didn’t want the chance of opening a pack of 9 cards with nothing but commons.

EDIT: Looks like Zero got in before me with a much more in depth answer!
At a glance our ideas are similar, but follow their approach as it’s more extensive. :slight_smile:

2 Likes

Another approach is to weight the categories and then pick a random item from that group or that array. You could remove the item from the array if it’s a one-time item.

Pick a random number 1 to 100 or whatever
If <60 choose a random common object
If >= 60 < 80 choose a random rare object

And so on…

1 Like

Thanks for all of your input, guys! I did actually end up doing this a bit before any of you guys responded, @ZeroX4, @Mauii, still I’ll consider using your guys’ methods as well.

Here’s what I ended up doing:
Card name example:
image
Here’s what I ended up doing:


Find the pack the card is from, find the rarity of the card, and the number to discriminate from cards with data identical to that (the ID number).
Like if I had the same name and birthday as you I wouldn’t look the same, would I (rhetorical question)?