Why SQL?
There is a book out there called “Design Patterns: Elements of Reusable Object-Oriented Software”, which I would recommend as a general catch-all for questions like this.
But more specifically to your question: Your idea with an ability script is not bad if you have only a handful of abilities. It will probably go off the rails as you approach 10 abilities, and making changes will be quite painful at that point.
You should first think about what all the abilities will have in common, and pull that mess of stuff into its own class. Each ability will presumably have a name, a text description, a sprite for the UI, and stuff like that.
Abilities may differ in how they are used, i.e. some abilities may do damage to enemies, while others might buff the player, and others might be utility like showing more of the map. For this reason, you might want a general catch-all method like use_ability()
that differs from ability to ability.
Abilities might also only be usable sometimes. Like, maybe you can only use the bazooka once every 10 seconds, or maybe the flashlight only works in a dark room. So maybe a catch-all can_use_ability()
.
This is just a “for instance”, because your question is quite general and does not provide any specifics.
So you’d end up with a class
class BaseAbility:
var name:String
var description:String
var ui_sprite:Texture
func can_use_ability():
pass
func use_ability()
pass
Then in any code that uses abilities, like the handler for a keystroke, you could do something like
func _on_use_ability_key_pressed():
if current_ability.can_use_ability():
current_ability.use_ability()
You’d then create abilities by subclassing BaseAbility, and set current_ability to one of these objects.
I want to stress, though, that this is just one of like, a million different ways to do this. Thus the book recommendation at the beginning. (There’s also a book called “Game Programming Patterns” which I’ve also found useful for game dev). The “correct” way will depend on which dimensions you need flexibility in, how many abilities you plan on having, and lots of other factors.