Update # 4 - refactoring and other code changes


Time for another update! Some time has passed, as real life goes on and you need to take care of things. But let's continue.

I am using the Godot Engine to make this game. It's fun, flexible, exports to all major operating systems and it's free and ready to run from Steam of all places.  Of course it is not without its quirks (like the need to force shader compiling to avoid hiccups during game-play) but that is a small price to pay for such a good general purpose game engine.

As a game grows, so does its code base. I've been wrestling with a few things:

  • Communication between game objects got more and more complex, and don't feel "right". You know that feeling?
  • Silly little bugs that annoy me but are hard to track down.

Let's look at the first point. One design pattern that I used in my other games was that of an event bus. I knew this would make signalling easier and more simple to track.  I was not sure how to implement this in Godot until I found this great GDQuest tutorial. Their implementation of this pattern as an auto-load script (singleton)  is simple and effective.

Mine looks like this now:

# events.gd
# autoload script
extends Node
# gui signals
signal fuel_changed(amount)
signal lives_changed(value)
signal level_changed(value)
signal score_changed(value)
signal hiscore_changed(value)
signal stats_visible(bool_value)
# screen signals
signal screen_wipe_requested(name)
signal screen_wipe_finished(name)
# game event signals
signal player_died(position)

And every object in the game can send a global signal using:

Events.emit_signal("fuel_changed", fuel_amount)

Every object in the game can also listen to any global event by connecting to it by:

Events.connect("fuel_changed", self, "_on_fuel_update")

Using this has also allowed me to fully decouple the GUI, scene fader and switcher from the rest of the game code, making re-use much simpler. The scene tree has changed a bit because of this but it looks a lot cleaner now. I am sure I will now also be able to free the landscape bits from the Play scene as well.

Which leads to the next issue. Small irritating bugs.  As I started refactoring code I thought it was a good time to start implementing more strict code rules. Blitz Max in particular was excellent at this when using the strict keyword. This required you to define each and every variable and object. This is not possible in GDScript, but the next best thing is called Static Typing.  While adding this I solved several small bugs that eluded me from the start.

Growing pains. We all go through them. But I am happy now and am almost ready to start producing some audio and sound effects. For that, I am going to use ChipTone, right here on itch. What a great community!

Get Scramble RND Revisited

Download NowName your own price

Comments

Log in with itch.io to leave a comment.

(+1)

Always interesting to read these posts about Scramble, Wiebo. I had a similar problem with GMS 2 when I wrote my first game using that. I had lots of separate objects all off doing their own thing and no dedicated game handler object to keep track of them all. It caused all sorts of wild problems with controlling instances of objects to update them with information from other objects to try and keep everything working correctly, even for something simple like a Pause mode. 

I learnt a lot from doing a bit of research before I started the next game as it's so much simpler to avoid future problems when you make sure you know what you're doing first.

Good luck with what you're doing.

Thanks Xerra.

Yes, some things can be avoided by doing a bit of research beforehand. The Godot scene tree concept does introduce some unique challenges that I've never experienced before because I never used such a system to keep track of game objects. The issues seem to slowly creep up on you as the size of the game grows.

Choices made in the scene tree structure can really impact you. I'm getting better at it and I'm just going along with it. Sure, it takes a bit more time to make progress, but that's fine by me.