Godot Version 4.3
I’m trying to make a game similar to Simon in Godot, where a directional input shows up on screen and then you have to press the right inputs.
Here is my script, but it only shows the first two directional inputs, and nothing happens if you press the right input.
extends Node
var DIRO = 0
var DIRT = 0
var DIRTH = 0
var DIRF = 0
var memory = 0
var gameTime = false
var dirInput = 100
func _ready() -> void:
$Graphics.play("Remember")
$TimerLonger.start()
$Directions.play("Gone")
dirInput = 100
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Input.is_action_just_pressed("Up") and gameTime == true:
dirInput = 1
if Input.is_action_just_pressed("Down") and gameTime == true:
dirInput = 2
if Input.is_action_just_pressed("Right") and gameTime == true:
dirInput = 3
if Input.is_action_just_pressed("Left") and gameTime == true:
dirInput = 4
if dirInput == DIRO:
$Hit.play()
print("It worked")
func memory_game_one():
$Graphics.play("Gone")
var dirOne = RandomNumberGenerator.new()
var random_dir = dirOne.randf_range(1, 4)
DIRO = int(random_dir)
var directionOne = str(DIRO)
memory = 2
$Directions.play(directionOne)
func memory_game_two():
var dirTwo = RandomNumberGenerator.new()
var random_dirTwo = dirTwo.randf_range(1, 4)
DIRT = int(random_dirTwo)
var directionTwo = str(DIRT)
memory = 3
$Directions.play(directionTwo)
func memory_game_three():
var dirThree = RandomNumberGenerator.new()
var random_dirThree = dirThree.randf_range(1, 4)
DIRTH = int(random_dirThree)
var directionThree = str(DIRTH)
memory = 4
$Directions.play(directionThree)
func memory_game_four():
var dirFour = RandomNumberGenerator.new()
var random_dirFour = dirFour.randf_range(1, 4)
DIRF = int(random_dirFour)
var directionFour = str(DIRF)
memory = 0
$Directions.play(directionFour)
func _on_timer_longer_timeout() -> void:
memory_game_one()
func _on_directions_animation_finished() -> void:
if memory == 2:
memory_game_two()
if memory == 3:
memory_game_three()
if memory == 4:
memory_game_four()
The “Simon Says” toy has four LEDs in different colors and four buttons that each correspond to one LED color. When playing the game, the toy will first turn on a sequence of LEDs, and then waits for the player to press the corresponding buttons in the same sequence that the LEDs turned on. Your code (or at least, the sample you posted) does not appear to do the latter.
When _on_timer_longer_timeout
fires, memory_game_one
is called. The first “LED” to light up is chosen, and is assigned to DIRO
. The player can press one of four buttons, and if it matches DIR0
, something is printed to the console to indicate success. So far, so good*.
I presume that child node Directions
is ultimately responsible for calling _on_directions_animation_finished()
. When that happens, memory_game_two
is called. A new “LED” to light up is chosen. The player can still input buttons. However, _process
will eventually hit this line of code:
if dirInput == DIRO:
The LED of the second game is not checked here. What is checked is whether the player pressed the same button as during game one. So for that reason, unless memory_game_two
selected the same “LED” to light up as the first game, nothing will be printed to the console.
* There are a number of other issues that prevent this code from playing like an actual “Simon Says” game:
- Simon and the player do not take turns. What I mean is,
_process
is called every single frame. There’s nothing to stop the player from pressing buttons all the time.
- In the “Simon Says” toy, the sequence of buttons to press increments every time the player inputs a correct sequence. The sequence of button presses in round n+1 will be the same as in round n, plus one new button press at the end of the sequence. This code mever increments the sequence.
- It’s very unlikely the player will ever have to press the ‘left’ button, because of these lines of code:
dirOne.randf_range(1, 4)
DIRO = int(random_dir)
This selects a random float in the range of 1 to 4, then casts the result to an integer. Casting is not the same as rounding. If the random number is 3.999, then the cast doesn’t round up to 4. Instead, it truncates to 3. The player will only ever have to press the 'left` button if the random number that is rolled is exactly a four.
How do i get the randomizer to round? And the reason that the inputs make no sense and the game doesn’t play like Simon is because I’m still trying to get the directions to show up first; only the first two show up. And the input I do have is just to test, the test does not work though; nothing happens when pressed.
Instead of rounding a float, you can request an integer directly by means of randi_range
.
2 Likes
Oh that makes sense lol. I think I’ve used randi range before, but i never realized what the i and f stood for.
I have tried some new game ideas similar to this and have stopped working on this code, so if anyone replies, know that I probably don’t need help for this anymore.