• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Random letter selection

S

Sawybean

Guest
Hello all,

I'm currently working on a game where the computer selects a random letter, and the player has to correctly enter that letter. The system I have currently is really unwieldy and doesn't work at the moment. Here's what I have:

Create:
Code:
randomize()

//set state to first stage
image_index = 1

//pick first letter
key = random_range(1,26)
keynumber = 0
keypress = "A"

//set image speed to 0
image_speed = 0
Step:
Code:
//print next letter
draw_text_color(40,50,key,c_black,c_black,c_black,c_black,1)
draw_text_color(40,40,keypress,c_black,c_black,c_black,c_black,1)

//scan for player input
keypress = keyboard_lastkey

//match keypress to number value
if keypress = "A" { keynumber = 1 }
if keypress = "B" keynumber = 2
if keypress = "C" keynumber = 3
if keypress = "D" keynumber = 4
if keypress = "E" keynumber = 5
if keypress = "F" keynumber = 6
if keypress = "G" keynumber = 7
if keypress = "H" keynumber = 8
if keypress = "I" keynumber = 9
if keypress = "J" keynumber = 10
if keypress = "K" keynumber = 11
if keypress = "L" keynumber = 12
if keypress = "M" keynumber = 13
if keypress = "N" keynumber = 14
if keypress = "O" keynumber = 15
if keypress = "P" keynumber = 16
if keypress = "Q" keynumber = 17
if keypress = "R" keynumber = 18
if keypress = "S" keynumber = 19
if keypress = "T" keynumber = 20
if keypress = "U" keynumber = 21
if keypress = "V" keynumber = 22
if keypress = "W" keynumber = 23
if keypress = "X" keynumber = 24
if keypress = "Y" keynumber = 25
if keypress = "Z" keynumber = 26

//check keypress with key needed to advance
if keypress = key && image_index < 6 {
    image_index++
} else if keypress != key && image_index > 0 {
    image_index--
}
Let me know if you guys have any ideas! And let me know if my explanation doesn't make much sense, I've been having a hard time describing my problem to people.
 
T

Taddio

Guest
Have you tried putting that in a ds_list, shuffling it, and then simply pick the index 0 inthe list? Looks like a lot less trouble and typing to me!
 
  • Like
Reactions: Rob

Rob

Member
You could also do this:

Code:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

letter_integer = irandom(25) + 1;

letter_string = string_char_at(alphabet, letter_integer);
There are plenty of string functions that also allow you to remove the random letter aftr its been chosen, if that's where you wanted to go with it etc.
 
Last edited:
T

Taddio

Guest
You could also do this:

Code:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

letter_integer = irandom(26) + 1;

letter_string = string_char_at(alphabet, letter_integer);
There are plenty of string functions that also allow you to remove the random letter aftr its been chosen, if that's where you wanted to go with it etc.
Geez man, this is like the riff to Smoke on the Water or even Whole Lotta Love. So simple, clever and effective. Wouldn't have thought of it that way for sure!
 

Rob

Member
Geez man, this is like the riff to Smoke on the Water or even Whole Lotta Love. So simple, clever and effective. Wouldn't have thought of it that way for sure!
It's only because I've been spending hours and hours trying to make my text engine work... my brain is full of it lol
 
S

Sawybean

Guest
You could also do this:

Code:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

letter_integer = irandom(25) + 1;

letter_string = string_char_at(alphabet, letter_integer);
There are plenty of string functions that also allow you to remove the random letter aftr its been chosen, if that's where you wanted to go with it etc.
This worked, thanks so much!!
 

TheouAegis

Member
And since I don't think anybody actually explained the biggest reason his code didn't work, random random_range returns fractions, but his code is checking for integers. That's why everyone was using irandom*.
 
Top