I am working in godot and this is my first project i decided on making a piano game like on those virtual piano games where you can play on your keyboard and i have everything working except when i want to play the black note its shift + the white note which its parent from so for example i have c2 as 1 and i want to play c#2 its should be shift + 1 but instead it plays c2
extends Node
# Define the note keymap for left and right hands
var left_hand = ['Z', 'S', 'X', 'D', 'C', 'V', 'G', 'B', 'H', 'N', 'J', 'M']
var right_hand = ['R', '5', 'T', '6', 'Y', 'U', '8', 'I', '9', 'O', '0', 'P']
# Define the piano notes
var piano_notes = ['C2', 'D2', 'E2', 'F2', 'G2','A2', 'B2', 'C3', 'D3', 'E3', 'F3', 'G3','A3', 'B3', 'C4',
'D4', 'E4', 'F4', 'G4','A4', 'B4', 'C5', 'D5', 'E5', 'F5', 'G5','A5', 'B5', 'C6', 'D6', 'E6', 'F6', 'F6#', 'G6', 'G6#',
'A6', 'B6', 'C7']
# Define white and black notes separately
var white_notes = ['C2', 'D2', 'E2', 'F2', 'G2',
'A2', 'B2', 'C3', 'D3', 'E3', 'F3', 'G3',
'A3', 'B3', 'C4', 'D4', 'E4', 'F4', 'G4',
'A4', 'B4', 'C5', 'D5', 'E5', 'F5', 'G5',
'A5', 'B5', 'C6', 'D6', 'E6', 'F6', 'G6',
'A6', 'B6', 'C7']
var black_notes = ['C#2', 'D#2', 'F#2', 'G#2',
'A#2', 'C#3', 'D#3', 'F#3', 'G#3',
'A#3', 'C#4', 'D#4', 'F#4', 'G#4',
'A#4', 'C#5', 'D#5', 'F#5', 'G#5',
'A#5', 'C#6', 'D#6', 'F#6', 'G#6',
'A#6', 'C#7']
# Combine white and black notes
var all_notes = white_notes + black_notes
# Dictionary to keep track of whether a key is pressed
var key_pressed = {}
func _ready():
# Initialize key_pressed dictionary
for note in piano_notes:
key_pressed[note] = false
# Connect input event
set_process_input(true)
func _process(delta):
# Check if a keybind is pressed for each note
for note in piano_notes:
var action = "play_note_" + note
if Input.is_action_pressed(action) and !key_pressed[note]:
play_note(note)
key_pressed[note] = true
elif !Input.is_action_pressed(action) and key_pressed[note]:
key_pressed[note] = false
func play_note(note):
# Load the audio file
var audio_stream = load("res://notes/" + note + ".wav")
if audio_stream:
# Create an AudioStreamPlayer node
var audio_player = AudioStreamPlayer.new()
# Assign the audio stream to the player
audio_player.stream = audio_stream
# Add the player to the scene
add_child(audio_player)
# Play the audio
audio_player.play()
I went and added comments to all of the code for you guys (or at least ask chat gpt to explain everything and add comments)
Also i get 4000 errors in my debug log saying:
E 0:00:02:0041 Piano.gd:45 @ _process(): The InputMap action "play_note_F6#" doesn't exist. Did you mean "play_note_F6"?
<C++ Error> Condition "!InputMap::get_singleton()->has_action(p_action)" is true. Returning: false
<C++ Source> core/input/input.cpp:288 @ is_action_pressed()
<Stack Trace> Piano.gd:45 @ _process()
like that for every key
I tried to add a Boolean variable that checked if shift is pressed then plays the input name + “#” + note but that made a double hash tag
JuanPlayz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.