Luck

Luck

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.

 

  1. Load GDevelop and Create a New Project

    1. Open GDevelop and click on "Create a New Project"
    2. Choose "Empty Project" to start from scratch
    3. PRO TIP - keep seperate tabs open to jump back and forth between steps and GDevelop
  2. Add a Text Object to display the random number

    1. Click on the "Objects" tab on the right and then click "+ Add a new object"
    2. Choose "Text" from the list of object types
    3. Name your text object, such as RandomNumberText, and adjust the text size and font as necessary
    4. Drag and drop the text object onto your scene to place it
  3. Add Instructions Text Object

    1. Click on the "Objects" tab on the right and then click "+ Add a new object"
    2. Choose "Text" from the list of object types
    3. Name your instructions text object, such as "InstructionText", and adjust the text size and font as necessary
    4. Set the initial value to "Press Spacebar to roll D6"
    5. Drag and drop the text object onto your scene to place it
  4. Create an Event to Generate a Random Number 

    1. Switch to the Events tab at the top of the screen to begin adding logic to your game
    2. 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.
    3. In the Conditions section, click on "Add a condition"
    4. Search for Key Pressed and select it
    5. Set the key to "Space"
    6. In the Actions section of the same event, click on "Add an Action."
    7. Select "RandomNumberText" from the Objects tab
    8. Select "Text" in the actions menu 
    9. In the text field, write the expression: ToString(RandomInRange(1, 6))
    10. 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
  5. Preview the Scene and Test

    1. Click on the play icon to preview the scene
    2. When you press the spacebar, the text object should update to display a new random d6 roll each time
  6. Add a Roll D100 Button

    1. Click on the "Objects" tab on the right and then click "+ Add a new object"
    2. Choose "Button" from the list of object types
    3. Name your Button something like "RollD100Button" and adjust the text size and font as necessary
    4. Set the initial text to "Roll D100"
    5. Drag and drop the text object onto your scene to place it
  7. Create an Event to Roll D100

    1. Switch to the Events tab at the top of the screen to begin adding logic to your game
    2. 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.
    3. In the Conditions section, click on "Add a condition"
    4. Select the "RollD100Button" Object in the Objects tab
    5. Select the "is pressed" action in the middle conditions menu and click ok to save the condition
    6. In the Actions section of the same event, click on "Add an Action."
    7. Select "RandomNumberText" from the Objects tab (We will use the same text to show the number)
    8. Select "Text" in the actions menu 
    9. In the text field, write the expression: ToString(RandomInRange(1, 100))
    10. 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
  8. Fine-Tuning & Expansion

    1. You can change the range of the random number by adjusting the parameters in RandomInRange(1,6)
    2. You can also trigger the random number generation with other events, like clicking a button or after a certain amount of time passes