Best way to store a large ammount of spells ?
Hi, I'm currently working on a spell system and I kind of hit wall on how to store each spell.
The spells in question have some common property , ex: a name, a description and a cost to cast, they also have common method like OnEquip(), OnCast(), OnDoneCasting(), ect. So I tried using inheritance and created a base class and was creating script that inherited from that class, but as the number of spell grew it as become hard to manage. I then tried using the "composition over inheritance" idea, but again, the number of possible combination of element, effect,animation ect was still a huge problem. So now I wonder if I have other options ? Some people in simmilar situation where told to use database/XML to store enemy and weapons, how would I go about doing that ?
Answer by DialBlitzness · Aug 10, 2020 at 01:29 PM
Hello !
I can show you my solution, i explain this in another topic but it's possibly the best for you. Here : Loader and database
I think writing C # or XML is the same, so you might as well stay in a controlled environment.
Your method is certainly an interesting idea, I will try to implement it, aside from some edge case it should work for most of my spells, and I could have a single script that simply reference the spells database to know what was casted and apply the right effect at the right time, maybe by storing them as : DataSpells[1] = PopulateDataSpell("fireball", cost, damage, effect, animation, "it goes boom when it hit something"); But that leave me with the problem of the various effect of the spells, for exemple, a fireball will light you on fire, a lightning ball will immobilise you for x seconds, ect, and also for the various animations, how do I reference those ? Create a list of effect, and add them as parameters like in my exemple and reference them by name to find them ? Also, in your exemple I see a "Remplir les données", a fellow french speaker ?
Haha sorry, i'm a french developer, i forgot to rewrite this comment :D
You fully understood the idea for the implementation of spells.
- For the different animations, you have to add in DataSpell an "AnimationName" property that you will fill like the rest in the "Database" class. This name must be the name of the prefab that contains your animation. Then, create a DataSpell.Instantiate () method which will have the role of creating the instance of this animation.
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
[System.Serializable]
public class DataSpells
{
public string Name = string.Empty;
public string Description = string.Empty;
public string ParticleEffect = string.Empty;
public string SoundEffect = string.Empty;
public string tag = string.Empty;
public DataSpells(){}
public DataSpells(string name, string tag)
{
this.Name = name;
this.tag = tag;
}
public GameObject Instantiate(float PlayerXPosition, float PlayerYPosition)
{
// Création de l'effet dans la scène
GameObject go = GameObject.Instantiate(
((GameObject)AssetDatabase.LoadAssetAtPath(LinkEffect(this.Name), typeof(GameObject)))
, new Vector3(PlayerXPosition, PlayerYPosition, 0), Quaternion.identity) as GameObject;
return go;
}
public string LinkEffect(string name)
{
return "YourPath"+name+".prefab";
}
}
- For the different effects, if it's turn-based combat, you can create a unique script to associate with each spell concerned according to their effects. But if it is in real time in the environment with multiple gameobjects, I would advise you rather to associate a script similar to all the gameobjects which will make the object react according to the spell which strikes it, thanks to OnCollideEnter (), OnTriggerEnter () or OnParticleCollide () for example.
This is kind of what I am doing at the moment, I have a single script for each spells. I am actually in the process of recoding everything using your recommendation, for now it seem to work and instead of creating a script for each spells I made a few list for the effects, animation and sound and a script that just use the parameter of the spell to find what it need in the list and instantiate them (actually not instantiate, for many things I use a "pool" created at startup, so I dont slow down my code by instantiating continuously).
the Script then put all the pieces together and fire the spell so a spells kinda look like this for the fireball : dataSpell("fireball", mana cost, damage, animation name for in hand, casting, and impact, sound for in hand casting and impact). I wonder if makings an enum for all the possible prefabs cathegories and create a simple class to contain the spell so they are easy to edit and create using those enum would make sence !
I'm from quebec "alors bonjour a toi et pas besoin de t'excuser !"
Your answer

Follow this Question
Related Questions
Load a 3d model from firebase storage using API to WebGL project 0 Answers
How to handle inventory in 2d game, via xml, no game objects (inheritance? interface?) 0 Answers
Dynamic spells and keybindings 0 Answers
Whats the best option for an item database for a single player game 2 Answers
Saving data with files 1 Answer