Wednesday, December 3, 2025

I Don't Get It Either!

I had an issue, I solved it, and I have no idea why my solution works. I also don't know why my old solution didn't work. This sort of thing makes me cranky, so I'm hoping someone else understands it and explains it to me!

Makealotl Music is getting a new game with a lilipad piano. I wanted to include a mode where you play Simon Says with Delia to learn some of the songs in the program. You can see the final version working here:

An oversimplification of what's going on under the hood is that the game stores the song in several different arrays, and then as you progress through the song, it glues the arrays together to build what Delia "listens" for to check if you're wrong or right. Originally I had the arrays stashed on the button itself that you click to pick a song. So, for example, the Mary Had A Little Lamb would store C D E D in an array, and then the game would load C D E D and see if that's what you were playing. You can read the code here in this Pastebin if you want the technical-atude of it all.

This worked great until I added a second button to add a second song. (Brother John, if you were curious.) Once I did that, Godot would not read any array at all, and would crash with a null reference error. (Are those called null reference errors in GDScript? That's what they're called in C#, I've never heard them called anything in GDScript.) I cannot explain why the code would work fine with one object and go south with two objects.

I got to a point where whatever the explanation was wasn't important, I needed the code to work. I got the idea to:

  • Have ONE object, just a regular ol node, run the code that lets Delia check what notes the user plays
  • Store all these arrays in a resource
  • Have the button send the resource up to the regular ol node and force it to read the arrays out that way. Tell me it's null now, you sonofabitch.

Godot 4.5 have a feature that I have been vibrating for. See that fancy "append node" checkbox? A signaling node can now send itself up with a signal! So we can store the resource on the button, have the button signal up, send the button on that signal, then read the resource!

Wow! Except it doesn't work!

If that song_data variable is null then I'm in Unity Engine. No. For whatever reason Godot simply would not read the resource off the button. Notice that the button itself isn't null? I guess it did manage to send itself, but none of its own data?????

Google suggested that sometimes you had to read the resource, duplicate it, then send it up. This doesn't work either.

So, like, don't do that, I guess, or suggest it.

Godot really loves the load() and preload() functions, those are pretty secure, so I tried this on the button:

Which doesn't work, but it lead me to the working solution. I don't love this solution, it's not slick and clean, but it's the only thing that actually works and allows the game to run multiple songs so let's goooo:

Using the connect dialog, send the resource's path as a string.

Then force the Teach Me object to load the path.

So I got this far, I got this working, party party, and then I remembered that I totally forgot that .bind() exists. Oh well, I can't promise that it would work, given that append node wasn't working.

Again, this makes me cranky, I'd rather just merrily be putting .tres files in the inspector by drag and drop. I'd love to know why the other solutions didn't work, and I'm curious to know if any of these will actually work in the upcoming 4.6. If you can explain it, let me know!

No comments:

Post a Comment