Hi everyone,
I'm new to Godot and trying my first project - not a game, but a guitar chord viewer...
So far I have layouted my main page which is a tab in a tab container containing a grid container. The grid is 4 (columns) x5 (rows). For layout purposes I just put my fretboard images inside and labels before. So it looks like this:
label - fretboard | label - fretboard
label - fretboard and so on
The labels are static, but I want to design a scene containing the fretboard. I already have that including four sprites for the fingers. I also have a function in MainNode that reacts on an OptionButton where I can change the chord. On that button I have a function that sends the new chord:
func changed():
var key = tonart_button.get_item_text(tonart_button.get_item_id(tonart_button.selected))
print ("Key: " + key)
This works and whenever I change the key, I get the print message with the correct key.
The fretboard scene is inside a texture rectangle so it fits into the grid container. This should be replaced whenever a new key is selected.
- fretboardTextureRect
- Finger1
- Finger2
- Finger3
- Finger4
My question is now: how can I replace the placeholder fretboard in the layout with the correct instance showing the four sprites for the fingers (there may be less than four, so I would put visibility on false).
This function is called, but the error is: "Invalid set index 'position' (on base: 'Nil') with value of type 'Vector2i'
func show_chord(chord: String):
match chord:
"C":
finger_1.position = Vector2i(93,62)
finger_2.position = Vector2i(240, 124)
finger_3.position = Vector2i(378, 156)
finger_4.visible = false
"D":
finger_1.position = Vector2i(240,92)
finger_2.position = Vector2i(240, 30)
finger_3.position = Vector2i(378, 62)
finger_4.visible = false
I tried something like this, but in Godot 4.2 it doesn't work (script on the button below the print that I showed previously):
var fretboard = load("res://fretboard_texture_rect.tscn")
var chord_to_load = fretboard.instantiate() # = fretboard.instance()
#tonika_texture_rect.add_child(chord)
match key:
"C":
chord_to_load.show_chord(key)
print("Key C loaded")
"D":
chord_to_load.show_chord(key)
print("Key D loaded")
Thanks for your help! I'm coming from C# and Unity, but GDScript is new for me, too.