Super() not Pasta


This post is a technical one talking about GDScript in particular.

During the game jam, a lot of code was made to work but did not follow good programming practices. Especially as the deadline was looming only days or hours away, I just had to make things work. Well now I have time go back and fix those things. 

One of the issues was that I did a lot of copy-pasta for all the interactable objects and when they highlight. I was at my day job and was thinking how I would remove all the duplicate code and have a nice clean system. It was like a fart in the grocery isle, it just hit me out of nowhere and I realized that I had already been using the feature I needed.

I have a base class called InteractableItem that inherits from a Node2D. It has a function called highlight(). This would run anytime the characters interaction_area was overlapping the item to highlight. This is fine for items like the books or scrolls in the library (lvl 2) but not so much for when things should only highlight when some condition is true. For example in the sanctuary (lvl 3) the block the torch is placed on should not highlight until all the other objects are setup correctly.  I would copy-paste the highlight code to the script for that particular object and set its conditionals. The way I have figured out is to use the call super(). super() calls the parent method of the same name and so I just had to inherit from the base InteractableItem class and override the highlight() function. I then could set the condition in which the object is supposed to highlight and then call super() to activate the outline. 

I was already using super() in the _ready function but just without the conditional statement so it did not occur to me at the time to use it in the highlight function too. 


In doing this I am able to remove almost 20 instances of the copied highlight code and  condense it into just one place in the base IteractableItem class.

I did discover something strange (to me at least). If I override a parent function, for example _ready() but I don't call super() then none of the un-overridden functions of the parent class work.  This was an issue when I was working on the pickup items and only needed the _ready() function to do item specific setup but wanted the parents default highlight() function to work as-is. It was fixed by calling super() in the one overridden function and the parents default methods started working.

This is a little insight into my thoughts and experiences though this journey  of learning game dev using Godot. Some will find this to be basic stuff. Others might not have a clue what I'm talking about.  Even with a handful of experience with other programming languages and game engines, you never know what fiddly little bit is going to trip you up. All systems have their quirks and this is one I ran into.  I'm sure there are many more to come but that will be for another time and another dev log.

Until next time rubber duck,

SwampN3rd

Get Escape the Alchemist’s Cult

Leave a comment

Log in with itch.io to leave a comment.