0 votes

Lettertile.gd code:

extends Node2D
class_name LetterTile

# Letter sprites by Kenney.nl
# https://opengameart.org/content/letter-tiles

var current_tile: int
var letter_value = ""
var letter_points = 0
var box_position: Vector2
signal tile_clicked

func _ready():
    $HiddenTimer.wait_time = rand_range(0.0, 0.5)
    current_tile = randi() % 26 # Chooses a number between 0-25. I want that at least 3 of the 16 times executed a vowel it's generated (10, 13, 15, 16 and 19). 
    letter_value = get_letter_value(current_tile)
    letter_points = get_points_value(current_tile)
    $Tiles.frame = current_tile

Main.gd code:

func spawn_new_letters():
    for i in range(4):
        for j in range(4):
            var tile:LetterTile = Letter.instance()
            # warning-ignore:return_value_discarded
            tile.connect("tile_clicked", self, "on_tile_clicked", [tile])
            tile.position = Vector2(i * TILESIZE, j * TILESIZE) + Vector2(4 * i, 4 * j)
            tile.box_position = tile.position

enter image description here

in Engine by (122 points)
edited by

1 Answer

+1 vote
Best answer

Currently, when you instance a new letter, it's free to randomly select any of the 26 items in your alphabet. Since there's (currently) no way for the letter generation to be aware of what has / has not already been generated, it's not possible to get the necessary mix of consonants and vowels.

Instead of letting LetterTile randomly decide what letter it represents, I'd change the code so that Main tells LetterTile what letter it will be. That simplifies the problem, because then Main can be in charge of the mix of consonants and vowels as it can have access to all of the chosen letters at the same time.

Regarding how to get Main to choose between consonants and vowels, there are lots of ways. One easy way might be to store the two letter types separately from each other. Then, you could choose from the set of vowels how ever many times you want, and then choose the remainder of the letters from the set of consonants.

by (21,800 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.