i’m making a game and to sum it up really quickly, i’d like to have a lot of spells (~300)
since i’m a beginner i’m just asking now so my code doesn’t eventually become a mess but how would you handle having that many spells that the player can use without it being laggy or a mess to code ?
define a class API that will be able to generically interact with each spell.
Extend the generic spell class with each spell, overloading the generic class functions with unique spell details.
The idea is to define the generic class with all common interactions. (Cast, is in cool down, etc )
You could even create a spell category API that extends the generic, if the spells can’t fit into one generic class. This will complicate the usage code but it will still be better then not doing it.
There are probably many ways to do this beyond class inheritance in oop. But I think, in practice, anything you do would still be the same.
Find a common set of attributes that all spells share and generalize an interface that the usage code will not need to know specific details about the thing it is interacting with. (Aka an abstraction)
Creating a spell system is complex, especially for a beginner. You might need to consider the following things:
Spell
– Defining each spell: id, stats (like damage, damage type), casting time, mana cost etc. I would recommend creating a custom Resource for spells.
Spell inventory
– Keeping track of what spells the player knows, like an array of known spell ids
Casting a spell
– Inputs, checking if casting is possible (enough mana?)
Effects of spells
– How a spell affects the world? What happens to enemies? Checking collisions etc
Graphics (animations, particles, shaders) and audio
Your spell resouce script could look like this (very simplified):
class_name Spell extends Resource
enum ID {
DEFAULT,
FIREBALL,
LIGHTNING_STORM,
HEALING
}
@export var id: ID = ID.DEFAULT
@export var fire_damage: int = 0
@export var lightning_damage: int = 0
@export var hp_healed: int = 0
@export var casting_time: int = 0
@export var mana_cost: int = 0
Now you can create Spell resources and define values for each spell. You can also create more custom resources that extend the Spell resource (for different spell types). Enum ids let you easily identify spells.
But because you don’t have ~300 spells right now, I recommend that you start by creating a couple of spells in whatever way seems the easiest and most intuitive to you. That way you will find out what you actually need for your spell system. As a beginner it’s very hard to start planning and designing a complex system from scratch. And if your game is in its early stages, you might realize that you actually need only a couple of spells instead of hundreds.