Let's explore a digital game mechanic...LUCK
01 Build a Random Number Generator
Just like in desktop games, luck can be a powerful game mechanic in all types of digital games. Use this to make things happen randomly, like rolling a die. Check out steps below to create a random number generator (RNG) with us below.
-
Load GDevelop and Create a New Project
- Open GDevelop and click on "Create a New Project"
- Choose "Empty Project" to start from scratch
- PRO TIP - keep seperate tabs open to jump back and forth between steps and GDevelop
-
Add a Text Object to display the random number
- Click on the "Objects" tab on the right and then click "+ Add a new object"
- Choose "Text" from the list of object types
- Name your text object, such as RandomNumberText, and adjust the text size and font as necessary
- Drag and drop the text object onto your scene to place it
-
Add Instructions Text Object
- Click on the "Objects" tab on the right and then click "+ Add a new object"
- Choose "Text" from the list of object types
- Name your instructions text object, such as "InstructionText", and adjust the text size and font as necessary
- Set the initial value to "Press Spacebar to roll D6"
- Drag and drop the text object onto your scene to place it
-
Create an Event to Generate a Random Number
- Switch to the Events tab at the top of the screen to begin adding logic to your game
- Click on "Add a new event" and select the event to trigger the random number generation. Let's use a "Key Press" event to trigger the action.
- In the Conditions section, click on "Add a condition"
- Search for Key Pressed and select it
- Set the key to "Space"
- In the Actions section of the same event, click on "Add an Action."
- Select "RandomNumberText" from the Objects tab
- Select "Text" in the actions menu
- In the text field, write the expression: ToString(RandomInRange(1, 6))
- When the spacebar is pressed this will generate a random number between 1 and 6 and convert it to text format so that it can be displayed as text in the "RandomNumberText" Object
-
Preview the Scene and Test
- Click on the play icon to preview the scene
- When you press the spacebar, the text object should update to display a new random d6 roll each time
-
Add a Roll D100 Button
- Click on the "Objects" tab on the right and then click "+ Add a new object"
- Choose "Button" from the list of object types
- Name your Button something like "RollD100Button" and adjust the text size and font as necessary
- Set the initial text to "Roll D100"
- Drag and drop the text object onto your scene to place it
-
Create an Event to Roll D100
- Switch to the Events tab at the top of the screen to begin adding logic to your game
- Click on "Add a new event" and select the event to trigger the random number generation. Let's use a "Button Press" event to trigger the action.
- In the Conditions section, click on "Add a condition"
- Select the "RollD100Button" Object in the Objects tab
- Select the "is pressed" action in the middle conditions menu and click ok to save the condition
- In the Actions section of the same event, click on "Add an Action."
- Select "RandomNumberText" from the Objects tab (We will use the same text to show the number)
- Select "Text" in the actions menu
- In the text field, write the expression: ToString(RandomInRange(1, 100))
- When the spacebar is pressed this will generate a random number between 1 and 100 and convert it to text format so that it can be displayed as text in the "RandomNumberText" Object
-
Fine-Tuning & Expansion
- You can change the range of the random number by adjusting the parameters in RandomInRange(1,6)
- You can also trigger the random number generation with other events, like clicking a button or after a certain amount of time passes