# Variable Array to create dynamic Random Event

I am just learning GDevelop and one thing I struggle with is the concept of array variables.

Here is what I want the system to do:

On Click of a button, the system will generate a random number from 0-100 and return a text string to a player.

Arrays use 0, so I need to account for that.

Imagine that the strings are:

1. Gold Bar
2. Silver Bar
3. Bronze Bar
4. Iron Bar
4-100: Return nothing to the player (no text/no object)

On 0-3 an instance of the appropropiate object goes into their inventory as true. They can only have one of each object at a time.

Let’s say this time the button was pressed and the player got a 1 and the system gave them a silver bar.

Now, there is no silver bar.

Is there a dynamic way without creating every possible variation of an array, so that I can have the system write logic to change the array?

1. Gold Bar
1.Bronze Bar
2. Iron Bar
3-100: Return nothing to the player (no text/no object)

Can I do something like that? if so, how.

The practical application of this for me will be much more advanced, but once I learn the basics, I can do it.

Basically, imagine a game where the player is a King and I want to determine the type of Courtiers that attend him at court to settle a matter. If I have a unique event where this can only happen once, will want to dynamically reshuffle the array so that that event is no longer possible, and possibly other events are MORE likely now.

I will eventually design the game, so that certain types of events are more likely to occur, or not occur at all based on condiitonal logic. IE: if the King follows the “Old Ways” versus the “New Gods” I might make an event not trigger at all, and another one more likely.

Learning to create a dynamic array is essential (I think).

What you’re asking is to remove variable by index. This will take out the element/child at a particular index and shuffle everything after it up one spot to fill the void:

Let’s talk about arrays and some randomness.
https://github.com/doug13579/gdevelop-ramdom-item-from-array-example

These snippets use the following scene variables.( I don’t think I used the variable “item” I was going to store the item in a text but decided to just add it to a text object)

I used a different button and different text object for each example. They’re paired. Button1 and NewText1 and so on.

Let’s start at the beginning. To pick a random item from the array named list.

This uses a variable to store a random number which is used to pick an item which is then removed.
It’s always a good idea to check the size of the array otherwise GD will create items if an index doesn’t exist.

This will pick a random item and also refills the array if the array is empty.

This is the same as the last except instead of manually adding the items, it uses a copy of the array. It converts the array to the JSON format and converts the JSON to the List variable

This picks a number form 0 to 10, if the number is larger than the size of the array, it displays “nothing” with the random number. Otherwise, it shows the string n the array.

This is used to simply show the contents of the array list at the beginning and when any button is clicked. The buttons are all in ButtonGroup

To add a bit of weighting, you could add the metal multiple times.
Like “bronze” 10 times, “silver” 5 times and “gold” only once

Or you could use less than and greater than to create ranges.
It’s late and this isn’t in the example on my GitHub page.
This version doesn’t use an array

There are infinite ways of doing things. I’m fairly certain these are stable but like everything, it should be tested. An even more advanced way would be to build an array off another array using a structure with the criteria or using an array of structures to hold the range numbers and cycle thru the array.

I hope this gives you a starting part.